| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
import re,random,socket |
|---|
| 11 |
import config |
|---|
| 12 |
import error |
|---|
| 13 |
|
|---|
| 14 |
class Gen(object): |
|---|
| 15 |
def __iter__(self): |
|---|
| 16 |
return iter([]) |
|---|
| 17 |
|
|---|
| 18 |
class SetGen(Gen): |
|---|
| 19 |
def __init__(self, set, _iterpacket=1): |
|---|
| 20 |
self._iterpacket=_iterpacket |
|---|
| 21 |
if type(set) is list: |
|---|
| 22 |
self.set = set |
|---|
| 23 |
elif isinstance(set, BasePacketList): |
|---|
| 24 |
self.set = list(set) |
|---|
| 25 |
else: |
|---|
| 26 |
self.set = [set] |
|---|
| 27 |
def transf(self, element): |
|---|
| 28 |
return element |
|---|
| 29 |
def __iter__(self): |
|---|
| 30 |
for i in self.set: |
|---|
| 31 |
if (type(i) is tuple) and (len(i) == 2) and type(i[0]) is int and type(i[1]) is int: |
|---|
| 32 |
if (i[0] <= i[1]): |
|---|
| 33 |
j=i[0] |
|---|
| 34 |
while j <= i[1]: |
|---|
| 35 |
yield j |
|---|
| 36 |
j += 1 |
|---|
| 37 |
elif isinstance(i, Gen) and (self._iterpacket or not isinstance(i,BasePacket)): |
|---|
| 38 |
for j in i: |
|---|
| 39 |
yield j |
|---|
| 40 |
else: |
|---|
| 41 |
yield i |
|---|
| 42 |
def __repr__(self): |
|---|
| 43 |
return "<SetGen %s>" % self.set.__repr__() |
|---|
| 44 |
|
|---|
| 45 |
class Net(Gen): |
|---|
| 46 |
"""Generate a list of IPs from a network address or a name""" |
|---|
| 47 |
name = "ip" |
|---|
| 48 |
ipaddress = re.compile(r"^(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)\.(\*|[0-2]?[0-9]?[0-9](-[0-2]?[0-9]?[0-9])?)(/[0-3]?[0-9])?$") |
|---|
| 49 |
def __init__(self, net): |
|---|
| 50 |
self.repr=net |
|---|
| 51 |
|
|---|
| 52 |
tmp=net.split('/')+["32"] |
|---|
| 53 |
if not self.ipaddress.match(net): |
|---|
| 54 |
tmp[0]=socket.gethostbyname(tmp[0]) |
|---|
| 55 |
netmask = int(tmp[1]) |
|---|
| 56 |
|
|---|
| 57 |
def parse_digit(a,netmask): |
|---|
| 58 |
netmask = min(8,max(netmask,0)) |
|---|
| 59 |
if a == "*": |
|---|
| 60 |
a = (0,256) |
|---|
| 61 |
elif a.find("-") >= 0: |
|---|
| 62 |
x,y = map(int,a.split("-")) |
|---|
| 63 |
if x > y: |
|---|
| 64 |
y = x |
|---|
| 65 |
a = (x & (0xffL<<netmask) , max(y, (x | (0xffL>>(8-netmask))))+1) |
|---|
| 66 |
else: |
|---|
| 67 |
a = (int(a) & (0xffL<<netmask),(int(a) | (0xffL>>(8-netmask)))+1) |
|---|
| 68 |
return a |
|---|
| 69 |
|
|---|
| 70 |
self.parsed = map(lambda x,y: parse_digit(x,y), tmp[0].split("."), map(lambda x,nm=netmask: x-nm, (8,16,24,32))) |
|---|
| 71 |
|
|---|
| 72 |
def __iter__(self): |
|---|
| 73 |
for d in xrange(*self.parsed[3]): |
|---|
| 74 |
for c in xrange(*self.parsed[2]): |
|---|
| 75 |
for b in xrange(*self.parsed[1]): |
|---|
| 76 |
for a in xrange(*self.parsed[0]): |
|---|
| 77 |
yield "%i.%i.%i.%i" % (a,b,c,d) |
|---|
| 78 |
def choice(self): |
|---|
| 79 |
ip = [] |
|---|
| 80 |
for v in self.parsed: |
|---|
| 81 |
ip.append(str(random.randint(v[0],v[1]-1))) |
|---|
| 82 |
return ".".join(ip) |
|---|
| 83 |
|
|---|
| 84 |
def __repr__(self): |
|---|
| 85 |
return "Net(%r)" % self.repr |
|---|
| 86 |
|
|---|
| 87 |
class OID(Gen): |
|---|
| 88 |
name = "OID" |
|---|
| 89 |
def __init__(self, oid): |
|---|
| 90 |
self.oid = oid |
|---|
| 91 |
self.cmpt = [] |
|---|
| 92 |
fmt = [] |
|---|
| 93 |
for i in oid.split("."): |
|---|
| 94 |
if "-" in i: |
|---|
| 95 |
fmt.append("%i") |
|---|
| 96 |
self.cmpt.append(tuple(map(int, i.split("-")))) |
|---|
| 97 |
else: |
|---|
| 98 |
fmt.append(i) |
|---|
| 99 |
self.fmt = ".".join(fmt) |
|---|
| 100 |
def __repr__(self): |
|---|
| 101 |
return "OID(%r)" % self.oid |
|---|
| 102 |
def __iter__(self): |
|---|
| 103 |
ii = [k[0] for k in self.cmpt] |
|---|
| 104 |
while 1: |
|---|
| 105 |
yield self.fmt % tuple(ii) |
|---|
| 106 |
i = 0 |
|---|
| 107 |
while 1: |
|---|
| 108 |
if i >= len(ii): |
|---|
| 109 |
raise StopIteration |
|---|
| 110 |
if ii[i] < self.cmpt[i][1]: |
|---|
| 111 |
ii[i]+=1 |
|---|
| 112 |
break |
|---|
| 113 |
else: |
|---|
| 114 |
ii[i] = self.cmpt[i][0] |
|---|
| 115 |
i += 1 |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
class Packet_metaclass(type): |
|---|
| 124 |
def __new__(cls, name, bases, dct): |
|---|
| 125 |
newcls = super(Packet_metaclass, cls).__new__(cls, name, bases, dct) |
|---|
| 126 |
for f in newcls.fields_desc: |
|---|
| 127 |
f.register_owner(newcls) |
|---|
| 128 |
config.conf.layers.register(newcls) |
|---|
| 129 |
return newcls |
|---|
| 130 |
def __getattr__(self, attr): |
|---|
| 131 |
for k in self.fields_desc: |
|---|
| 132 |
if k.name == attr: |
|---|
| 133 |
return k |
|---|
| 134 |
raise AttributeError(attr) |
|---|
| 135 |
|
|---|
| 136 |
class NewDefaultValues(Packet_metaclass): |
|---|
| 137 |
"""NewDefaultValues metaclass. Example usage: |
|---|
| 138 |
class MyPacket(Packet): |
|---|
| 139 |
fields_desc = [ StrField("my_field", "my default value"), ] |
|---|
| 140 |
|
|---|
| 141 |
class MyPacket_variant(MyPacket): |
|---|
| 142 |
__metaclass__ = NewDefaultValues |
|---|
| 143 |
my_field = "my new default value" |
|---|
| 144 |
""" |
|---|
| 145 |
def __new__(cls, name, bases, dct): |
|---|
| 146 |
fields = None |
|---|
| 147 |
for b in bases: |
|---|
| 148 |
if hasattr(b,"fields_desc"): |
|---|
| 149 |
fields = b.fields_desc |
|---|
| 150 |
break |
|---|
| 151 |
if fields is None: |
|---|
| 152 |
raise error.Scapy_Exception("No fields_desc in superclasses") |
|---|
| 153 |
|
|---|
| 154 |
new_fields = [] |
|---|
| 155 |
for f in fields: |
|---|
| 156 |
if f.name in dct: |
|---|
| 157 |
f = f.copy() |
|---|
| 158 |
f.default = dct[f.name] |
|---|
| 159 |
del(dct[f.name]) |
|---|
| 160 |
new_fields.append(f) |
|---|
| 161 |
dct["fields_desc"] = new_fields |
|---|
| 162 |
return super(NewDefaultValues, cls).__new__(cls, name, bases, dct) |
|---|
| 163 |
|
|---|
| 164 |
class BasePacket(Gen): |
|---|
| 165 |
pass |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
class BasePacketList: |
|---|
| 173 |
pass |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|