-
Notifications
You must be signed in to change notification settings - Fork 4
/
ip_dst_src.py
103 lines (93 loc) · 5.09 KB
/
ip_dst_src.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python2
#from dpkt.ip import IP, IP_PROTO_UDP
try:
from dpkt.tcp import TCP
from dpkt.udp import UDP
import dpkt
except ImportError:
print "dpkt module is missing, try running apt-get install python-dpkt\n"
exit()
try:
from IPy import IP
except ImportError:
print "IPy module is missing, try running apt-get install python-IPy\n"
exit()
try:
from netaddr import IPNetwork
import pprint
except ImportError:
print "netaddr module is missing, try running apt-get install python-netaddr\n"
exit()
import sys
import socket
#rfc1819 ip address ranges
classA="10.0.0.0/8"
classB="172.16.0.0/12"
classC="192.168.0.0/16"
def dst_pkts(pcap):
for ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
if eth.type == dpkt.ethernet.ETH_TYPE_IP:
if IP(socket.inet_ntoa(ip.dst)).iptype() == "PRIVATE":
if type(ip.data) == UDP:
#print socket.inet_ntoa(ip.dst), "UDP"
packettype="UDP"
elif type(ip.data) == TCP:
#print socket.inet_ntoa(ip.dst), "TCP"
packettype="TCP"
else:
pass
if str(IP(socket.inet_ntoa(ip.dst))) in (IPNetwork(classA)):
if str(IP(socket.inet_ntoa(ip.dst))).split('.')[3] == "255" or str(IP(socket.inet_ntoa(ip.dst))).split('.')[3] == "0":
print "Possible IP address not in a /24 CIDR: %s" % (IP(socket.inet_ntoa(ip.dst)))
else:
print "%s\t%s\t%s.%s.%s.%s" % ((IP(socket.inet_ntoa(ip.dst))),packettype, str(IP(socket.inet_ntoa(ip.dst))).split('.')[0], str(IP(socket.inet_ntoa(ip.dst))).split('.')[1], str(IP(socket.inet_ntoa(ip.dst))).split('.')[2], "0/24")
elif str(IP(socket.inet_ntoa(ip.dst))) in (IPNetwork(classB)):
if str(IP(socket.inet_ntoa(ip.dst))).split('.')[3] == "255" or str(IP(socket.inet_ntoa(ip.dst))).split('.')[3] == "0":
print "Possible IP address not in a /24 CIDR: %s" % (IP(socket.inet_ntoa(ip.dst)))
else:
print "%s\t%s\t%s.%s.%s.%s" % ((IP(socket.inet_ntoa(ip.dst))),packettype, str(IP(socket.inet_ntoa(ip.dst))).split('.')[0], str(IP(socket.inet_ntoa(ip.dst))).split('.')[1], str(IP(socket.inet_ntoa(ip.dst))).split('.')[2], "0/24")
elif str(IP(socket.inet_ntoa(ip.dst))) in (IPNetwork(classC)):
if str(IP(socket.inet_ntoa(ip.dst))).split('.')[3] == "255" or str(IP(socket.inet_ntoa(ip.dst))).split('.')[3] == "0":
print "Possible IP address not in a /24 CIDR: %s" % (IP(socket.inet_ntoa(ip.dst)))
else:
print "%s\t%s\t%s.%s.%s.%s" % ((IP(socket.inet_ntoa(ip.dst))),packettype, str(IP(socket.inet_ntoa(ip.dst))).split('.')[0], str(IP(socket.inet_ntoa(ip.dst))).split('.')[1], str(IP(socket.inet_ntoa(ip.dst))).split('.')[2], "0/24")
def src_pkts(pcap):
for ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
if eth.type == dpkt.ethernet.ETH_TYPE_IP:
if IP(socket.inet_ntoa(ip.src)).iptype() == "PRIVATE":
if type(ip.data) == UDP:
packettype="UDP"
elif type(ip.data) == TCP:
packettype="TCP"
else:
pass
if str(IP(socket.inet_ntoa(ip.src))) in (IPNetwork(classA)):
if str(IP(socket.inet_ntoa(ip.src))).split('.')[3] == "255" or str(IP(socket.inet_ntoa(ip.src))).split('.')[3] == "0":
print "Possible IP address not in a /24 CIDR: %s" % ((IP(socket.inet_ntoa(ip.src))))
else:
print "%s\t%s\t%s.%s.%s.%s" % ((IP(socket.inet_ntoa(ip.src))), packettype, str(IP(socket.inet_ntoa(ip.src))).split('.')[0], str(IP(socket.inet_ntoa(ip.src))).split('.')[1], str(IP(socket.inet_ntoa(ip.src))).split('.')[2], "0/24")
elif str(IP(socket.inet_ntoa(ip.src))) in (IPNetwork(classB)):
if str(IP(socket.inet_ntoa(ip.src))).split('.')[3] == "255" or str(IP(socket.inet_ntoa(ip.src))).split('.')[3] == "0":
print "Possible IP address not in a /24 CIDR: %s" % ((IP(socket.inet_ntoa(ip.src))))
else:
print "%s\t%s\t%s.%s.%s.%s" % ((IP(socket.inet_ntoa(ip.src))), packettype, str(IP(socket.inet_ntoa(ip.src))).split('.')[0], str(IP(socket.inet_ntoa(ip.src))).split('.')[1], str(IP(socket.inet_ntoa(ip.src))).split('.')[2], "0/24")
elif str(IP(socket.inet_ntoa(ip.src))) in (IPNetwork(classC)):
if str(IP(socket.inet_ntoa(ip.src))).split('.')[3] == "255" or str(IP(socket.inet_ntoa(ip.src))).split('.')[3] == "0":
print "Possible IP address not in a /24 CIDR: %s" % ((IP(socket.inet_ntoa(ip.src))))
else:
print "%s\t%s\t%s.%s.%s.%s" % ((IP(socket.inet_ntoa(ip.src))), packettype, str(IP(socket.inet_ntoa(ip.src))).split('.')[0], str(IP(socket.inet_ntoa(ip.src))).split('.')[1], str(IP(socket.inet_ntoa(ip.src))).split('.')[2], "0/24")
def decode_file(fname):
pcap = dpkt.pcap.Reader(fname)
dst_pkts(pcap)
src_pkts(pcap)
def main():
try:
fname = open(sys.argv[1])
decode_file(fname)
except IOError:
print "The file %s, does not exit or cannot be found." % sys.argv[1]
main()