2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2012-03-27 22:59:12 +04:00
|
|
|
|
|
|
|
"""
|
2013-01-18 18:07:51 +04:00
|
|
|
Copyright (c) 2006-2013 sqlmap developers (http://sqlmap.org/)
|
2012-03-27 22:59:12 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
2012-03-28 17:46:31 +04:00
|
|
|
import os
|
2012-04-02 16:22:40 +04:00
|
|
|
import re
|
2012-03-27 23:29:24 +04:00
|
|
|
import socket
|
|
|
|
import threading
|
2012-03-28 17:46:31 +04:00
|
|
|
import time
|
2012-03-27 23:29:24 +04:00
|
|
|
|
2012-12-06 13:42:53 +04:00
|
|
|
class DNSQuery(object):
|
2012-03-27 22:59:12 +04:00
|
|
|
"""
|
|
|
|
Used for making fake DNS resolution responses based on received
|
|
|
|
raw request
|
|
|
|
|
|
|
|
Reference(s):
|
|
|
|
http://code.activestate.com/recipes/491264-mini-fake-dns-server/
|
|
|
|
https://code.google.com/p/marlon-tools/source/browse/tools/dnsproxy/dnsproxy.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, raw):
|
|
|
|
self._raw = raw
|
|
|
|
self._query = ""
|
|
|
|
|
|
|
|
type_ = (ord(raw[2]) >> 3) & 15 # Opcode bits
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-03-27 22:59:12 +04:00
|
|
|
if type_ == 0: # Standard query
|
|
|
|
i = 12
|
|
|
|
j = ord(raw[i])
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-03-27 22:59:12 +04:00
|
|
|
while j != 0:
|
2013-01-10 16:18:44 +04:00
|
|
|
self._query += raw[i + 1:i + j + 1] + '.'
|
2012-03-27 22:59:12 +04:00
|
|
|
i = i + j + 1
|
|
|
|
j = ord(raw[i])
|
|
|
|
|
|
|
|
def response(self, resolution):
|
2012-04-04 17:24:58 +04:00
|
|
|
"""
|
|
|
|
Crafts raw DNS resolution response packet
|
|
|
|
"""
|
|
|
|
|
2012-03-28 17:46:31 +04:00
|
|
|
retVal = ""
|
2012-03-27 22:59:12 +04:00
|
|
|
|
|
|
|
if self._query:
|
2012-05-27 14:03:02 +04:00
|
|
|
retVal += self._raw[:2] # Transaction ID
|
2012-05-28 20:57:10 +04:00
|
|
|
retVal += "\x85\x80" # Flags (Standard query response, No error)
|
2012-03-28 17:46:31 +04:00
|
|
|
retVal += self._raw[4:6] + self._raw[4:6] + "\x00\x00\x00\x00" # Questions and Answers Counts
|
2012-05-27 14:03:02 +04:00
|
|
|
retVal += self._raw[12:(12 + self._raw[12:].find("\x00") + 5)] # Original Domain Name Query
|
2012-03-28 17:46:31 +04:00
|
|
|
retVal += "\xc0\x0c" # Pointer to domain name
|
2012-05-27 14:03:02 +04:00
|
|
|
retVal += "\x00\x01" # Type A
|
|
|
|
retVal += "\x00\x01" # Class IN
|
2012-05-28 23:50:12 +04:00
|
|
|
retVal += "\x00\x00\x00\x20" # TTL (32 seconds)
|
2012-05-27 14:03:02 +04:00
|
|
|
retVal += "\x00\x04" # Data length
|
2012-03-28 17:46:31 +04:00
|
|
|
retVal += "".join(chr(int(_)) for _ in resolution.split('.')) # 4 bytes of IP
|
2012-03-27 22:59:12 +04:00
|
|
|
|
2012-03-28 17:46:31 +04:00
|
|
|
return retVal
|
2012-03-27 23:29:24 +04:00
|
|
|
|
2012-12-06 13:42:53 +04:00
|
|
|
class DNSServer(object):
|
2012-03-27 23:29:24 +04:00
|
|
|
def __init__(self):
|
|
|
|
self._requests = []
|
2012-03-28 17:46:31 +04:00
|
|
|
self._lock = threading.Lock()
|
2012-03-30 17:20:29 +04:00
|
|
|
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
2012-07-27 12:02:47 +04:00
|
|
|
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2012-03-30 17:20:29 +04:00
|
|
|
self._socket.bind(("", 53))
|
|
|
|
self._running = False
|
2014-01-09 18:46:55 +04:00
|
|
|
self._initialized = False
|
2012-03-30 17:20:29 +04:00
|
|
|
|
2012-04-03 14:10:33 +04:00
|
|
|
def pop(self, prefix=None, suffix=None):
|
2012-04-04 17:24:58 +04:00
|
|
|
"""
|
|
|
|
Returns received DNS resolution request (if any) that has given
|
|
|
|
prefix/suffix combination (e.g. prefix.<query result>.suffix.domain)
|
|
|
|
"""
|
|
|
|
|
2012-04-02 16:22:40 +04:00
|
|
|
retVal = None
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-04-02 16:22:40 +04:00
|
|
|
with self._lock:
|
|
|
|
for _ in self._requests:
|
2012-05-28 19:01:56 +04:00
|
|
|
if prefix is None and suffix is None or re.search("%s\..+\.%s" % (prefix, suffix), _, re.I):
|
2012-04-02 16:22:40 +04:00
|
|
|
retVal = _
|
|
|
|
self._requests.remove(_)
|
|
|
|
break
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-04-02 16:22:40 +04:00
|
|
|
return retVal
|
|
|
|
|
2012-03-27 23:29:24 +04:00
|
|
|
def run(self):
|
2012-04-04 17:24:58 +04:00
|
|
|
"""
|
|
|
|
Runs a DNSServer instance as a daemon thread (killed by program exit)
|
|
|
|
"""
|
|
|
|
|
2012-03-27 23:29:24 +04:00
|
|
|
def _():
|
|
|
|
try:
|
2012-03-30 17:20:29 +04:00
|
|
|
self._running = True
|
2014-01-09 18:46:55 +04:00
|
|
|
self._initialized = True
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-03-27 23:29:24 +04:00
|
|
|
while True:
|
2012-03-30 17:20:29 +04:00
|
|
|
data, addr = self._socket.recvfrom(1024)
|
2012-03-27 23:29:24 +04:00
|
|
|
_ = DNSQuery(data)
|
2012-05-28 23:13:50 +04:00
|
|
|
self._socket.sendto(_.response("127.0.0.1"), addr)
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-03-30 17:20:29 +04:00
|
|
|
with self._lock:
|
|
|
|
self._requests.append(_._query)
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-03-30 17:20:29 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
2012-04-04 16:33:16 +04:00
|
|
|
|
2012-03-27 23:29:24 +04:00
|
|
|
finally:
|
2012-03-30 17:20:29 +04:00
|
|
|
self._running = False
|
2012-03-27 23:29:24 +04:00
|
|
|
|
|
|
|
thread = threading.Thread(target=_)
|
2012-04-03 13:18:30 +04:00
|
|
|
thread.daemon = True
|
2012-03-27 23:29:24 +04:00
|
|
|
thread.start()
|
2012-03-28 17:46:31 +04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-04-04 16:49:45 +04:00
|
|
|
server = None
|
2012-03-28 17:46:31 +04:00
|
|
|
try:
|
2012-03-30 17:20:29 +04:00
|
|
|
server = DNSServer()
|
2012-03-28 17:46:31 +04:00
|
|
|
server.run()
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2014-01-09 18:46:55 +04:00
|
|
|
while not server._initialized:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2012-03-30 17:20:29 +04:00
|
|
|
while server._running:
|
|
|
|
while True:
|
|
|
|
_ = server.pop()
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-03-30 17:20:29 +04:00
|
|
|
if _ is None:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print "[i] %s" % _
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-03-28 17:46:31 +04:00
|
|
|
time.sleep(1)
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-03-30 17:20:29 +04:00
|
|
|
except socket.error, ex:
|
|
|
|
if 'Permission' in str(ex):
|
|
|
|
print "[x] Please run with sudo/Administrator privileges"
|
|
|
|
else:
|
|
|
|
raise
|
2012-03-28 17:46:31 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
os._exit(0)
|
2012-03-30 17:20:29 +04:00
|
|
|
finally:
|
2012-04-04 16:49:45 +04:00
|
|
|
if server:
|
|
|
|
server._running = False
|