-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdecode_udp.py
56 lines (45 loc) · 1.95 KB
/
decode_udp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/python
#
# This program decodes UDP packets from the wire
import dpkt
import sys
import socket
import pcap
def decode_udp ( pc ) :
"""decode_udp is a generator function that listens to a pcap.pcap object and returns a UDP object when it hears a packet"""
for ts, pkt in pc:
# parse the packet. Decode the ethertype
eth = dpkt.ethernet.Ethernet(pkt)
if eth.type == dpkt.ethernet.ETH_TYPE_IP :
ip = eth.data
if ip.p == dpkt.ip.IP_PROTO_UDP :
# This doesn't deal with IP fragments
udp = ip.data
# Pass the IP addresses, source port, destination port, and data back to the caller.
yield ( ip.src, udp.sport, ip.dst, udp.dport, udp.data, ip.v)
elif eth.type == dpkt.ethernet.ETH_TYPE_IP6 :
ip = eth.data
if ip.nxt == dpkt.ip.IP_PROTO_UDP :
# This doesn't deal with IP fragments
udp = ip.data
# Pass the IP addresses, source port, destination port, and data back to the caller.
yield ( ip.src, udp.sport, ip.dst, udp.dport, udp.data, ip.v)
else :
# If the packet is something else, then I need to figure out a better way of handling it.
pass
def main() :
if sys.argv[1] == "-i" :
pc = pcap.pcap( sys.argv[2] )
elif sys.argv[1] == "-f" :
pc = dpkt.pcap.Reader( open ( sys.argv[2] ) )
else :
print """Use -i INTERFACE to packet capture from an interface.
Use -f FILENAME to read a packet capture file"""
sys.exit(2)
for src, sport, dst, dport, data, ip_version in decode_udp( pc ) :
if ip_version == 4 :
print "from ", socket.inet_ntoa(src),":",sport, " to ", socket.inet_ntoa(dst),":",dport
else :
print "from ", socket.inet_ntop(AF_INET6, src),".",sport, " to ", socket.inet_ntop(AF_INET6, dst), ".", dport
if __name__ == "__main__" :
main()