is there a way how to query a specific DHCP server for an IP address? Something similar to
dig @_IP_ADDRESS_ _server_name
dig @8.8.8.8 apple.com
Thanks
is there a way how to query a specific DHCP server for an IP address? Something similar to
dig @_IP_ADDRESS_ _server_name
dig @8.8.8.8 apple.com
Thanks
As long as you're on the same subnet as the DHCP server, you can use scapy to send a DHCP request and get the response.
For example, I've adapted it into the follow which shows me all DHCP servers on the network and what range they're serving:
[michael:~/prog/util]$ sudo ./findDhcpServers.py
Begin emission:
Finished to send 1 packets.
*................................
Received 33 packets, got 1 answers, remaining 0 packets
DHCP offers received:
MAC: 00:1b:64:33:df:29, Server IP: 192.168.0.6, Offer IP: 192.168.0.135
Mask: 255.255.255.0, Router: 192.168.0.1, Domain: office.myworkplace.ca
One of these methods is probably what you're after.
Teh codez:
#!/usr/bin/python
# Michael Brown <michael@supermathie.net>
# idea stolen from http://bb.secdev.org/scapy/wiki/doc/IdentifyingRogueDHCPServers
from __future__ import print_function
from scapy.all import *
import sys
# Turn off response IP address validation
conf.checkIPaddr = False
# Set up the interface
fam,hw = get_if_raw_hwaddr(conf.iface)
dhcp_discover = Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0",dst="255.255.255.255")/UDP(sport=68,dport=67)/BOOTP(chaddr=hw)/DHCP(options=[("message-type","discover"),"end"])
#print("Press Ctrl-C after several seconds...", fd=sys.stderr)
ans, unans = srp(dhcp_discover, multi=True, timeout=5)
if len(ans) == 0:
print("No DHCP offers received", file=sys.stderr)
else:
print("DHCP offers received:")
for pair in ans:
p = pair[1]
d = p[DHCP]
print("MAC: {0}, Server IP: {1}, Offer IP: {2}\n Mask: {3}, Router: {4}, Domain: {5}".format(
p[Ether].src,
p[IP].src,
p[BOOTP].yiaddr,
filter(lambda x: x[0] == 'subnet_mask', d.options)[0][1],
filter(lambda x: x[0] == 'router', d.options)[0][1],
filter(lambda x: x[0] == 'domain', d.options)[0][1],
))
The easiest way to capture the DHCP exchange would be to run a packet capture on the DHCP Server or Client during the configuration process (easily done by ipconfig /release /renew
on Windows and dhclient -r; dhclient
on *nix). I highly recommend using Wireshark for this, or capturing the exchange in a command line utility (tcpdump
for example in *nix) then opening the capture in Wireshark for easy analysis of the exchange.
dhclient (on some systems at least) has a -s option to specify a particular server rather than using the default broadcast.