def __init__(self): DhcpPacket.__init__(self) self.SetOption("htype",[1]) self.SetOption("hlen",[6]) self.SetOption("xid",[0x34,0x87,0x11,0x31]) self.SetOption("host_name", strlist("xyang-test").list())
def create_packet(self, mt, chaddr, ciaddr, yiaddr, xid, server): req = DhcpPacket() req.SetOption('op', [1]) req.SetOption('htype',[1]) req.SetOption('hlen',[6]) req.SetOption('hops', [0]) req.SetOption('xid', xid) req.SetOption('giaddr', ipv4().list()) req.SetOption('chaddr', hwmac(chaddr).list() + [0] * 10) req.SetOption('ciaddr', ipv4(ciaddr).list()) if mt == DHCPRequest: req.SetOption('yiaddr', ipv4(yiaddr).list()) req.SetOption('request_ip_address', ipv4(yiaddr).list()) if mt == DHCPRelease: req.SetOption('siaddr', server) req.SetOption('server_identifier', ipv4(server).list()) if server == '255.255.255.255': req.SetOption('flags', [128, 0]) req.SetOption('dhcp_message_type',[mt]) req.SetOption("domain_name", strlist("catdamnit.com").list()) return req
def HandleDhcpRequest(self, packet): print 'New Request: ' + ipv4( packet.GetOption('request_ip_address')).str() print strlist(packet.GetOption('host_name')) print hwmac(packet.GetHardwareAddress()) print ipv4(packet.GetOption('request_ip_address')) time = strftime("%H:%M:%S", gmtime()) date = strftime("%Y-%m-%d", gmtime()) cur.execute("INSERT INTO data(name,mac,ip,date,time) VALUES ('" + strlist(packet.GetOption('host_name')).str() + "','" + hwmac(packet.GetHardwareAddress()).str() + "','" + ipv4(packet.GetOption('request_ip_address')).str() + "','" + date + "','" + time + "');") print "INSERT INTO data(name,mac,ip,date,time) VALUES ('" + strlist( packet.GetOption('host_name')).str() + "','" + hwmac( packet.GetHardwareAddress()).str() + "','" + ipv4( packet.GetOption('request_ip_address')).str( ) + "','" + date + "','" + time + "');" conn.commit()
def prepare_packet(giaddr='0.0.0.0', chaddr='00:00:00:00:00:00', ciaddr='0.0.0.0', hostname=None): req = DhcpPacket() req.SetOption('op', [1]) req.SetOption('htype', [1]) req.SetOption('hlen', [6]) req.SetOption('hops', [0]) req.SetOption('xid', genxid()) req.SetOption('giaddr', ipv4(giaddr).list()) req.SetOption('chaddr', hwmac(chaddr).list() + [0] * 10) req.SetOption('ciaddr', ipv4('0.0.0.0').list()) req.SetOption('dhcp_message_type', [3]) # request req.SetOption('request_ip_address', ipv4(ciaddr).list()) if hostname: req.SetOption('host_name', strlist(hostname).list()) return req
#!/usr/bin/python from pydhcplib.dhcp_packet import DhcpPacket from pydhcplib.type_strlist import strlist from pydhcplib.type_ipv4 import ipv4 packet = DhcpPacket() packet.SetOption("domain_name", strlist("anemon.org").list()) packet.SetOption("router", ipv4("192.168.0.1").list() + [6, 4, 2, 1]) packet.SetOption("time_server", [100, 100, 100, 7, 6, 4, 2, 1]) packet.SetOption("yiaddr", [192, 168, 0, 18]) packet.PrintHeaders() packet.PrintOptions()
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 each in DhcpOptions: 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
#!/usr/bin/python from pydhcplib.type_strlist import strlist word = strlist() print("a0 : ", word) word1 = strlist("azerty") print("a1 : ", word1) word2 = strlist("qwerty") print("a2 : ", word2) word3 = strlist([97, 122, 101, 114, 116, 121]) print("a3 : ", word3) if word1 == word2: print("test 1 : ", word1, "==", word2) else: print("test 1 : ", word1, "!=", word2) if word1 == word3: print("test 2 : ", word1, "==", word3) else: print("test 2 : ", word1, "!=", word3)
def parse_ip_or_str(value): m = IP_RE.match(value) if m: return list(map(int, m.groups())) return strlist(value).list()
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 each in DhcpOptions: 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
def HandleDhcpDiscover(self, packet): print 'discover' print strlist(packet.GetOption('host_name')) print hwmac(packet.GetHardwareAddress())
#!/usr/bin/python from pydhcplib.type_strlist import strlist word = strlist() print "a0 : ",word word1 = strlist("azerty") print "a1 : ",word1 word2 = strlist("qwerty") print "a2 : ",word2 word3 = strlist([97, 122, 101, 114, 116, 121]) print "a3 : ",word3 if word1 == word2 : print "test 1 : ",word1, "==",word2 else : print "test 1 : " ,word1, "!=",word2 if word1 == word3 : print "test 2 : ", word1, "==",word3 else : print "test 2 : ", word1, "!=",word3
#!/usr/bin/python from pydhcplib.dhcp_packet import DhcpPacket from pydhcplib.type_strlist import strlist from pydhcplib.type_ipv4 import ipv4 packet = DhcpPacket() packet.SetOption("domain_name",strlist("anemon.org").list()) packet.SetOption("router",ipv4("192.168.0.1").list()+[6,4,2,1]) packet.SetOption("time_server",[100,100,100,7,6,4,2,1]) packet.SetOption("yiaddr",[192,168,0,18]) packet.PrintHeaders() packet.PrintOptions()
def convert_opt82_from_str(Agent_Circuit_ID, Agent_Remote_ID=''): return [1, len(Agent_Circuit_ID)] + strlist(Agent_Circuit_ID).list() + [2, 1, 1]