Example #1
0
def preparePacket(xid=None,
                  giaddr='0.0.0.0',
                  chaddr='00:00:00:00:00:00',
                  ciaddr='0.0.0.0',
                  msgtype='discover',
                  required_opts=[]):
    req = DhcpPacket()
    req.SetOption('op', [1])
    req.SetOption('htype', [1])
    req.SetOption('hlen', [6])
    req.SetOption('hops', [0])
    if not xid: xid = genxid()
    req.SetOption('xid', xid)
    req.SetOption('giaddr', ipv4(giaddr).list())
    req.SetOption('chaddr', hwmac(chaddr).list() + [0] * 10)
    req.SetOption('ciaddr', ipv4(ciaddr).list())
    if msgtype == 'request':
        mt = 3
    elif msgtype == 'release':
        mt = 7
    else:
        mt = 1
    req.SetOption('dhcp_message_type', [mt])
    #       req.SetOption('parameter_request_list',1)
    return req
Example #2
0
    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
Example #3
0
 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
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
Example #6
0
def preparePacket(xid=None,giaddr='0.0.0.0',chaddr='00:00:00:00:00:00',ciaddr='0.0.0.0',msgtype='discover',required_opts=[]):
        req = DhcpPacket()
        req.SetOption('op',[1])
        req.SetOption('htype',[1])
        req.SetOption('hlen',[6])
        req.SetOption('hops',[0])
        if not xid: xid = genxid()
        req.SetOption('xid',xid)
        req.SetOption('giaddr',ipv4(giaddr).list())
        req.SetOption('chaddr',hwmac(chaddr).list() + [0] * 10)
        req.SetOption('ciaddr',ipv4(ciaddr).list())
        if msgtype == 'request':
                mt = 3
        elif msgtype == 'release':
                mt = 7
        else:
                mt = 1
        req.SetOption('dhcp_message_type',[mt])
#       req.SetOption('parameter_request_list',1)
        return req
Example #7
0
def preparePacket(xid=None,giaddr='0.0.0.0',chaddr='00:00:00:00:00:00',ciaddr='0.0.0.0', yiaddr='0.0.0.0', msgtype='discover'):
        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(giaddr).list())
        req.SetOption('chaddr',hwmac(chaddr).list() + [0] * 10)
        req.SetOption('ciaddr',ipv4(ciaddr).list())
        if msgtype == 'request':
                mt = 3
        elif msgtype == 'release':
                mt = 7
        else:
                mt = 1
        if mt == 3:
                req.SetOption('yiaddr', ipv4(yiaddr).list())
                req.SetOption('request_ip_address', ipv4(yiaddr).list())
        req.SetOption('dhcp_message_type',[mt])
        return req
Example #8
0
def main():
        parser =  OptionParser()
        parser.add_option("-s","--server", dest="server", default='0.0.0.0', help="DHCP server IP (default %default)")
        parser.add_option("-p","--port", type="int", dest="port", default=67, help="DHCP server port (default (%default)")
        parser.add_option("-m","--mac","--chaddr", dest="chaddr", help="chaddr: Client's MAC address, default random")
        parser.add_option("-c","--ciaddr", dest="ciaddr", default='0.0.0.0', help="ciaddr: Client's desired IP address")
        parser.add_option("-g","--giaddr", dest="giaddr", default='0.0.0.0', help="giaddr: Gateway IP address (if any)")
        parser.add_option("-t","--type", dest="msgtype", type="choice", choices=["discover","request","release"],
                        default="discover", help="DHCP message type: discover, request, release (default %default)")
        parser.add_option("-w","--timeout", dest="timeout", type="int", default=4, help="UDP timeout (default %default)")
        parser.add_option("-r","--require", action="append", type="int", default=[1,3,6,51], dest="required_opts", help="Require options by its number")
        parser.add_option("-y","--cycle", action="store_true", dest="docycle", help="Do full cycle: DISCOVERY, REQUEST, RELEASE")
        parser.add_option("-n","--cycles", dest="cycles", type="int", default="1", help="Do number of cycles (default %default)")
        parser.add_option("-v","--verbose", action="store_true", dest="verbose", help="Verbose operation")
        parser.add_option("-q","--quiet", action="store_false", dest="verbose", help="Quiet operation")
        parser.add_option("--nagios", action="store_true", dest="nagios", help="Nagios mode of operation")
        
        (opts, args) = parser.parse_args()

        if not opts.chaddr:
                chaddr = genmac()
        else:
                chaddr = opts.chaddr
        
        if opts.nagios: opts.verbose = False
        verbose = opts.verbose

        if opts.docycle:
                request_dhcp_message_type = "discover"
        else:
                request_dhcp_message_type = opts.msgtype

        request_ciaddr = opts.ciaddr 
        serverip = opts.server 
        cycleno = 1
        xid = genxid()
        
        while True:
                
                if opts.cycles > 1 and opts.verbose is not False and (not opts.docycle or request_dhcp_message_type == "discover"):
                        print "="*100
                        print "| Cycle %s"%cycleno
                        print "="*100

                req = preparePacket(xid=xid, giaddr=opts.giaddr, chaddr=chaddr, ciaddr=request_ciaddr, msgtype=request_dhcp_message_type, required_opts=opts.required_opts)
                if verbose != False:
                        print "Sending %s [%s] packet to %s"%(request_dhcp_message_type.upper(),chaddr, opts.server)
                if verbose == True:
                        print "-"*100
                        #req.PrintHeaders()
                        #req.PrintOptions()
                        print "="*100
                        print "\n"
                
                try:
                        res = receivePacket(serverip=serverip, serverport=opts.port, timeout=opts.timeout, req=req)
                except socket.timeout:
                        res = None
                        if opts.nagios: nagiosExit(2,"%s request has been timed out."%request_dhcp_message_type.upper())
                        if verbose != False: print "Timed out."
                        pass
        
                if res:
                        dhcp_message_type = res.GetOption('dhcp_message_type')[0]
                        server_identifier = ipv4(res.GetOption('server_identifier'))
                        chaddr = hwmac(res.GetOption('chaddr')[:6])
                        yiaddr = ipv4(res.GetOption('yiaddr'))
                        
                        if opts.nagios and dhcp_message_type not in (2, 5):
                                nagiosExit(2,"Got %s response for our %s request"%(dhcpTypes.get(dhcp_message_type,'UNKNOWN'),dhcpTypes.get(request_dhcp_message_type,'UNKNOWN')))
        
                        if verbose != False:
                                print "Received %s packet from %s; [%s] was bound to %s"%(dhcpTypes.get(dhcp_message_type,'UNKNOWN'), server_identifier, chaddr, yiaddr )
                        if verbose == True:
                                print "-"*100
                                res.PrintHeaders()
                                res.PrintOptions()
                                print "="*100
                                print "\n"

                        if opts.docycle:
                                if dhcp_message_type == 2:
                                        request_dhcp_message_type = 'request'
                                        request_ciaddr = yiaddr.str()
                                        serverip = server_identifier.str()
                                        continue
                                
                                if dhcp_message_type == 5:
                                        request_dhcp_message_type = 'release'
                                        request_ciaddr = yiaddr.str()
                                        serverip = server_identifier.str()
                                        continue
                
                cycleno += 1
                if cycleno > opts.cycles:
                        if opts.nagios:
                                if res:
                                        nagiosExit(0,"%s finished successfully: %s. yiaddr: %s, chaddr: %s"%(
                                                request_dhcp_message_type.upper(),
                                                dhcpTypes.get(dhcp_message_type,'UNKNOWN'),
                                                yiaddr,
                                                chaddr,
                                        ))
                                elif opts.docycle:
                                        nagiosExit(0,"Cycle has been finished successfully. Got %s for %s"%(yiaddr,chaddr))
                                else:
                                        nagiosExit(0,"%s has been finished without the answer"%(request_dhcp_message_type.upper()))
                        break

                if opts.docycle:
                        request_dhcp_message_type = "discover"
                        request_ciaddr = opts.ciaddr 
                        serverip = opts.server 
                xid = genxid()
                if not opts.chaddr:
                        chaddr = genmac()
                else:
                        chaddr = opts.chaddr
Example #9
0
def main():
    parser = OptionParser()
    parser.add_option("-s",
                      "--server",
                      dest="server",
                      default='0.0.0.0',
                      help="DHCP server IP (default %default)")
    parser.add_option("-p",
                      "--port",
                      type="int",
                      dest="port",
                      default=67,
                      help="DHCP server port (default (%default)")
    parser.add_option("-m",
                      "--mac",
                      "--chaddr",
                      dest="chaddr",
                      help="chaddr: Client's MAC address, default random")
    parser.add_option("-c",
                      "--ciaddr",
                      dest="ciaddr",
                      default='0.0.0.0',
                      help="ciaddr: Client's desired IP address")
    parser.add_option("-g",
                      "--giaddr",
                      dest="giaddr",
                      default='0.0.0.0',
                      help="giaddr: Gateway IP address (if any)")
    parser.add_option(
        "-t",
        "--type",
        dest="msgtype",
        type="choice",
        choices=["discover", "request", "release"],
        default="discover",
        help="DHCP message type: discover, request, release (default %default)"
    )
    parser.add_option("-w",
                      "--timeout",
                      dest="timeout",
                      type="int",
                      default=4,
                      help="UDP timeout (default %default)")
    parser.add_option("-r",
                      "--require",
                      action="append",
                      type="int",
                      default=[1, 3, 6, 51],
                      dest="required_opts",
                      help="Require options by its number")
    parser.add_option("-y",
                      "--cycle",
                      action="store_true",
                      dest="docycle",
                      help="Do full cycle: DISCOVERY, REQUEST, RELEASE")
    parser.add_option("-n",
                      "--cycles",
                      dest="cycles",
                      type="int",
                      default="1",
                      help="Do number of cycles (default %default)")
    parser.add_option("-v",
                      "--verbose",
                      action="store_true",
                      dest="verbose",
                      help="Verbose operation")
    parser.add_option("-q",
                      "--quiet",
                      action="store_false",
                      dest="verbose",
                      help="Quiet operation")
    parser.add_option("--nagios",
                      action="store_true",
                      dest="nagios",
                      help="Nagios mode of operation")

    (opts, args) = parser.parse_args()

    if not opts.chaddr:
        chaddr = genmac()
    else:
        chaddr = opts.chaddr

    if opts.nagios: opts.verbose = False
    verbose = opts.verbose

    if opts.docycle:
        request_dhcp_message_type = "discover"
    else:
        request_dhcp_message_type = opts.msgtype

    request_ciaddr = opts.ciaddr
    serverip = opts.server
    cycleno = 1
    xid = genxid()

    while True:

        if opts.cycles > 1 and opts.verbose is not False and (
                not opts.docycle or request_dhcp_message_type == "discover"):
            print "=" * 100
            print "| Cycle %s" % cycleno
            print "=" * 100

        req = preparePacket(xid=xid,
                            giaddr=opts.giaddr,
                            chaddr=chaddr,
                            ciaddr=request_ciaddr,
                            msgtype=request_dhcp_message_type,
                            required_opts=opts.required_opts)
        if verbose != False:
            print "Sending %s [%s] packet to %s" % (
                request_dhcp_message_type.upper(), chaddr, opts.server)
        if verbose == True:
            print "-" * 100
            #req.PrintHeaders()
            #req.PrintOptions()
            print "=" * 100
            print "\n"

        try:
            res = receivePacket(serverip=serverip,
                                serverport=opts.port,
                                timeout=opts.timeout,
                                req=req)
        except socket.timeout:
            res = None
            if opts.nagios:
                nagiosExit(
                    2, "%s request has been timed out." %
                    request_dhcp_message_type.upper())
            if verbose != False: print "Timed out."
            pass

        if res:
            dhcp_message_type = res.GetOption('dhcp_message_type')[0]
            server_identifier = ipv4(res.GetOption('server_identifier'))
            chaddr = hwmac(res.GetOption('chaddr')[:6])
            yiaddr = ipv4(res.GetOption('yiaddr'))

            if opts.nagios and dhcp_message_type not in (2, 5):
                nagiosExit(
                    2, "Got %s response for our %s request" %
                    (dhcpTypes.get(dhcp_message_type, 'UNKNOWN'),
                     dhcpTypes.get(request_dhcp_message_type, 'UNKNOWN')))

            if verbose != False:
                print "Received %s packet from %s; [%s] was bound to %s" % (
                    dhcpTypes.get(dhcp_message_type, 'UNKNOWN'),
                    server_identifier, chaddr, yiaddr)
            if verbose == True:
                print "-" * 100
                res.PrintHeaders()
                res.PrintOptions()
                print "=" * 100
                print "\n"

            if opts.docycle:
                if dhcp_message_type == 2:
                    request_dhcp_message_type = 'request'
                    request_ciaddr = yiaddr.str()
                    serverip = server_identifier.str()
                    continue

                if dhcp_message_type == 5:
                    request_dhcp_message_type = 'release'
                    request_ciaddr = yiaddr.str()
                    serverip = server_identifier.str()
                    continue

        cycleno += 1
        if cycleno > opts.cycles:
            if opts.nagios:
                if res:
                    nagiosExit(
                        0,
                        "%s finished successfully: %s. yiaddr: %s, chaddr: %s"
                        % (
                            request_dhcp_message_type.upper(),
                            dhcpTypes.get(dhcp_message_type, 'UNKNOWN'),
                            yiaddr,
                            chaddr,
                        ))
                elif opts.docycle:
                    nagiosExit(
                        0,
                        "Cycle has been finished successfully. Got %s for %s" %
                        (yiaddr, chaddr))
                else:
                    nagiosExit(
                        0, "%s has been finished without the answer" %
                        (request_dhcp_message_type.upper()))
            break

        if opts.docycle:
            request_dhcp_message_type = "discover"
            request_ciaddr = opts.ciaddr
            serverip = opts.server
        xid = genxid()
        if not opts.chaddr:
            chaddr = genmac()
        else:
            chaddr = opts.chaddr
Example #10
0
#!/usr/bin/python

from pydhcplib.type_hw_addr import hwmac


address = hwmac()
print("a0 : ",address)

address1 = hwmac("ff:11:22:33:44:55")
print("a1 : ",address1)

address2 = hwmac("f6.16.26.36.46.56")
print("a2 : ",address2)

address3 = hwmac("ff.11-22:33-44.55")
print("a3 : ",address3)

if address1 == address2:
    print("test 1 : ", address1, "==", address2)
else:
    print("test 1 : " , address1, "!=", address2)

if address1 == address3 :
    print("test 2 : ", address1, "==", address3)
else:
    print("test 2 : ", address1, "!=", address3)

address4 = hwmac([186, 45, 67, 176, 6, 11])
address5 = hwmac("ba:2d:43:b0:06:0c")
    
print("b0 : ", address4,address4.list())
Example #11
0
#!/usr/bin/python

from pydhcplib.type_hw_addr import hwmac

address = hwmac()
print("a0 : ", address)

address1 = hwmac("ff:11:22:33:44:55")
print("a1 : ", address1)

address2 = hwmac("f6.16.26.36.46.56")
print("a2 : ", address2)

address3 = hwmac("ff.11-22:33-44.55")
print("a3 : ", address3)

if address1 == address2: print("test 1 : ", address1, "==", address2)
else: print("test 1 : ", address1, "!=", address2)

if address1 == address3: print("test 2 : ", address1, "==", address3)
else: print("test 2 : ", address1, "!=", address3)

address4 = hwmac([186, 45, 67, 176, 6, 11])
address5 = hwmac("ba:2d:43:b0:06:0c")

print("b0 : ", address4, address4.list())
print("b1 : ", address5, address5.list())
Example #12
0
 def HandleDhcpDiscover(self, packet):
     print 'discover'
     print strlist(packet.GetOption('host_name'))
     print hwmac(packet.GetHardwareAddress())