2014-12-03 3 views
7

파이썬 패킷 구문 분석/스니핑 도구 Scapy를 사용하여 원시 바이트 문자열에서 패킷을 만들고 싶습니다. 이 맡아 봤다고 것처럼 나는 같은 모양 원시 바이트에서 new_packet을 생산할 수있는 방법원시 바이트에서 Scapy 패킷을 만드는 방법

# Get an example packet (we won't really have an offline file in production.) 
pkt = sniff(offline="./example_packets/example_packets2.pcap") 

# Convert it to raw bytes -- oddly __str__ does this. 
raw_packet = str(pkt) 

# Current, broken, attempt to construct a new packet from the same bytes as the old. 
# In truth, there are easier ways to copy packets from existing Scapy packets, but 
# we are really just using that offline packet as a convenient example. 
new_packet = Packet(_pkt=raw_packet) 

# Sadly, while this packet has the bytes internally, it no longer has the 
# interpretations of the layers like the original packet did (such as saying that 
# the packet is ARP and has these field values, etc. 
print repr(new_packet) 

: 내 특정 사용 케이스의 세부 사항이 더 현실적인 있지만, 다음과 같은 예를 들어 내 문제와 내 현재의 시도를 보여 pcap 파일에서?

답변

7

Scapy가 패킷의 첫 번째 계층을 추측 할 방법이 없으므로이를 지정해야합니다.

해당 Packet 하위 클래스를 사용하면됩니다. 예를 들어 패킷의 첫 번째 계층이 이더넷이라고 가정하면 Packet(raw_packet) 대신 Ether(raw_packet)을 사용하십시오.

+1

'Ether'클래스를 사용하는 경우 Scapy가 다른 레이어를 자동으로 결정할 수 있습니까? 'sniff' 함수가 어떻게 든 다른 레이어를 파악하는 것 같습니다. – BlackVegetable

+1

다른 레이어에서 이더넷 레이어의 레이어를 의미하는 경우 'type' 필드 덕분에 대답은'예 '입니다. – Pierre

+0

물론! 그게 내게 분명 했어야 했어. 해명 해줘서 고마워. (라우터가 타입 정보가 아닌 다른 레이어를 해석하는 방법을 알고 싶다면?) – BlackVegetable

관련 문제