Skip to content

Instantly share code, notes, and snippets.

@blark
Last active December 25, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blark/7037006 to your computer and use it in GitHub Desktop.
Save blark/7037006 to your computer and use it in GitHub Desktop.
LLMNR listener round 2
import socket
import struct
import datetime
ALL = "0.0.0.0"
LLMNR_ADDR = "224.0.0.252"
LLMNR_PORT = 5355
# Setup network stuff
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((ALL, LLMNR_PORT))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
# Join multicast group
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(LLMNR_ADDR) + socket.inet_aton(ALL))
while True:
try:
data, addr = sock.recvfrom(1024)
nameLen = int(data[12])
# Define the packet structure
LLMNR_HEADER = "!HHHHHHb" + str(nameLen) + "sbHH"
# Unpack data and assign to variables, the _ throws away unused stuff
reqID, flags, questions, _, _, _, _, hostname, _, queryType, queryClass \
= struct.unpack(LLMNR_HEADER, data)
print("[%s] request id 0x%04x for %s from %s" %
(datetime.datetime.now(), reqID, hostname.decode('utf-8'), addr[0]))
except:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment