summaryrefslogtreecommitdiff
path: root/common/lib/scammer/commonfields.py
blob: 82f4eaad143089bb2d37b958f897036e2da30fc4 (plain)
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
104
105
106
107
#!/usr/bin/python

#############################################################################
#  Copyright (C) 2011 Spidcom
#############################################################################

from scapy.fields import *

class XLEShortField (LEShortField):
    """This class is used to represent LEShortField (scapy) in Hexadecimal
    representation, this functionality should exist in the future version of
    scapy called 'XLEShortField' """
    def i2repr(self, pkt, x):
        if x is None:
            x = 0
        return lhex(self.i2h(pkt, x))

class LEBitField (BitField):
    def __init__(self, name, default, size):
        BitField.__init__(self, name, default, size)

    def addfield (self, pkt, s, val):
        val = self.i2m(pkt, val)
        if type(s) is tuple:
            s,bitsdone,v = s
        else:
            bitsdone = 0
            v = 0
        if self.rev:
            val = self.reverse(val)
        v |= val << bitsdone
        bitsdone += self.size
        while bitsdone >= 8:
            bitsdone -= 8
            s = s+struct.pack("B", v >> bitsdone)
            v &= (1L<<bitsdone)-1
        if bitsdone:
            return s,bitsdone,v
        else:
            return s

    def getfield(self, pkt, s):
        if type(s) is tuple:
            s,bn = s
        else:
            bn = 0
        # we don't want to process all the string
        nb_bytes = (self.size+bn-1)/8 + 1
        w = s[:nb_bytes]

        # split the substring byte by byte
        bytes = struct.unpack('%dB' % nb_bytes , w)

        b = 0L
        for c in range(nb_bytes):
            b |= long(bytes[nb_bytes - 1 - c]) << (nb_bytes-c-1)*8

        # Decal of bn.
        b = b >> bn
        # get rid of high order bits
        b &= (1L << (self.size)) - 1

        if self.rev:
            b = self.reverse(b)

        bn += self.size
        s = s[bn/8:]
        if bn % 8 != 0:
            sbyte = struct.unpack ('B', s[0])[0]
            decal_bits = (8 - (nb_bytes * 8 - self.size))
            sbyte &= ~((1 << decal_bits) - 1)
            sbyte = struct.pack ('B', sbyte)
            s = sbyte + s[1:]

        bn = bn%8
        b = self.m2i(pkt, b)
        if bn:
            return (s,bn),b
        else:
            return s,b

class XLEBitField (LEBitField):
    """This class is used to represent LEShortField (scapy) in Hexadecimal
    representation, this functionality should exist in the future version of
    scapy called 'XLEShortField' """
    def i2repr(self, pkt, x):
        if x is None:
            x = 0
        return lhex(self.i2h(pkt, x))

class HPAVNIDField (Field):
    """Special Field for NID HPAV field."""
    def __init__(self, name, default):
        Field.__init__(self, name, default, "7s")

    def addfield (self, pkt, s, val):
        nb_bytes = 7
        w = struct.pack ('Q', val)
        w = w[:nb_bytes]
        return s + w

    def getfield (self, pkt, s):
        nb_bytes = 7
        w = "%s\0" % s[0:nb_bytes]
        w = struct.unpack ('Q', w)[0]
        s = s[nb_bytes:]
        return s,w