Example #1
0
def strToList(s):
    """
    Converts the given string into an encoded byte format.
    
    @type s: basestring
    @param s: The string to be converted.
    
    @rtype: list
    @return: An encoded byte version of the given string.
    """
    return type_strlist.strlist(str(s)).list()
Example #2
0
    def PrintHeaders(self):
        print "# Header fields\n"
        print "readable_dhcp_headers = {"
        for opt in  ['op','htype','hlen','hops','xid','secs','flags',
                     'ciaddr','yiaddr','siaddr','giaddr','chaddr','sname','file'] :
            begin = DhcpFields[opt][0]
            end = DhcpFields[opt][0]+DhcpFields[opt][1]
            data = self.packet_data[begin:end]
            if DhcpFieldsTypes[opt] == "int" : result = str(data[0])
            if DhcpFieldsTypes[opt] == "int2" : result = str(data[0]*256+data[0])
            if DhcpFieldsTypes[opt] == "int4" : result = str(ipv4(data).int())
            if DhcpFieldsTypes[opt] == "str" : result = strlist(data).str()
            if DhcpFieldsTypes[opt] == "ipv4" : result = ipv4(data).str()
            if DhcpFieldsTypes[opt] == "hwmac" : result = "".join(map(chr,data))

            line = "\t'"+opt+"':"+str(data)+",\t# "+result
            print line
        print "\t'end':'true'}"
Example #3
0
    def PrintHeaders(self):
        print "# Header fields\n"
        print "readable_dhcp_headers = {"
        for opt in  ['op','htype','hlen','hops','xid','secs','flags',
                     'ciaddr','yiaddr','siaddr','giaddr','chaddr','sname','file'] :
            begin = DhcpFields[opt][0]
            end = DhcpFields[opt][0]+DhcpFields[opt][1]
            data = self.packet_data[begin:end]
            if DhcpFieldsTypes[opt] == "int" : result = str(data[0])
            if DhcpFieldsTypes[opt] == "int2" : result = str(data[0]*256+data[0])
            if DhcpFieldsTypes[opt] == "int4" : result = str(ipv4(data).int())
            if DhcpFieldsTypes[opt] == "str" : result = strlist(data).str()
            if DhcpFieldsTypes[opt] == "ipv4" : result = ipv4(data).str()
            if DhcpFieldsTypes[opt] == "hwmac" : result = "".join(map(chr,data))

            line = "\t'"+opt+"':"+str(data)+",\t# "+result
            print line
        print "\t'end':'true'}"
Example #4
0
 def PrintOptions(self):
     print "# Options fields"
     print "readable_dhcp_options = {"
     for opt in self.options_data.keys():
         data = self.options_data[opt]
         result = ""
         optnum  = DhcpOptions[opt]
         if DhcpOptionsTypes[optnum] == "char" : result = str(data[0])
         if DhcpOptionsTypes[optnum] == "16-bits" : result = str(data[0]*256+data[0])
         if DhcpOptionsTypes[optnum] == "32bits" : result = str(ipv4(data).int())
         if DhcpOptionsTypes[optnum] == "string" : result = strlist(data).str()
         if DhcpOptionsTypes[optnum] == "ipv4" : result = ipv4(data).str()
         if DhcpOptionsTypes[optnum] == "ipv4+" :
             for i in range(0,len(data),4) :
                 if len(data[i:i+4]) == 4 :
                     result += ipv4(data[i:i+4]).str() + " - "
         line = "\t'"+opt+"':"+str(data)+",\t# "+result
         print line
     print "\t'end':'true'}"
Example #5
0
 def PrintOptions(self):
     print "# Options fields"
     print "readable_dhcp_options = {"
     for opt in self.options_data.keys():
         data = self.options_data[opt]
         result = ""
         optnum  = DhcpOptions[opt]
         if DhcpOptionsTypes[optnum] == "char" : result = str(data[0])
         if DhcpOptionsTypes[optnum] == "16-bits" : result = str(data[0]*256+data[0])
         if DhcpOptionsTypes[optnum] == "32bits" : result = str(ipv4(data).int())
         if DhcpOptionsTypes[optnum] == "string" : result = strlist(data).str()
         if DhcpOptionsTypes[optnum] == "ipv4" : result = ipv4(data).str()
         if DhcpOptionsTypes[optnum] == "ipv4+" :
             for i in range(0,len(data),4) :
                 if len(data[i:i+4]) == 4 :
                     result += ipv4(data[i:i+4]).str() + " - "
         line = "\t'"+opt+"':"+str(data)+",\t# "+result
         print line
     print "\t'end':'true'}"
Example #6
0
    def PrintHeaders(self):
        print "# Header fields\n"
        print "readable_dhcp_headers = {"
        for opt in [
            "op",
            "htype",
            "hlen",
            "hops",
            "xid",
            "secs",
            "flags",
            "ciaddr",
            "yiaddr",
            "siaddr",
            "giaddr",
            "chaddr",
            "sname",
            "file",
        ]:
            begin = DhcpFields[opt][0]
            end = DhcpFields[opt][0] + DhcpFields[opt][1]
            data = self.packet_data[begin:end]
            if DhcpFieldsTypes[opt] == "int":
                result = str(data[0])
            if DhcpFieldsTypes[opt] == "int2":
                result = str(data[0] * 256 + data[0])
            if DhcpFieldsTypes[opt] == "int4":
                result = str(ipv4(data).int())
            if DhcpFieldsTypes[opt] == "str":
                result = strlist(data).str()
            if DhcpFieldsTypes[opt] == "ipv4":
                result = ipv4(data).str()
            if DhcpFieldsTypes[opt] == "hwmac":
                result = "".join(map(chr, data))

            line = "\t'" + opt + "':" + str(data) + ",\t# " + result
            print line
        print "\t'end':'true'}"
Example #7
0
            except ValueError : return False


        elif option_type == "char" :
            try :
                digit = int(value)
                binary_value = [digit&0xFF]
            except ValueError : return False

        elif option_type == "bool" :
            if value=="False" or value=="false" or value==0 :
                binary_value = [0]
            else : binary_value = [1]
            
        elif option_type == "string" :
            binary_value = strlist(value).list()

        else :
            binary_value = strlist(value).list()
        
        return binary_value
    
    # FIXME: This is called from IsDhcpSomethingPacket, but is this really
    # needed?  Or maybe this testing should be done in
    # DhcpBasicPacket.DecodePacket().

    # Test Packet Type
    def IsDhcpSomethingPacket(self,type):
        if self.IsDhcpPacket() == False : return False
        if self.IsOption("dhcp_message_type") == False : return False
        if self.GetOption("dhcp_message_type") != type : return False
Example #8
0
    def _OptionsToBinary(self, parameter, value):
        # Transform textual data into dhcp binary data

        p = parameter.strip()
        # 1- Search for header informations or specific parameter
        if p == 'op' or p == 'htype':
            value = value.strip()
            if value.isdigit(): return [int(value)]
            try:
                value = DhcpNames[value.strip()]
                return [value]
            except KeyError:
                return [0]

        elif p == 'hlen' or p == 'hops':
            try:
                value = int(value)
                return [value]
            except ValueError:
                return [0]

        elif p == 'secs' or p == 'flags':
            try:
                value = ipv4(int(value)).list()
            except ValueError:
                value = [0, 0, 0, 0]

            return value[2:]

        elif p == 'xid':
            try:
                value = ipv4(int(value)).list()
            except ValueError:
                value = [0, 0, 0, 0]
            return value

        elif p == 'ciaddr' or p == 'yiaddr' or p == 'siaddr' or p == 'giaddr':
            try:
                ip = ipv4(value).list()
            except ValueError:
                ip = [0, 0, 0, 0]
            return ip

        elif p == 'chaddr':
            try:
                value = hwmac(value).list() + [0] * 10
            except (ValueError, TypeError):
                value = [0] * 16
            return value

        elif p == 'sname':
            return
        elif p == 'file':
            return
        elif p == 'parameter_request_list':
            value = value.strip().split(',')
            tmp = []
            for each in value:
                if DhcpOptions.has_key(each): tmp.append(DhcpOptions[each])
            return tmp
        elif p == 'dhcp_message_type':
            try:
                return [DhcpNames[value]]
            except KeyError:
                return

        # 2- Search for options
        try:
            option_type = DhcpOptionsTypes[DhcpOptions[parameter]]
        except KeyError:
            return False

        if option_type == "ipv4":
            # this is a single ip address
            try:
                binary_value = map(int, value.split("."))
            except ValueError:
                return False

        elif option_type == "ipv4+":
            # this is multiple ip address
            iplist = value.split(",")
            opt = []
            for single in iplist:
                opt += (ipv4(single).list())
            binary_value = opt

        elif option_type == "32-bits":
            # This is probably a number...
            try:
                digit = int(value)
                binary_value = [
                    digit >> 24 & 0xFF, (digit >> 16) & 0xFF,
                    (digit >> 8) & 0xFF, digit & 0xFF
                ]
            except ValueError:
                return False

        elif option_type == "16-bits":
            try:
                digit = int(value)
                binary_value = [(digit >> 8) & 0xFF, digit & 0xFF]
            except ValueError:
                return False

        elif option_type == "char":
            try:
                digit = int(value)
                binary_value = [digit & 0xFF]
            except ValueError:
                return False

        elif option_type == "bool":
            if value == "False" or value == "false" or value == 0:
                binary_value = [0]
            else:
                binary_value = [1]

        elif option_type == "string":
            binary_value = strlist(value).list()

        else:
            binary_value = strlist(value).list()

        return binary_value
Example #9
0
        elif option_type == "char":
            try:
                digit = int(value)
                binary_value = [digit & 0xFF]
            except ValueError:
                return False

        elif option_type == "bool":
            if value == "False" or value == "false" or value == 0:
                binary_value = [0]
            else:
                binary_value = [1]

        elif option_type == "string":
            binary_value = strlist(value).list()

        else:
            binary_value = strlist(value).list()

        return binary_value

    # FIXME: This is called from IsDhcpSomethingPacket, but is this really
    # needed?  Or maybe this testing should be done in
    # DhcpBasicPacket.DecodePacket().

    # Test Packet Type
    def IsDhcpSomethingPacket(self, type):
        if self.IsDhcpPacket() == False: return False
        if self.IsOption("dhcp_message_type") == False: return False
        if self.GetOption("dhcp_message_type") != type: return False