From 86112e2440df6a72e2bc8731a5cf128f3b07de7f Mon Sep 17 00:00:00 2001 From: pbi Date: Wed, 27 Apr 2005 11:53:32 +0000 Subject: [PATCH] - early BSD port with libdnet and libpcap wrappers --- scapy.py | 378 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 260 insertions(+), 118 deletions(-) diff --git a/scapy.py b/scapy.py index 9924ec889..83f32ab25 100755 --- a/scapy.py +++ b/scapy.py @@ -21,6 +21,9 @@ # # $Log: scapy.py,v $ +# Revision 0.9.17.85 2005/04/27 13:53:32 pbi +# - early BSD port with libdnet and libpcap wrappers +# # Revision 0.9.17.84 2005/04/24 14:57:45 pbi # - added a usable geolocation database from GeoIP. # @@ -739,7 +742,7 @@ from __future__ import generators -RCSID="$Id: scapy.py,v 0.9.17.84 2005/04/24 14:57:45 pbi Exp $" +RCSID="$Id: scapy.py,v 0.9.17.85 2005/04/27 13:53:32 pbi Exp $" VERSION = RCSID.split()[2]+"beta" @@ -905,16 +908,25 @@ try: import Gnuplot GNUPLOT=1 except ImportError: + print "WARNING: did not find gnuplot lib. Won't be able to plot" GNUPLOT=0 -PCAP=0 -DNET=0 + +LINUX=sys.platform.startswith("linux") + + +if LINUX: + DNET=PCAP=0 +else: + DNET=PCAP=1 + if PCAP: try: import pcap PCAP = 1 except ImportError: + print "WARNING: did not find pcap module. Fallback to linux primitives" PCAP = 0 if DNET: @@ -922,6 +934,7 @@ if DNET: import dnet DNET = 1 except ImportError: + print "WARNING: did not find dnet module. Fallback to linux primitives" DNET = 0 try: @@ -1243,7 +1256,7 @@ class Route: nhop = thenet dev,ifaddr,x = self.route(nhop) else: - ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",dev)) + ifreq = ioctl(self.s, SIOCGIFADDR,struct.pack("16s16x",dev)) ifaddr = socket.inet_ntoa(ifreq[20:24]) return (atol(thenet),(1L<= 2.3 :( + stats = dict(map(lambda x: (x,0), self.stats)) other = 0 for r in self.res: f = 0 @@ -5452,15 +5563,57 @@ class L2ListenSocket(SuperSocket): -if DNET and PCAP: - # XXX: works only for Ethernet - class L3dnetSocket(SuperSocket): - def __init__(self, type = None, filter=None, promisc=None, iface=None): - self.iflist = {} - self.ins = pcap.pcapObject() - if iface is None: - iface = "any" - self.ins.open_live(iface, 1600, 0, 100) +# XXX: works only for Ethernet +class L3dnetSocket(SuperSocket): + def __init__(self, type = None, filter=None, promisc=None, iface=None): + self.iflist = {} + self.ins = pcap.pcapObject() + if iface is None: + iface = "any" + self.ins.open_live(iface, 1600, 0, 100) + if conf.except_filter: + if filter: + filter = "(%s) and not (%s)" % (filter, conf.except_filter) + else: + filter = "not (%s)" % conf.except_filter + if filter: + self.ins.setfilter(filter, 0, 0) + def send(self, x): + if hasattr(x,"dst"): + iff,a,gw = conf.route.route(x.dst) + else: + iff = conf.iface + ifs = self.iflist.get(iff) + if ifs is None: + self.iflist[iff] = ifs = dnet.eth(iff) + ifs.send(str(Ether()/x)) + def recv(self,x): + ll = self.ins.datalink() + if LLTypes.has_key(ll): + cls = LLTypes[ll] + else: + warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using Ethernet" % (sa_ll[0],sa_ll[1],sa_ll[3])) + cls = Ether + + pkt = self.ins.next()[1] + try: + pkt = cls(pkt) + except: + pkt = Raw(pkt) + return pkt.payload + def close(self): + if hasattr(self, "ins"): + del(self.ins) + if hasattr(self, "outs"): + del(self.outs) + +class L2dnetSocket(SuperSocket): + def __init__(self, iface = None, type = ETH_P_ALL, filter=None): + if iface is None: + iface = conf.iface + self.ins = pcap.pcapObject() + self.ins.open_live(iface, 1600, 0, 100) + if type == ETH_P_ALL: # Do not apply any filter if Ethernet type is given if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) @@ -5468,79 +5621,61 @@ if DNET and PCAP: filter = "not (%s)" % conf.except_filter if filter: self.ins.setfilter(filter, 0, 0) - def send(self, x): - if hasattr(x,"dst"): - iff,a,gw = conf.route.route(x.dst) - else: - iff = conf.iface - ifs = self.iflist.get(iff) - if ifs is None: - self.iflist[iff] = ifs = dnet.eth(iff) - ifs.send(str(Ether()/x)) - def recv(self,x): - return Ether(self.ins.next()[1][2:]).payload - def close(self): - if hasattr(self, "ins"): - del(self.ins) - if hasattr(self, "outs"): - del(self.outs) + self.outs = dnet.eth(iface) + def recv(self,x): + ll = self.ins.datalink() + if LLTypes.has_key(ll): + cls = LLTypes[ll] + else: + warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using Ethernet" % (sa_ll[0],sa_ll[1],sa_ll[3])) + cls = Ether - class L2dnetSocket(SuperSocket): - def __init__(self, iface = None, type = ETH_P_ALL, filter=None): - if iface is None: - iface = conf.iface - self.ins = pcap.pcapObject() - self.ins.open_live(iface, 1600, 0, 100) - if type == ETH_P_ALL: # Do not apply any filter if Ethernet type is given - if conf.except_filter: - if filter: - filter = "(%s) and not (%s)" % (filter, conf.except_filter) - else: - filter = "not (%s)" % conf.except_filter - if filter: - self.ins.setfilter(filter, 0, 0) - self.outs = dnet.eth(iface) - def recv(self,x): - return Ether(self.ins.next()[1]) - def close(self): - if hasattr(self, "ins"): - del(self.ins) - if hasattr(self, "outs"): - del(self.outs) - - - + pkt = self.ins.next()[1] + try: + pkt = cls(pkt) + except: + pkt = Raw(pkt) + return pkt -if PCAP: - class L2pcapListenSocket(SuperSocket): - def __init__(self, iface = None, type = ETH_P_ALL, promisc=None, filter=None): - self.type = type - self.outs = None - self.ins = pcap.pcapObject() - if iface is None: - iface = "any" - if promisc is None: - promisc = conf.sniff_promisc - self.promisc = promisc - self.ins.open_live(iface, 1600, self.promisc, 100) - if type == ETH_P_ALL: # Do not apply any filter if Ethernet type is given - if conf.except_filter: - if filter: - filter = "(%s) and not (%s)" % (filter, conf.except_filter) - else: - filter = "not (%s)" % conf.except_filter - if filter: - self.ins.setfilter(filter, 0, 0) - - def close(self): + def close(self): + if hasattr(self, "ins"): del(self.ins) + if hasattr(self, "outs"): + del(self.outs) - def recv(self, x): - return Ether(self.ins.next()[1][2:]) - - def send(self, x): - raise Exception("Can't send anything with L2pcapListenSocket") + + + + +class L2pcapListenSocket(SuperSocket): + def __init__(self, iface = None, type = ETH_P_ALL, promisc=None, filter=None): + self.type = type + self.outs = None + self.ins = pcap.pcapObject() + if iface is None: + iface = conf.iface + if promisc is None: + promisc = conf.sniff_promisc + self.promisc = promisc + self.ins.open_live(iface, 1600, self.promisc, 100) + if type == ETH_P_ALL: # Do not apply any filter if Ethernet type is given + if conf.except_filter: + if filter: + filter = "(%s) and not (%s)" % (filter, conf.except_filter) + else: + filter = "not (%s)" % conf.except_filter + if filter: + self.ins.setfilter(filter, 0, 0) + + def close(self): + del(self.ins) + + def recv(self, x): + return Ether(self.ins.next()[1]) + + def send(self, x): + raise Exception("Can't send anything with L2pcapListenSocket") @@ -7574,6 +7709,13 @@ warning_threshold : how much time between warnings from the same place conf=Conf() +if PCAP: + conf.L2listen=L2pcapListenSocket + if DNET: + conf.L3socket=L3dnetSocket + conf.L2socket=L2dnetSocket + + p0f_kdb = p0fKnowledgeBase(conf.p0f_base) queso_kdb = QuesoKnowledgeBase(conf.queso_base) nmap_kdb = NmapKnowledgeBase(conf.nmap_base)