Changeset 918:c6d5e354eb6f
- Timestamp:
- 09/12/08 17:57:20
(4 months ago)
- Author:
- Phil <phil@secdev.org>
- Message:
Scapy6 adapatations to have it work inside scapy 2
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r914 |
r918 |
|
| 313 | 313 | temp_files = [] |
|---|
| 314 | 314 | netcache = NetCache() |
|---|
| 315 | | load_layers = ["l2", "inet", "dhcp", "dns", "dot11", "gprs", "hsrp", "ip6", "ir", "isakmp", "l2tp", |
|---|
| | 315 | load_layers = ["l2", "inet", "dhcp", "dns", "dot11", "gprs", "hsrp", "inet6", "ir", "isakmp", "l2tp", |
|---|
| 316 | 316 | "mgcp", "mobileip", "netbios", "netflow", "ntp", "ppp", "radius", "rip", "rtp", |
|---|
| 317 | 317 | "sebek", "skinny", "smb", "snmp", "tftp", "x509", "bluetooth" ] |
|---|
| r880 |
r918 |
|
| 278 | 278 | ck=checksum(psdhdr+p) |
|---|
| 279 | 279 | p = p[:16]+struct.pack("!H", ck)+p[18:] |
|---|
| 280 | | elif isinstance(self.underlayer, IPv6) or isinstance(self.underlayer, _IPv6OptionHeader): |
|---|
| | 280 | elif isinstance(self.underlayer, inet6.IPv6) or isinstance(self.underlayer, inet6._IPv6ExtHdr): |
|---|
| 281 | 281 | ck = in6_chksum(socket.IPPROTO_TCP, self.underlayer, p) |
|---|
| 282 | 282 | p = p[:16]+struct.pack("!H", ck)+p[18:] |
|---|
| … | … | |
| 302 | 302 | if isinstance(self.underlayer, IP): |
|---|
| 303 | 303 | return self.underlayer.sprintf("TCP %IP.src%:%TCP.sport% > %IP.dst%:%TCP.dport% %TCP.flags%") |
|---|
| 304 | | elif isinstance(self.underlayer, IPv6): |
|---|
| | 304 | elif isinstance(self.underlayer, inet6.IPv6): |
|---|
| 305 | 305 | return self.underlayer.sprintf("TCP %IPv6.src%:%TCP.sport% > %IPv6.dst%:%TCP.dport% %TCP.flags%") |
|---|
| 306 | 306 | else: |
|---|
| … | … | |
| 332 | 332 | ck=checksum(psdhdr+p) |
|---|
| 333 | 333 | p = p[:6]+struct.pack("!H", ck)+p[8:] |
|---|
| 334 | | elif isinstance(self.underlayer, IPv6) or isinstance(self.underlayer, _IPv6OptionHeader): |
|---|
| | 334 | elif isinstance(self.underlayer, inet6.IPv6) or isinstance(self.underlayer, inet6._IPv6ExtHdr): |
|---|
| 335 | 335 | ck = in6_chksum(socket.IPPROTO_UDP, self.underlayer, p) |
|---|
| 336 | 336 | p = p[:6]+struct.pack("!H", ck)+p[8:] |
|---|
| … | … | |
| 353 | 353 | if isinstance(self.underlayer, IP): |
|---|
| 354 | 354 | return self.underlayer.sprintf("UDP %IP.src%:%UDP.sport% > %IP.dst%:%UDP.dport%") |
|---|
| 355 | | elif isinstance(self.underlayer, IPv6): |
|---|
| | 355 | elif isinstance(self.underlayer, inet6.IPv6): |
|---|
| 356 | 356 | return self.underlayer.sprintf("UDP %IPv6.src%:%UDP.sport% > %IPv6.dst%:%UDP.dport%") |
|---|
| 357 | 357 | else: |
|---|
| … | … | |
| 926 | 926 | ports_done = {} |
|---|
| 927 | 927 | for s,r in self.res: |
|---|
| 928 | | r = r[IP] or r[IPv6] or r |
|---|
| 929 | | s = s[IP] or s[IPv6] or s |
|---|
| | 928 | r = r[IP] or r[inet6.IPv6] or r |
|---|
| | 929 | s = s[IP] or s[inet6.IPv6] or s |
|---|
| 930 | 930 | ips[r.src] = None |
|---|
| 931 | 931 | if TCP in s: |
|---|
| … | … | |
| 938 | 938 | trace_id = (s.src,s.dst,s.proto,0) |
|---|
| 939 | 939 | trace = rt.get(trace_id,{}) |
|---|
| 940 | | ttl = IPv6 in s and s.hlim or s.ttl |
|---|
| 941 | | if not (ICMP in r and r[ICMP].type == 11) and not (IPv6 in r and ICMPv6TimeExceeded in r): |
|---|
| | 940 | ttl = inet6.IPv6 in s and s.hlim or s.ttl |
|---|
| | 941 | if not (ICMP in r and r[ICMP].type == 11) and not (inet6.IPv6 in r and ICMPv6TimeExceeded in r): |
|---|
| 942 | 942 | if trace_id in ports_done: |
|---|
| 943 | 943 | continue |
|---|
| … | … | |
| 1222 | 1222 | conf.stats_classic_protocols += [TCP,UDP,ICMP] |
|---|
| 1223 | 1223 | conf.stats_dot11_protocols += [TCP,UDP,ICMP] |
|---|
| | 1224 | |
|---|
| | 1225 | from scapy.layers import inet6 |
|---|
| r917 |
r918 |
|
| 2 | 2 | ############################################################################# |
|---|
| 3 | 3 | ## ## |
|---|
| 4 | | ## scapy6.py --- IPv6 support for Scapy ## |
|---|
| 5 | | ## see http://namabiiru.hongo.wide.ad.jp/scapy6/ ## |
|---|
| 6 | | ## for more informations ## |
|---|
| | 4 | ## inet6.py --- IPv6 support for Scapy ## |
|---|
| | 5 | ## see http://natisbad.org/IPv6/ ## |
|---|
| | 6 | ## for more informations ## |
|---|
| 7 | 7 | ## ## |
|---|
| 8 | 8 | ## Copyright (C) 2005 Guillaume Valadon <guedou@hongo.wide.ad.jp> ## |
|---|
| … | … | |
| 11 | 11 | ## This program is free software; you can redistribute it and/or modify it ## |
|---|
| 12 | 12 | ## under the terms of the GNU General Public License version 2 as ## |
|---|
| 13 | | ## published by the Free Software Foundation; version 2. ## |
|---|
| | 13 | ## published by the Free Software Foundation. ## |
|---|
| 14 | 14 | ## ## |
|---|
| 15 | 15 | ## This program is distributed in the hope that it will be useful, but ## |
|---|
| … | … | |
| 20 | 20 | ############################################################################# |
|---|
| 21 | 21 | |
|---|
| 22 | | from scapy import * |
|---|
| 23 | | import __builtin__ |
|---|
| | 22 | |
|---|
| | 23 | from scapy.layers.l2 import * |
|---|
| | 24 | from scapy.layers.inet import * |
|---|
| | 25 | from scapy.layers.dns import * |
|---|
| | 26 | from scapy.fields import * |
|---|
| | 27 | from scapy.packet import * |
|---|
| | 28 | from scapy.volatile import * |
|---|
| | 29 | from scapy.config import conf |
|---|
| | 30 | from scapy.sendrecv import sr,sr1,srp1 |
|---|
| | 31 | from scapy.as_resolvers import AS_resolver_riswhois |
|---|
| | 32 | from scapy.supersocket import SuperSocket,L3RawSocket |
|---|
| | 33 | from scapy.arch import * |
|---|
| 24 | 34 | |
|---|
| 25 | 35 | |
|---|
| … | … | |
| 29 | 39 | |
|---|
| 30 | 40 | def get_cls(name, fallback_cls): |
|---|
| 31 | | return __builtin__.__dict__.get(name, fallback_cls) |
|---|
| | 41 | return globals().get(name, fallback_cls) |
|---|
| 32 | 42 | |
|---|
| 33 | 43 | |
|---|
| … | … | |
| 43 | 53 | WINDOWS = sys.platform.startswith("win") |
|---|
| 44 | 54 | |
|---|
| 45 | | if OPENBSD or FREEBSD or NETBSD or DARWIN: |
|---|
| 46 | | loname = "lo0" |
|---|
| 47 | | else: |
|---|
| 48 | | loname = "lo" |
|---|
| 49 | 55 | |
|---|
| 50 | 56 | # From net/ipv6.h on Linux (+ Additions) |
|---|
| … | … | |
| 91 | 97 | elif in6_ismaddr(addr): |
|---|
| 92 | 98 | if in6_ismnladdr(addr): |
|---|
| 93 | | cset = [('::1', 16, loname)] |
|---|
| | 99 | cset = [('::1', 16, LOOPBACK_NAME)] |
|---|
| 94 | 100 | elif in6_ismgladdr(addr): |
|---|
| 95 | 101 | cset = filter(lambda x: x[1] == IPV6_ADDR_GLOBAL, laddr) |
|---|
| … | … | |
| 322 | 328 | if not pathes: |
|---|
| 323 | 329 | warning("No route found for IPv6 destination %s (no default route?)" % dst) |
|---|
| 324 | | return (loname, "::", "::") # XXX Linux specific |
|---|
| | 330 | return (LOOPBACK_NAME, "::", "::") # XXX Linux specific |
|---|
| 325 | 331 | |
|---|
| 326 | 332 | pathes.sort() |
|---|
| … | … | |
| 342 | 348 | # 6to4 address in those available |
|---|
| 343 | 349 | # - dst is link local (unicast or multicast) and multiple output |
|---|
| 344 | | # interfaces are available. Take main one (conf.iface) |
|---|
| | 350 | # interfaces are available. Take main one (conf.iface6) |
|---|
| 345 | 351 | # - if none of the previous or ambiguity persists, be lazy and keep |
|---|
| 346 | 352 | # first one |
|---|
| … | … | |
| 355 | 361 | elif in6_ismaddr(dst) or in6_islladdr(dst): |
|---|
| 356 | 362 | # TODO : I'm sure we are not covering all addresses. Check that |
|---|
| 357 | | tmp = filter(lambda x: x[1][0] == conf.iface, res) |
|---|
| | 363 | tmp = filter(lambda x: x[1][0] == conf.iface6, res) |
|---|
| 358 | 364 | |
|---|
| 359 | 365 | if tmp: |
|---|
| … | … | |
| 442 | 448 | |
|---|
| 443 | 449 | cset = [] # candidate set (possible source addresses) |
|---|
| 444 | | if dev == loname: |
|---|
| | 450 | if dev == LOOPBACK_NAME: |
|---|
| 445 | 451 | if d == '::': |
|---|
| 446 | 452 | continue |
|---|
| … | … | |
| 469 | 475 | xx = "::1" |
|---|
| 470 | 476 | scope = 128 |
|---|
| 471 | | ifname = loname |
|---|
| | 477 | ifname = LOOPBACK_NAME |
|---|
| 472 | 478 | ret.append(xx, scope, ifname) |
|---|
| 473 | 479 | return ret |
|---|
| … | … | |
| 479 | 485 | dp = 0 |
|---|
| 480 | 486 | nh = '::' |
|---|
| 481 | | dev = loname |
|---|
| | 487 | dev = LOOPBACK_NAME |
|---|
| 482 | 488 | cset = ['::1'] |
|---|
| 483 | 489 | routes.append((d, dp, nh, dev, cset)) |
|---|
| … | … | |
| 551 | 557 | if '%' in nh: |
|---|
| 552 | 558 | nh,dev = nh.split('%') |
|---|
| 553 | | if loname in dev: |
|---|
| | 559 | if LOOPBACK_NAME in dev: |
|---|
| 554 | 560 | cset = ['::1'] |
|---|
| 555 | 561 | nh = '::' |
|---|
| … | … | |
| 604 | 610 | |
|---|
| 605 | 611 | def __init__(self): |
|---|
| 606 | | self.neighcache = arp_cache |
|---|
| | 612 | self.neighcache = {} |
|---|
| 607 | 613 | |
|---|
| 608 | 614 | def flush(self, statictoo=True): |
|---|
| … | … | |
| 686 | 692 | return mac |
|---|
| 687 | 693 | |
|---|
| 688 | | iff,a,nh = conf.route6.route(ip6, dev=conf.iface) |
|---|
| 689 | | |
|---|
| 690 | | if iff == loname: |
|---|
| | 694 | iff,a,nh = conf.route6.route(ip6, dev=conf.iface6) |
|---|
| | 695 | |
|---|
| | 696 | if iff == LOOPBACK_NAME: |
|---|
| 691 | 697 | return "ff:ff:ff:ff:ff:ff" |
|---|
| 692 | 698 | |
|---|
| … | … | |
| 1025 | 1031 | tod = struct.pack("!II", i,j) |
|---|
| 1026 | 1032 | # TODO: Add some check regarding system address gathering |
|---|
| 1027 | | rawmac = get_if_raw_hwaddr(conf.iface)[1] |
|---|
| | 1033 | rawmac = get_if_raw_hwaddr(conf.iface6)[1] |
|---|
| 1028 | 1034 | mac = ":".join(map(lambda x: "%.02x" % ord(x), list(rawmac))) |
|---|
| 1029 | 1035 | # construct modified EUI-64 ID |
|---|
| … | … | |
| 1565 | 1571 | SourceIP6Field("src", "dst"), # dst is for src @ selection |
|---|
| 1566 | 1572 | IP6Field("dst", "::1") ] |
|---|
| | 1573 | |
|---|
| | 1574 | def route(self): |
|---|
| | 1575 | dst = self.dst |
|---|
| | 1576 | if isinstance(dst,Gen): |
|---|
| | 1577 | dst = iter(dst).next() |
|---|
| | 1578 | return conf.route6.route(dst) |
|---|
| | 1579 | |
|---|
| 1567 | 1580 | def mysummary(self): |
|---|
| 1568 | 1581 | return "%s > %s (%i)" % (self.src,self.dst, self.nh) |
|---|
| … | … | |
| 1674 | 1687 | return self.payload.answers(other.payload) |
|---|
| 1675 | 1688 | |
|---|
| 1676 | | import scapy |
|---|
| 1677 | | scapy.IPv6 = IPv6 |
|---|
| | 1689 | |
|---|
| | 1690 | conf.neighbor.register_l3(Ether, IPv6, lambda l2,l3: getmacbyip6(l3.dst)) |
|---|
| | 1691 | |
|---|
| 1678 | 1692 | |
|---|
| 1679 | 1693 | class IPerror6(IPv6): |
|---|
| … | … | |
| 1833 | 1847 | name = 'Abstract IPV6 Option Header' |
|---|
| 1834 | 1848 | aliastypes = [IPv6, IPerror6] # TODO ... |
|---|
| 1835 | | |
|---|
| 1836 | | scapy._IPv6OptionHeader = _IPv6ExtHdr |
|---|
| 1837 | 1849 | |
|---|
| 1838 | 1850 | |
|---|
| … | … | |
| 4696 | 4708 | msg = """ |
|---|
| 4697 | 4709 | dhcp6d( dns="2001:500::1035", domain="localdomain, local", duid=None) |
|---|
| 4698 | | iface=conf.iface, advpref=255, sntpservers=None, |
|---|
| | 4710 | iface=conf.iface6, advpref=255, sntpservers=None, |
|---|
| 4699 | 4711 | sipdomains=None, sipservers=None, |
|---|
| 4700 | 4712 | nisdomain=None, nisservers=None, |
|---|
| … | … | |
| 4710 | 4722 | |
|---|
| 4711 | 4723 | iface : the interface to listen/reply on if you do not want to use |
|---|
| 4712 | | conf.iface. |
|---|
| | 4724 | conf.iface6. |
|---|
| 4713 | 4725 | |
|---|
| 4714 | 4726 | advpref : Value in [0,255] given to Advertise preference field. |
|---|
| … | … | |
| 4764 | 4776 | nisdomain=None, nisservers=None, nispdomain=None, |
|---|
| 4765 | 4777 | nispservers=None, bcmcsservers=None, bcmcsdomains=None, |
|---|
| 4766 | | iface=conf.iface, debug=0, advpref=255): |
|---|
| | 4778 | iface=None, debug=0, advpref=255): |
|---|
| 4767 | 4779 | def norm_list(val, param_name): |
|---|
| 4768 | 4780 | if val is None: |
|---|
| … | … | |
| 4778 | 4790 | return -1 |
|---|
| 4779 | 4791 | |
|---|
| | 4792 | if iface is None: |
|---|
| | 4793 | iface = conf.iface6 |
|---|
| | 4794 | |
|---|
| 4780 | 4795 | self.debug = debug |
|---|
| 4781 | 4796 | |
|---|
| … | … | |
| 5931 | 5946 | ############################################################################# |
|---|
| 5932 | 5947 | |
|---|
| 5933 | | L3Types[ETH_P_IPV6] = IPv6 |
|---|
| 5934 | | LLTypes[31] = IPv6 |
|---|
| 5935 | | LLNumTypes[IPv6] = 31 |
|---|
| | 5948 | conf.l3types.register(ETH_P_IPV6, IPv6) |
|---|
| | 5949 | conf.l2types.register(31, IPv6) |
|---|
| 5936 | 5950 | |
|---|
| 5937 | 5951 | bind_layers(Ether, IPv6, type = 0x86dd ) |
|---|
| … | … | |
| 5949 | 5963 | def get_working_if6(): |
|---|
| 5950 | 5964 | """ |
|---|
| 5951 | | try to guess the best interface for conf.iface by looking for the |
|---|
| | 5965 | try to guess the best interface for conf.iface6 by looking for the |
|---|
| 5952 | 5966 | one used by default route if any. |
|---|
| 5953 | 5967 | """ |
|---|
| … | … | |
| 5959 | 5973 | |
|---|
| 5960 | 5974 | conf.route6 = Route6() |
|---|
| 5961 | | conf.iface = get_working_if6() |
|---|
| | 5975 | conf.iface6 = get_working_if6() |
|---|
| 5962 | 5976 | |
|---|
| 5963 | 5977 | if __name__ == '__main__': |
|---|