#!/usr/bin/env python import struct import socket class DHCPPack: fmt = "BBBBIHH4B4B4B4B8H64s128s" def __init__(self,data=None): if data is not None: info = struct.unpack(DHCPPack.fmt, data) self.op = info[0] self.htype = info[1] self.hlen = info[2] self.hops = info[3] self.xid = info[4] self.secs = info[5] self.flags = info[6] self.ciaddr = tuple(info[7:10]) self.yiaddr = tuple(info[11:14]) self.siaddr = tuple(info[15:18]) self.giaddr = tuple(info[19:22]) self.chaddr = tuple(info[23:30]) self.sname = info[31] self.file = info[32] else: self.op = 1 self.htype = 1 self.hlen = 6 self.hops = 0 self.xid = 0 self.secs = 0 self.flags = 0 self.ciaddr = (0,0,0,0) self.yiaddr = (0,0,0,0) self.siaddr = (0,0,0,0) self.giaddr = (0,0,0,0) self.chaddr = (0xDE,0xAD,0xBE,0xEF,0x13,0x37,0x13,0x37) self.sname = "" self.file = "" def pack(self): return struct.unpack(DHCPPack.fmt, self.op, self.htype, self.hlen, self.hops, self.xid, self.secs, self.flags, self.ciaddr[0], self.ciaddr[1], self.ciaddr[2], self.ciaddr[3], self.yiaddr[0], self.yiaddr[1], self.yiaddr[2], self.yiaddr[3], self.siaddr[0], self.siaddr[1], self.siaddr[2], self.siaddr[3], self.giaddr[0], self.giaddr[1], self.giaddr[2], self.giaddr[3], self.chaddr[0], self.chaddr[1], self.chaddr[2], self.chaddr[3], self.chaddr[4], self.chaddr[5], self.chaddr[6], self.chaddr[7], self.sname, self.file) def main(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('',68)) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) packet = DHCPPack(); s.sendto(packet.pack(), ('',67)) data, addr = sock.recvfrom(1024) packet2 = DHCPPack(data) if packet2.op == 2: print("Got answer") else: print("WTF") main()