main.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. This file is part of the cintruder project, http://cintruder.03c8.net
  5. Copyright (c) 2012/2019 psy <epsylon@riseup.net>
  6. cintruder is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation version 3 of the License.
  9. cintruder is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  12. details.
  13. You should have received a copy of the GNU General Public License along
  14. with cintruder; if not, write to the Free Software Foundation, Inc., 51
  15. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. """
  17. import os, traceback, hashlib, sys, time, socket, urlparse
  18. import platform, subprocess, re, webbrowser, shutil
  19. from core.options import CIntruderOptions
  20. from core.crack import CIntruderCrack
  21. from core.ocr import CIntruderOCR
  22. from core.curl import CIntruderCurl
  23. from core.xml_export import CIntruderXML
  24. from core.update import Updater
  25. from urlparse import urlparse
  26. # set to emit debug messages about errors (0 = off).
  27. DEBUG = 0
  28. class cintruder():
  29. """
  30. CIntruder application class
  31. """
  32. def __init__(self):
  33. self.captcha = ""
  34. self.optionOCR = []
  35. self.optionCrack = []
  36. self.optionParser = None
  37. self.optionCurl = None
  38. self.options = None
  39. self.word_sug = None
  40. self.train == 0
  41. self.crack == 0
  42. self.ignoreproxy = 1
  43. self.isurl = 0
  44. self.os_sys = platform.system()
  45. self._webbrowser = webbrowser
  46. self.GIT_REPOSITORY = 'https://code.03c8.net/epsylon/cintruder' # oficial code source [OK! 26/01/2019]
  47. self.GIT_REPOSITORY2 = 'https://github.com/epsylon/cintruder' # mirror source [since: 04/06/2018]
  48. def set_options(self, options):
  49. """
  50. Set cintruder options
  51. """
  52. self.options = options
  53. def create_options(self, args=None):
  54. """
  55. Create the program options for OptionParser.
  56. """
  57. self.optionParser = CIntruderOptions()
  58. self.options = self.optionParser.get_options(args)
  59. if not self.options:
  60. return False
  61. return self.options
  62. def set_webbrowser(self, browser):
  63. self._webbrowser = browser
  64. def banner(self):
  65. print '='*75
  66. print ""
  67. print " o8%8888, "
  68. print " o88%8888888. "
  69. print " 8'- -:8888b "
  70. print " 8' 8888 "
  71. print " d8.-=. ,==-.:888b "
  72. print " >8 `~` :`~' d8888 "
  73. print " 88 ,88888 "
  74. print " 88b. `-~ ':88888 "
  75. print " 888b \033[1;31m~==~\033[1;m .:88888 "
  76. print " 88888o--:':::8888 "
  77. print " `88888| :::' 8888b "
  78. print " 8888^^' 8888b "
  79. print " d888 ,%888b. "
  80. print " d88% %%%8--'-. "
  81. print " /88:.__ , _%-' --- - "
  82. print " '''::===..-' = --. `\n"
  83. print self.optionParser.description, "\n"
  84. print '='*75
  85. @classmethod
  86. def try_running(cls, func, error, args=None):
  87. """
  88. Try running a function and print some error if it fails and exists with
  89. a fatal error.
  90. """
  91. args = args or []
  92. try:
  93. return func(*args)
  94. except Exception:
  95. print(error, "error")
  96. if DEBUG:
  97. traceback.print_exc()
  98. def get_attack_captchas(self):
  99. """
  100. Get captchas to brute force
  101. """
  102. captchas = []
  103. options = self.options
  104. p = self.optionParser
  105. if options.train:
  106. print('='*75)
  107. print(str(p.version))
  108. print('='*75)
  109. print("Starting to 'train'...")
  110. print('='*75)
  111. captchas = [options.train]
  112. if options.crack:
  113. print('='*75)
  114. print(str(p.version))
  115. print('='*75)
  116. print("Starting to 'crack'")
  117. print('='*75)
  118. captchas = [options.crack]
  119. if options.track:
  120. print('='*75)
  121. print(str(p.version))
  122. print('='*75)
  123. print("Tracking captchas from url...")
  124. print('='*75+"\n")
  125. captchas = [options.track]
  126. return captchas
  127. def train(self, captchas):
  128. """
  129. Learn mode:
  130. + Add words to the brute forcing dictionary
  131. """
  132. self.train_captcha(captchas)
  133. def train_captcha(self, captcha):
  134. """
  135. Learn mode:
  136. 1- Apply OCR to captcha/image and split into unities
  137. 2- Human-Recognize that unities like alphanumeric words (gui supported)
  138. 3- Move that words into dictionary (gui supported)
  139. """
  140. options = self.options
  141. # step 1: applying OCR techniques
  142. if options.name: # with a specific OCR module
  143. print "[Info] Using module: [", options.name, "]"
  144. try:
  145. sys.path.append('mods/%s/'%(options.name))
  146. exec("from " + options.name + "_ocr" + " import CIntruderOCR") # import module
  147. except Exception:
  148. print "\n[Error] '"+ options.name+ "' module not found!\n"
  149. return #sys.exit(2)
  150. if options.setids: # with a specific colour ID
  151. setids = int(options.setids)
  152. if setids >= 0 and setids <= 255:
  153. self.optionOCR = CIntruderOCR(captcha, options)
  154. else:
  155. print "\n[Error] You must enter a valid RGB colour ID number (between 0 and 255)\n"
  156. return #sys.exit(2)
  157. else:
  158. self.optionOCR = CIntruderOCR(captcha, options)
  159. else: # using general OCR algorithm
  160. if options.setids: # with a specific colour ID
  161. setids = int(options.setids)
  162. if setids >= 0 and setids <= 255:
  163. self.optionOCR = CIntruderOCR(captcha, options)
  164. else:
  165. print "\n[Error] You must enter a valid RGB colour ID number (between 0 and 255)\n"
  166. return #sys.exit(2)
  167. else:
  168. self.optionOCR = CIntruderOCR(captcha, options)
  169. def crack(self, captchas):
  170. """
  171. Crack mode:
  172. + Brute force target's captcha against a dictionary
  173. """
  174. self.crack_captcha(captchas)
  175. def crack_captcha(self, captcha):
  176. """
  177. Crack mode: bruteforcing...
  178. """
  179. options = self.options
  180. if options.name:
  181. print "Loading module:", options.name
  182. print "==============="
  183. try:
  184. sys.path.append('mods/%s/'%(options.name))
  185. exec("from " + options.name + "_crack" + " import CIntruderCrack")
  186. except Exception:
  187. print "\n[Error] '"+ options.name+ "' module not found!\n"
  188. return #sys.exit(2)
  189. self.optionCrack = CIntruderCrack(captcha)
  190. w = self.optionCrack.crack(options)
  191. self.word_sug = w
  192. else:
  193. self.optionCrack = CIntruderCrack(captcha)
  194. w = self.optionCrack.crack(options)
  195. self.word_sug = w
  196. def remote(self, captchas, proxy):
  197. """
  198. Get remote captchas
  199. """
  200. l = []
  201. if not os.path.exists("inputs/"):
  202. os.mkdir("inputs/")
  203. for captcha in captchas:
  204. c = self.remote_captcha(captcha, proxy)
  205. l.append(c)
  206. return l
  207. def remote_captcha(self, captcha, proxy):
  208. """
  209. Get remote captcha
  210. """
  211. if proxy:
  212. self.ignoreproxy=0
  213. self.optionCurl = CIntruderCurl(captcha, self.ignoreproxy, proxy)
  214. buf = self.optionCurl.request()
  215. if buf != "exit":
  216. m = hashlib.md5()
  217. m.update(captcha)
  218. c = "%s.gif"%(m.hexdigest())
  219. h = "inputs/" + c
  220. f = open(h, 'wb')
  221. f.write(buf.getvalue())
  222. f.close
  223. buf.close
  224. return h
  225. else:
  226. return #sys.exit(2)
  227. def export(self, captchas):
  228. """
  229. Export results
  230. """
  231. if self.options.xml and not (self.options.train):
  232. self.optionXML = CIntruderXML(captchas)
  233. if self.word_sug == None:
  234. print "[Info] XML NOT created!. There are not words to suggest..."
  235. else:
  236. self.optionXML.print_xml_results(captchas, self.options.xml, self.word_sug)
  237. print "[Info] XML created:", self.options.xml, "\n"
  238. def track(self, captchas, proxy, num_tracks):
  239. """
  240. Download captchas from url
  241. """
  242. for captcha in captchas:
  243. self.track_captcha(captcha, proxy, num_tracks)
  244. def track_captcha(self, captcha, proxy, num_tracks):
  245. """
  246. This technique is useful to create a dictionary of 'session based' captchas
  247. """
  248. options = self.options
  249. urlp = urlparse(captcha)
  250. self.domain = urlp.hostname
  251. if not os.path.exists("inputs/%s"%(self.domain)):
  252. os.mkdir("inputs/%s"%(self.domain))
  253. if proxy:
  254. self.ignoreproxy = 0
  255. buf = ""
  256. i=0
  257. while i < int(num_tracks) and buf != "exit":
  258. self.optionCurl = CIntruderCurl(captcha, self.ignoreproxy, proxy)
  259. buf = self.optionCurl.request()
  260. if options.verbose:
  261. print "\n[-]Connection data:"
  262. out = self.optionCurl.print_options()
  263. print '-'*45
  264. if buf != "exit":
  265. m = hashlib.md5()
  266. m.update("%s%s"%(time.time(), captcha))
  267. h = "inputs/%s/%s.gif"%(self.domain, m.hexdigest())
  268. f = open(h, 'wb')
  269. f.write(buf.getvalue())
  270. f.close
  271. buf.close
  272. print "[Info] Saved:", h
  273. print "------------"
  274. i=i+1
  275. if buf != "exit":
  276. print "\n================="
  277. print "Tracking Results:"
  278. print "================="
  279. print "\nNumber of tracked captchas: [", num_tracks, "] \n"
  280. def run(self, opts=None):
  281. """
  282. Run cintruder
  283. """
  284. if opts:
  285. options = self.create_options(opts)
  286. self.set_options(options)
  287. options = self.options
  288. #step -1: run GUI/Web interface
  289. if options.web:
  290. self.create_web_interface()
  291. return
  292. #step -1: check/update for latest stable version
  293. if options.update:
  294. self.banner()
  295. try:
  296. print("\n[AI] Trying to update automatically to the latest stable version\n")
  297. Updater()
  298. except:
  299. print "Not any .git repository found!\n"
  300. print "="*30
  301. print "\nTo have working this feature, you should clone CIntruder with:\n"
  302. print "$ git clone %s" % self.GIT_REPOSITORY
  303. print "\nAlso you can try this other mirror:\n"
  304. print "$ git clone %s" % self.GIT_REPOSITORY2 + "\n"
  305. #step 0: list output results and get captcha targets
  306. if options.listmods:
  307. print "====================================="
  308. print "Listing specific OCR exploit modules:"
  309. print "=====================================\n"
  310. top = 'mods/'
  311. for root, dirs, files in os.walk(top, topdown=False):
  312. for name in files:
  313. if name == 'DESCRIPTION':
  314. if self.os_sys == "Windows": #check for win32 sys
  315. subprocess.call("type %s/%s"%(root, name), shell=True)
  316. else:
  317. subprocess.call("cat %s/%s"%(root, name), shell=True)
  318. print "\n[Info] List end...\n"
  319. return
  320. if options.track_list:
  321. print "=============================="
  322. print "Listing last tracked captchas:"
  323. print "==============================\n"
  324. top = 'inputs/'
  325. tracked_captchas = []
  326. for root, dirs, files in os.walk(top, topdown=False):
  327. for name in files:
  328. path = os.path.relpath(os.path.join(root, name))
  329. if self.os_sys == "Windows": #check for win32 sys
  330. atime = os.path.getatime(path)
  331. else:
  332. date = os.stat(path)
  333. atime = time.ctime(date.st_atime)
  334. tracked_captchas.append([atime,str(" + "+name),str(" |-> "+path)])
  335. tracked_captchas=sorted(tracked_captchas,key=lambda x:x[0]) # sorted by accessed time
  336. ca = 0 # to control max number of results
  337. for t in tracked_captchas:
  338. ca = ca + 1
  339. print "-------------------------"
  340. for c in t:
  341. if ca < 26:
  342. print c
  343. else:
  344. break
  345. print "-------------------------"
  346. print "\n[Info] List end...\n"
  347. return
  348. captchas = self.try_running(self.get_attack_captchas, "\nInternal error getting -captchas-. look at the end of this Traceback.")
  349. captchas = self.sanitize_captchas(captchas)
  350. captchas2track = captchas
  351. if self.isurl == 1 and (options.train or options.crack):
  352. if options.proxy:
  353. captchas = self.remote(captchas, options.proxy)
  354. else:
  355. captchas = self.remote(captchas, "")
  356. if options.verbose:
  357. print "[-] Connection data:"
  358. out = self.optionCurl.print_options()
  359. print '-'*45
  360. #step 0: track
  361. if options.track:
  362. if options.s_num:
  363. num_tracks = int(options.s_num) # tracking number defined by user
  364. else:
  365. num_tracks = int(5) # default track connections
  366. if options.proxy:
  367. self.try_running(self.track, "\nInternal problems tracking: ", (captchas2track, options.proxy, num_tracks))
  368. else:
  369. self.try_running(self.track, "\nInternal problems tracking: ", (captchas2track, "", num_tracks))
  370. #step 1: train
  371. if options.train:
  372. try:
  373. if len(captchas) == 1:
  374. for captcha in captchas:
  375. if captcha is None:
  376. print "\n[Error] Applying OCR algorithm... Is that captcha supported?\n"
  377. if os.path.exists('core/images/previews'):
  378. shutil.rmtree('core/images/previews') # remove last OCR
  379. else:
  380. print "Target:", options.train
  381. print "=======\n"
  382. self.try_running(self.train, "\nInternal problems training: ", (captchas))
  383. else:
  384. for captcha in captchas:
  385. if len(captchas) > 1 and captcha is None:
  386. pass
  387. else:
  388. print "Target: ", options.train
  389. self.try_running(self.train, "\nInternal problems training: ", (captchas))
  390. except:
  391. print "\n[Error] Something wrong getting captcha. Aborting...\n"
  392. if options.xml:
  393. print "[Info] You don't need export to XML on this mode... File not generated!\n"
  394. #step 2: crack
  395. if options.crack:
  396. if len(captchas) == 1:
  397. for captcha in captchas:
  398. if captcha is None:
  399. print "\n[Error] Trying to bruteforce... Is that captcha supported?\n"
  400. if os.path.exists('core/images/previews'):
  401. shutil.rmtree('core/images/previews') # remove last OCR
  402. else:
  403. print "Target: ", options.crack
  404. print "======="
  405. self.try_running(self.crack, "\nInternal problems cracking: ", (captchas))
  406. else:
  407. for captcha in captchas:
  408. if len(captchas) > 1 and captcha is None:
  409. pass
  410. else:
  411. print "Target: ", options.crack
  412. print "======="
  413. self.try_running(self.crack, "\nInternal problems cracking: ", (captchas))
  414. if options.command:
  415. print "[Info] Executing tool connector... \n"
  416. if self.word_sug is not None:
  417. print "[Info] This is the word suggested by CIntruder: [", self.word_sug, "] \n"
  418. else:
  419. print "[Error] CIntruder hasn't any word to suggest... Handlering tool process aborted! ;(\n"
  420. sys.exit(2)
  421. if "CINT" in options.command: # check parameter CINT on command (*)
  422. # change cintruder suggested word for the users captchas input form parameter
  423. # and execute handlered tool with it.
  424. if self.word_sug is not None:
  425. cmd = options.command.replace("CINT", self.word_sug)
  426. subprocess.call(cmd, shell=True)
  427. else:
  428. cmd = options.command
  429. subprocess.call(cmd, shell=True)
  430. else:
  431. print "[Error] Captcha's parameter flag: 'CINT' is not present on:", options.command, "\n"
  432. #step 3: export
  433. if options.xml:
  434. self.try_running(self.export, "\nInternal problems exporting: ", (captchas))
  435. def sanitize_captchas(self, captchas):
  436. """
  437. Sanitize correct input of source target(s)
  438. """
  439. options = self.options
  440. all_captchas = set()
  441. for captcha in captchas:
  442. # captcha from url
  443. if "http://" in captcha or "https://" in captcha:
  444. all_captchas.add(captcha)
  445. self.isurl = 1
  446. elif self.isurl == 0: # captcha from file
  447. (root, ext) = os.path.splitext(captcha)
  448. if ext != '.gif' and ext != '.jpg' and ext != '.jpeg' and ext != '.png': #by the moment
  449. captcha = None
  450. all_captchas.add(captcha)
  451. else:
  452. all_captchas.add(captcha)
  453. self.isurl = 0
  454. return all_captchas
  455. def create_web_interface(self):
  456. from webgui import ClientThread
  457. host = '0.0.0.0'
  458. port = 9999
  459. try:
  460. webbrowser.open('http://127.0.0.1:9999', new=1)
  461. tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  462. tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  463. tcpsock.bind((host,port))
  464. while True:
  465. tcpsock.listen(4)
  466. (clientsock, (ip, port)) = tcpsock.accept()
  467. newthread = ClientThread(ip, port, clientsock)
  468. newthread.start()
  469. except (KeyboardInterrupt, SystemExit):
  470. sys.exit()
  471. if __name__ == "__main__":
  472. app = cintruder()
  473. options = app.create_options()
  474. if options:
  475. app.set_options(options)
  476. app.run()