pedronrivera

Pentester Academy Log Analysis

Oct 22nd, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 50.77 KB | None | 0 0
  1. # Pentester Academy Log Analysis #
  2.  
  3. Command-Line Log Analysis
  4. JOEMCCRAY FEB 20TH, 2016 (EDITED) 1,680 NEVER
  5.  
  6. rawdownloadcloneembedreportprintdiff text 25.07 KB
  7. ##################################
  8. # Pentester Academy Log Analysis #
  9. ##################################
  10.  
  11. I'm doing this set of videos for my good friend Vivek Ramachandran at SecurityTube.net/PentesterAcademy.com
  12.  
  13.  
  14.  
  15.  
  16.  
  17. ##########
  18. # VMWare #
  19. ##########
  20. - For this workshop you'll need the latest version of VMWare Workstation (Windows), Fusion (Mac), or Player.
  21.  
  22. - Although you can get the VM to run in VirtualBox, I will not be supporting this configuration for this class.
  23.  
  24. VM for these labs
  25. -----------------
  26. https://s3.amazonaws.com/StrategicSec-VMs/Win7x64.zip
  27. username: workshop
  28. password: password
  29.  
  30.  
  31.  
  32.  
  33. ##############################################
  34. # Log Analysis with Linux command-line tools #
  35. ##############################################
  36. The following command line executables are found in the Mac as well as most Linux Distributions.
  37.  
  38. cat – prints the content of a file in the terminal window
  39. grep – searches and filters based on patterns
  40. awk – can sort each row into fields and display only what is needed
  41. sed – performs find and replace functions
  42. sort – arranges output in an order
  43. uniq – compares adjacent lines and can report, filter or provide a count of duplicates
  44.  
  45.  
  46.  
  47. ###############
  48. # Apache Logs #
  49. ###############
  50.  
  51. Reference:
  52. http://www.the-art-of-web.com/system/logs/
  53.  
  54. wget https://s3.amazonaws.com/SecureNinja/Python/access_log
  55.  
  56.  
  57. You want to list all user agents ordered by the number of times they appear (descending order):
  58.  
  59. awk -F\" '{print $6}' access_log | sort | uniq -c | sort -fr
  60.  
  61.  
  62.  
  63. Using the default separator which is any white-space (spaces or tabs) we get the following:
  64.  
  65. awk '{print $1}' access_log # ip address (%h)
  66. awk '{print $2}' access_log # RFC 1413 identity (%l)
  67. awk '{print $3}' access_log # userid (%u)
  68. awk '{print $4,5}' access_log # date/time (%t)
  69. awk '{print $9}' access_log # status code (%>s)
  70. awk '{print $10}' access_log # size (%b)
  71.  
  72. You might notice that we've missed out some items. To get to them we need to set the delimiter to the " character which changes the way the lines are 'exploded' and allows the following:
  73.  
  74. awk -F\" '{print $2}' access_log # request line (%r)
  75. awk -F\" '{print $4}' access_log # referer
  76. awk -F\" '{print $6}' access_log # user agent
  77.  
  78.  
  79. awk -F\" '{print $6}' access_log \
  80. | sed 's/(\([^;]\+; [^;]\+\)[^)]*)/(\1)/' \
  81. | sort | uniq -c | sort -fr
  82.  
  83.  
  84. The next step is to start filtering the output so you can narrow down on a certain page or referer. Would you like to know which pages Google has been requesting from your site?
  85.  
  86. awk -F\" '($6 ~ /Googlebot/){print $2}' access_log | awk '{print $2}'
  87. Or who's been looking at your guestbook?
  88.  
  89. awk -F\" '($2 ~ /guestbook\.html/){print $6}' access_log
  90.  
  91.  
  92. Reference:
  93. https://blog.nexcess.net/2011/01/21/one-liners-for-apache-log-files/
  94.  
  95. # top 20 URLs from the last 5000 hits
  96. tail -5000 ./access_log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  97. tail -5000 ./access_log | awk '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  98.  
  99. # top 20 URLS excluding POST data from the last 5000 hits
  100. tail -5000 ./access_log | awk -F"[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  101. tail -5000 ./access_log | awk -F"[ ?]" '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  102.  
  103. # top 20 IPs from the last 5000 hits
  104. tail -5000 ./access_log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
  105. tail -5000 ./access_log | awk '{freq[$1]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  106.  
  107. # top 20 URLs requested from a certain ip from the last 5000 hits
  108. IP=1.2.3.4; tail -5000 ./access_log | grep $IP | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  109. IP=1.2.3.4; tail -5000 ./access_log | awk -v ip=$IP ' $1 ~ ip {freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  110.  
  111. # top 20 URLS requested from a certain ip excluding, excluding POST data, from the last 5000 hits
  112. IP=1.2.3.4; tail -5000 ./access_log | fgrep $IP | awk -F "[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  113. IP=1.2.3.4; tail -5000 ./access_log | awk -F"[ ?]" -v ip=$IP ' $1 ~ ip {freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  114.  
  115. # top 20 referrers from the last 5000 hits
  116. tail -5000 ./access_log | awk '{print $11}' | tr -d '"' | sort | uniq -c | sort -rn | head -20
  117. tail -5000 ./access_log | awk '{freq[$11]++} END {for (x in freq) {print freq[x], x}}' | tr -d '"' | sort -rn | head -20
  118.  
  119. # top 20 user agents from the last 5000 hits
  120. tail -5000 ./access_log | cut -d\ -f12- | sort | uniq -c | sort -rn | head -20
  121.  
  122. # sum of data (in MB) transferred in the last 5000 hits
  123. tail -5000 ./access_log | awk '{sum+=$10} END {print sum/1048576}'
  124.  
  125.  
  126. ##############
  127. # Cisco Logs #
  128. ##############
  129.  
  130. wget https://s3.amazonaws.com/StrategicSec-Files/LogAnalysis/cisco.log
  131.  
  132.  
  133. AWK Basics
  134. ----------
  135. To quickly demonstrate the print feature in awk, we can instruct it to show only the 5th word of each line. Here we will print $5. Only the last 4 lines are being shown for brevity.
  136.  
  137. cat cisco.log | awk '{print $5}' | tail -n 4
  138.  
  139.  
  140.  
  141.  
  142. Looking at a large file would still produce a large amount of output. A more useful thing to do might be to output every entry found in “$5”, group them together, count them, then sort them from the greatest to least number of occurrences. This can be done by piping the output through “sort“, using “uniq -c” to count the like entries, then using “sort -rn” to sort it in reverse order.
  143.  
  144. cat cisco.log | awk '{print $5}'| sort | uniq -c | sort -rn
  145.  
  146.  
  147.  
  148.  
  149. While that’s sort of cool, it is obvious that we have some garbage in our output. Evidently we have a few lines that aren’t conforming to the output we expect to see in $5. We can insert grep to filter the file prior to feeding it to awk. This insures that we are at least looking at lines of text that contain “facility-level-mnemonic”.
  150.  
  151. cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk '{print $5}' | sort | uniq -c | sort -rn
  152.  
  153.  
  154.  
  155.  
  156.  
  157. Now that the output is cleaned up a bit, it is a good time to investigate some of the entries that appear most often. One way to see all occurrences is to use grep.
  158.  
  159. cat cisco.log | grep %LINEPROTO-5-UPDOWN:
  160.  
  161. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| awk '{print $10}' | sort | uniq -c | sort -rn
  162.  
  163. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10}' | sort | uniq -c | sort -rn
  164.  
  165. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10 " changed to " $14}' | sort | uniq -c | sort -rn
  166.  
  167.  
  168.  
  169.  
  170. #################################
  171. # Using Python for log analysis #
  172. #################################
  173.  
  174.  
  175.  
  176.  
  177. ###########################################
  178. # Python Basics Lesson 1: Simple Printing #
  179. ###########################################
  180.  
  181. >>> print 1
  182.  
  183. >>> print hello
  184.  
  185. >>> print "hello"
  186.  
  187. >>> print "Today we are learning Python."
  188.  
  189.  
  190.  
  191. ###################################################
  192. # Python Basics Lesson 2: Simple Numbers and Math #
  193. ###################################################
  194.  
  195. >>> 2+2
  196.  
  197. >>> 6-3
  198.  
  199. >>> 18/7
  200.  
  201. >>> 18.0/7
  202.  
  203. >>> 18.0/7.0
  204.  
  205. >>> 18/7
  206.  
  207. >>> 9%4
  208.  
  209. >>> 8%4
  210.  
  211. >>> 8.75%.5
  212.  
  213. >>> 6.*7
  214.  
  215. >>> 6*6*6
  216.  
  217. >>> 6**3
  218.  
  219. >>> 5**12
  220.  
  221. >>> -5**4
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. #####################################
  229. # Python Basics Lesson 3: Variables #
  230. #####################################
  231.  
  232. >>> x=18
  233.  
  234. >>> x+15
  235.  
  236. >>> x**3
  237.  
  238. >>> y=54
  239.  
  240. >>> x+y
  241.  
  242. >>> age=input("Enter number here: ")
  243. 43
  244.  
  245. >>> age+32
  246.  
  247. >>> age**3
  248.  
  249. >>> fname = raw_input("Enter your first name: ")
  250.  
  251. >>> lname = raw_input("Enter your first name: ")
  252.  
  253. >>> fname = raw_input("Enter your name: ")
  254. Enter your name: Joe
  255.  
  256. >>> lname = raw_input("Enter your name: ")
  257. Enter your name: McCray
  258.  
  259. >>> print fname
  260. Joe
  261.  
  262. >>> print lname
  263. McCray
  264.  
  265. >>> print fname lname
  266.  
  267. >>> print fname+lname
  268. JoeMcCray
  269.  
  270.  
  271.  
  272. NOTE:
  273. Use "input() for integers and expressions, and use raw_input() when you are dealing with strings.
  274.  
  275.  
  276.  
  277.  
  278.  
  279. #################################################
  280. # Python Basics Lesson 4: Modules and Functions #
  281. #################################################
  282.  
  283. >>> 5**4
  284.  
  285. >>> pow(5,4)
  286.  
  287. >>> abs(-18)
  288.  
  289. >>> abs(5)
  290.  
  291. >>> floor(18.7)
  292.  
  293. >>> import math
  294.  
  295. >>> math.floor(18.7)
  296.  
  297. >>> math.sqrt(81)
  298.  
  299. >>> joe = math.sqrt
  300.  
  301. >>> joe(9)
  302.  
  303. >>> joe=math.floor
  304.  
  305. >>> joe(19.8)
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. ###################################
  316. # Python Basics Lesson 5: Strings #
  317. ###################################
  318.  
  319. >>> "XSS"
  320.  
  321. >>> 'SQLi'
  322.  
  323. >>> "Joe's a python lover"
  324.  
  325. >>> 'Joe\'s a python lover'
  326.  
  327. >>> "Joe said \"InfoSec is fun\" to me"
  328.  
  329. >>> a = "Joe"
  330.  
  331. >>> b = "McCray"
  332.  
  333. >>> a, b
  334.  
  335. >>> a+b
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344. ########################################
  345. # Python Basics Lesson 6: More Strings #
  346. ########################################
  347.  
  348. >>> num = 10
  349.  
  350. >>> num + 2
  351.  
  352. >>> "The number of open ports found on this system is " + num
  353.  
  354. >>> num = str(18)
  355.  
  356. >>> "There are " + num + " vulnerabilities found in this environment."
  357.  
  358. >>> num2 = 46
  359.  
  360. >>> "As of 08/20/2012, the number of states that enacted the Security Breach Notification Law is " + `num2`
  361.  
  362.  
  363.  
  364. NOTE:
  365. Use "input() for integers and expressions, and use raw_input() when you are dealing with strings.
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373. ###############################################
  374. # Python Basics Lesson 7: Sequences and Lists #
  375. ###############################################
  376.  
  377. >>> attacks = ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  378.  
  379. >>> attacks
  380. ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  381.  
  382. >>> attacks[3]
  383. 'SQL Injection'
  384.  
  385. >>> attacks[-2]
  386. 'Cross-Site Scripting'
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393. ########################################
  394. # Python Basics Level 8: If Statement #
  395. ########################################
  396. >>> attack="SQLI"
  397. >>> if attack=="SQLI":
  398. print 'The attacker is using SQLI'
  399.  
  400. >>> attack="XSS"
  401. >>> if attack=="SQLI":
  402. print 'The attacker is using SQLI'
  403.  
  404.  
  405. #############################
  406. # Reference Videos To Watch #
  407. #############################
  408. Here is your first set of youtube videos that I'd like for you to watch:
  409. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 1-10)
  410.  
  411.  
  412.  
  413.  
  414.  
  415. #####################################
  416. # Lesson 9: Intro to Log Analysis #
  417. #####################################
  418.  
  419. Login to your StrategicSec Ubuntu machine. You can download the VM from the following link:
  420.  
  421. https://s3.amazonaws.com/StrategicSec-VMs/Strategicsec-Ubuntu-VPN-163.zip
  422. username: strategicsec
  423. password: strategicsec
  424.  
  425. Then execute the following commands:
  426. ---------------------------------------------------------------------------------------------------------
  427.  
  428.  
  429. wget https://s3.amazonaws.com/SecureNinja/Python/access_log
  430.  
  431.  
  432. cat access_log | grep 141.101.80.188
  433.  
  434. cat access_log | grep 141.101.80.187
  435.  
  436. cat access_log | grep 108.162.216.204
  437.  
  438. cat access_log | grep 173.245.53.160
  439.  
  440. ---------------------------------------------------------
  441.  
  442. Google the following terms:
  443. - Python read file
  444. - Python read line
  445. - Python read from file
  446.  
  447.  
  448.  
  449.  
  450. ########################################################
  451. # Lesson 10: Use Python to read in a file line by line #
  452. ########################################################
  453.  
  454.  
  455. Reference:
  456. http://cmdlinetips.com/2011/08/three-ways-to-read-a-text-file-line-by-line-in-python/
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463. Let's have some fun.....
  464.  
  465.  
  466. >>> f = open('access_log', "r")
  467.  
  468. >>> lines = f.readlines()
  469.  
  470. >>> print lines
  471.  
  472. >>> lines[0]
  473.  
  474. >>> lines[10]
  475.  
  476. >>> lines[50]
  477.  
  478. >>> lines[1000]
  479.  
  480. >>> lines[5000]
  481.  
  482. >>> lines[10000]
  483.  
  484. >>> print len(lines)
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494. ---------------------------------------------------------
  495. vi logread1.py
  496.  
  497.  
  498. ## Open the file with read only permit
  499. f = open('access_log', "r")
  500.  
  501. ## use readlines to read all lines in the file
  502. ## The variable "lines" is a list containing all lines
  503. lines = f.readlines()
  504.  
  505. print lines
  506.  
  507.  
  508. ## close the file after reading the lines.
  509. f.close()
  510.  
  511. ---------------------------------------------------------
  512.  
  513.  
  514. Google the following:
  515. - python difference between readlines and readline
  516. - python readlines and readline
  517.  
  518.  
  519.  
  520.  
  521.  
  522. #################################
  523. # Lesson 11: A quick challenge #
  524. #################################
  525.  
  526. Can you write an if/then statement that looks for this IP and print "Found it"?
  527.  
  528.  
  529. 141.101.81.187
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536. ---------------------------------------------------------
  537. Hint 1: Use Python to look for a value in a list
  538.  
  539. Reference:
  540. http://www.wellho.net/mouth/1789_Looking-for-a-value-in-a-list-Python.html
  541.  
  542.  
  543.  
  544.  
  545. ---------------------------------------------------------
  546. Hint 2: Use Python to prompt for user input
  547.  
  548. Reference:
  549. http://www.cyberciti.biz/faq/python-raw_input-examples/
  550.  
  551.  
  552.  
  553.  
  554. ---------------------------------------------------------
  555. Hint 3: Use Python to search for a string in a list
  556.  
  557. Reference:
  558. http://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string
  559.  
  560.  
  561.  
  562.  
  563.  
  564. Here is my solution:
  565. -------------------
  566. $ python
  567. >>> f = open('access_log', "r")
  568. >>> lines = f.readlines()
  569. >>> ip = '141.101.81.187'
  570. >>> for string in lines:
  571. ... if ip in string:
  572. ... print(string)
  573.  
  574.  
  575.  
  576.  
  577. Here is one student's solution - can you please explain each line of this code to me?
  578. -------------------------------------------------------------------------------------
  579. #!/usr/bin/python
  580.  
  581. f = open('access_log')
  582.  
  583. strUsrinput = raw_input("Enter IP Address: ")
  584.  
  585. for line in iter(f):
  586. ip = line.split(" - ")[0]
  587. if ip == strUsrinput:
  588. print line
  589.  
  590. f.close()
  591.  
  592.  
  593.  
  594.  
  595. -------------------------------
  596.  
  597. Working with another student after class we came up with another solution:
  598.  
  599. #!/usr/bin/env python
  600.  
  601.  
  602. # This line opens the log file
  603. f=open('access_log',"r")
  604.  
  605. # This line takes each line in the log file and stores it as an element in the list
  606. lines = f.readlines()
  607.  
  608.  
  609. # This lines stores the IP that the user types as a var called userinput
  610. userinput = raw_input("Enter the IP you want to search for: ")
  611.  
  612.  
  613.  
  614. # This combination for loop and nested if statement looks for the IP in the list called lines and prints the entire line if found.
  615. for ip in lines:
  616. if ip.find(userinput) != -1:
  617. print ip
  618.  
  619.  
  620.  
  621. ##################################################
  622. # Lesson 12: Look for web attacks in a log file #
  623. ##################################################
  624.  
  625. 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.
  626. Supported attacks:
  627. 1. SQL Injection
  628. 2. Local File Inclusion
  629. 3. Remote File Inclusion
  630. 4. Cross-Site Scripting
  631.  
  632.  
  633.  
  634. wget https://s3.amazonaws.com/SecureNinja/Python/scan_log.py
  635.  
  636. The usage for scan_log.py is simple. You feed it an apache log file.
  637.  
  638. cat scan_log.py | less (use your up/down arrow keys to look through the file)
  639.  
  640.  
  641.  
  642.  
  643.  
  644. ################################
  645. # Log Analysis with Powershell #
  646. ################################
  647.  
  648. VM for these labs
  649. -----------------
  650. https://s3.amazonaws.com/StrategicSec-VMs/Win7x64.zip
  651. username: workshop
  652. password: password
  653.  
  654.  
  655. You can do the updates in the Win7 VM (yes, it is a lot of updates).
  656.  
  657. You'll need to create directory in the Win7 VM called "c:\ps"
  658.  
  659. #####################
  660. # Powershell Basics #
  661. #####################
  662.  
  663. PowerShell is Microsoft’s new scripting language that has been built in since the release Vista.
  664.  
  665. PowerShell file extension end in .ps1 .
  666.  
  667. An important note is that you cannot double click on a PowerShell script to execute it.
  668.  
  669. To open a PowerShell command prompt either hit Windows Key + R and type in PowerShell or Start -> All Programs -> Accessories -> Windows PowerShell -> Windows PowerShell.
  670.  
  671. dir
  672. cd
  673. ls
  674. cd c:\
  675.  
  676.  
  677. To obtain a list of cmdlets, use the Get-Command cmdlet
  678.  
  679. Get-Command
  680.  
  681.  
  682.  
  683. You can use the Get-Alias cmdlet to see a full list of aliased commands.
  684.  
  685. Get-Alias
  686.  
  687.  
  688.  
  689. Don't worry you won't blow up your machine with Powershell
  690. Get-Process | stop-process What will this command do?
  691. Get-Process | stop-process -whatif
  692.  
  693.  
  694. To get help with a cmdlet, use the Get-Help cmdlet along with the cmdlet you want information about.
  695.  
  696. Get-Help Get-Command
  697.  
  698. Get-Help Get-Service –online
  699.  
  700. Get-Service -Name TermService, Spooler
  701.  
  702. Get-Service –N BITS
  703.  
  704. Start-Transcript
  705.  
  706. PowerShell variables begin with the $ symbol. First lets create a variable
  707.  
  708. $serv = Get-Service –N Spooler
  709.  
  710. To see the value of a variable you can just call it in the terminal.
  711.  
  712. $serv
  713.  
  714. $serv.gettype().fullname
  715.  
  716.  
  717. Get-Member is another extremely useful cmdlet that will enumerate the available methods and properties of an object. You can pipe the object to Get-Member or pass it in
  718.  
  719. $serv | Get-Member
  720.  
  721. Get-Member -InputObject $serv
  722.  
  723.  
  724.  
  725.  
  726.  
  727. Let’s use a method and a property with our object.
  728.  
  729. $serv.Status
  730. $serv.Stop()
  731. $serv.Refresh()
  732. $serv.Status
  733. $serv.Start()
  734. $serv.Refresh()
  735. $serv.Status
  736.  
  737.  
  738.  
  739.  
  740. Methods can return properties and properties can have sub properties. You can chain them together by appending them to the first call.
  741.  
  742.  
  743.  
  744. #############################
  745. # Simple Event Log Analysis #
  746. #############################
  747.  
  748. Step 1: Dump the event logs
  749. ---------------------------
  750. The first thing to do is to dump them into a format that facilitates later processing with Windows PowerShell.
  751.  
  752. To dump the event log, you can use the Get-EventLog and the Exportto-Clixml cmdlets if you are working with a traditional event log such as the Security, Application, or System event logs.
  753. If you need to work with one of the trace logs, use the Get-WinEvent and the ExportTo-Clixml cmdlets.
  754.  
  755. Get-EventLog -LogName application | Export-Clixml Applog.xml
  756.  
  757. type .\Applog.xml
  758.  
  759. $logs = "system","application","security"
  760.  
  761. The % symbol is an alias for the Foreach-Object cmdlet. It is often used when working interactively from the Windows PowerShell console
  762.  
  763. $logs | % { get-eventlog -LogName $_ | Export-Clixml "$_.xml" }
  764.  
  765.  
  766.  
  767. Step 2: Import the event log of interest
  768. ----------------------------------------
  769. To parse the event logs, use the Import-Clixml cmdlet to read the stored XML files.
  770. Store the results in a variable.
  771. Let's take a look at the commandlets Where-Object, Group-Object, and Select-Object.
  772.  
  773. The following two commands first read the exported security log contents into a variable named $seclog, and then the five oldest entries are obtained.
  774.  
  775. $seclog = Import-Clixml security.xml
  776.  
  777. $seclog | select -Last 5
  778.  
  779.  
  780. Cool trick from one of our students named Adam. This command allows you to look at the logs for the last 24 hours:
  781.  
  782. Get-EventLog Application -After (Get-Date).AddDays(-1)
  783.  
  784. You can use '-after' and '-before' to filter date ranges
  785.  
  786. One thing you must keep in mind is that once you export the security log to XML, it is no longer protected by anything more than the NFTS and share permissions that are assigned to the location where you store everything.
  787. By default, an ordinary user does not have permission to read the security log.
  788.  
  789.  
  790. Step 3: Drill into a specific entry
  791. -----------------------------------
  792. To view the entire contents of a specific event log entry, choose that entry, send the results to the Format-List cmdlet, and choose all of the properties.
  793.  
  794.  
  795. $seclog | select -first 1 | fl *
  796.  
  797. The message property contains the SID, account name, user domain, and privileges that are assigned for the new login.
  798.  
  799.  
  800. ($seclog | select -first 1).message
  801.  
  802. (($seclog | select -first 1).message).gettype()
  803.  
  804.  
  805.  
  806. In the *nix world you often want a count of something (wc -l).
  807. How often is the SeSecurityPrivilege privilege mentioned in the message property?
  808. To obtain this information, pipe the contents of the security log to a Where-Object to filter the events, and then send the results to the Measure-Object cmdlet to determine the number of events:
  809.  
  810. $seclog | ? { $_.message -match 'SeSecurityPrivilege'} | measure
  811.  
  812. If you want to ensure that only event log entries return that contain SeSecurityPrivilege in their text, use Group-Object to gather the matches by the EventID property.
  813.  
  814.  
  815. $seclog | ? { $_.message -match 'SeSecurityPrivilege'} | group eventid
  816.  
  817. Because importing the event log into a variable from the stored XML results in a collection of event log entries, it means that the count property is also present.
  818. Use the count property to determine the total number of entries in the event log.
  819.  
  820. $seclog.Count
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827. ############################
  828. # Simple Log File Analysis #
  829. ############################
  830.  
  831.  
  832. You'll need to create the directory c:\ps and download sample iss log http://pastebin.com/raw.php?i=LBn64cyA
  833.  
  834.  
  835. mkdir c:\ps
  836. cd c:\ps
  837. (new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=LBn64cyA", "c:\ps\u_ex1104.log")
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846. ###############################################
  847. # Intrusion Analysis Using Windows PowerShell #
  848. ###############################################
  849.  
  850. Download sample file http://pastebin.com/raw.php?i=ysnhXxTV into the c:\ps directory
  851.  
  852.  
  853.  
  854.  
  855.  
  856. (new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=ysnhXxTV", "c:\ps\CiscoLogFileExamples.txt")
  857.  
  858. Select-String 192.168.208.63 .\CiscoLogFileExamples.txt
  859.  
  860.  
  861.  
  862.  
  863. The Select-String cmdlet searches for text and text patterns in input strings and files. You can use it like Grep in UNIX and Findstr in Windows.
  864.  
  865. Select-String 192.168.208.63 .\CiscoLogFileExamples.txt | select line
  866.  
  867.  
  868.  
  869.  
  870. To see how many connections are made when analyzing a single host, the output from that can be piped to another command: Measure-Object.
  871.  
  872. Select-String 192.168.208.63 .\CiscoLogFileExamples.txt | select line | Measure-Object
  873.  
  874.  
  875.  
  876. To select all IP addresses in the file expand the matches property, select the value, get unique values and measure the output.
  877.  
  878. Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique | Measure-Object
  879.  
  880.  
  881.  
  882. Removing Measure-Object shows all the individual IPs instead of just the count of the IP addresses. The Measure-Object command counts the IP addresses.
  883.  
  884. Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique
  885.  
  886.  
  887. In order to determine which IP addresses have the most communication the last commands are removed to determine the value of the matches. Then the group command is issued on the piped output to group all the IP addresses (value), and then sort the objects by using the alias for Sort-Object: sort count –des.
  888. This sorts the IP addresses in a descending pattern as well as count and deliver the output to the shell.
  889.  
  890. Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select value | group value | sort count -des
  891.  
  892.  
  893.  
  894.  
  895. This will get the setting for logs in the windows firewall which should be enabled in GPO policy for analysis.
  896. The command shows that the Firewall log is at:
  897. %systemroot%\system32\LogFiles\Firewall\pfirewall.log, in order to open the file PowerShell will need to be run with administrative privileges.
  898.  
  899.  
  900. First step is to get the above command into a variable using script logic.
  901. Thankfully PowerShell has a built-in integrated scripting environment, PowerShell.ise.
  902.  
  903. netsh advfirewall show allprofiles | Select-String FileName | select -ExpandProperty line | Select-String “%systemroot%.+\.log" | select -ExpandProperty matches | select -ExpandProperty value | sort –uniq
  904.  
  905.  
  906. ##############################################
  907. # Parsing Log files using windows PowerShell #
  908. ##############################################
  909.  
  910. Download the sample IIS log http://pastebin.com/LBn64cyA
  911.  
  912.  
  913. (new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=LBn64cyA", "c:\ps\u_ex1104.log")
  914.  
  915. Get-Content ".\*log" | ? { ($_ | Select-String "WebDAV")}
  916.  
  917.  
  918.  
  919. The above command would give us all the WebDAV requests.
  920.  
  921. To filter this to a particular user name, use the below command:
  922.  
  923. Get-Content ".\*log" | ? { ($_ | Select-String "WebDAV") -and ($_ | Select-String "OPTIONS")}
  924.  
  925.  
  926.  
  927. Some more options that will be more commonly required :
  928.  
  929. For Outlook Web Access : Replace WebDAV with OWA
  930.  
  931. For EAS : Replace WebDAV with Microsoft-server-activesync
  932.  
  933. For ECP : Replace WebDAV with ECP
  934.  
  935.  
  936.  
  937. To find out the count of the EWS request we can go ahead and run the below command
  938.  
  939. (Get-Content ".\*log" | ? { ($_ | Select-String "WebDAV") -and ($_ | Select-String "Useralias")}).count
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949. Explain to me how this script works.
  950. RAW Paste Data
  951.  
  952. ##################################
  953. # Pentester Academy Log Analysis #
  954. ##################################
  955.  
  956. I'm doing this set of videos for my good friend Vivek Ramachandran at SecurityTube.net/PentesterAcademy.com
  957.  
  958.  
  959.  
  960.  
  961.  
  962. ##########
  963. # VMWare #
  964. ##########
  965. - For this workshop you'll need the latest version of VMWare Workstation (Windows), Fusion (Mac), or Player.
  966.  
  967. - Although you can get the VM to run in VirtualBox, I will not be supporting this configuration for this class.
  968.  
  969. VM for these labs
  970. -----------------
  971. https://s3.amazonaws.com/StrategicSec-VMs/Win7x64.zip
  972. username: workshop
  973. password: password
  974.  
  975.  
  976.  
  977.  
  978. ##############################################
  979. # Log Analysis with Linux command-line tools #
  980. ##############################################
  981. The following command line executables are found in the Mac as well as most Linux Distributions.
  982.  
  983. cat – prints the content of a file in the terminal window
  984. grep – searches and filters based on patterns
  985. awk – can sort each row into fields and display only what is needed
  986. sed – performs find and replace functions
  987. sort – arranges output in an order
  988. uniq – compares adjacent lines and can report, filter or provide a count of duplicates
  989.  
  990.  
  991.  
  992. ###############
  993. # Apache Logs #
  994. ###############
  995.  
  996. Reference:
  997. http://www.the-art-of-web.com/system/logs/
  998.  
  999. wget https://s3.amazonaws.com/SecureNinja/Python/access_log
  1000.  
  1001.  
  1002. You want to list all user agents ordered by the number of times they appear (descending order):
  1003.  
  1004. awk -F\" '{print $6}' access_log | sort | uniq -c | sort -fr
  1005.  
  1006.  
  1007.  
  1008. Using the default separator which is any white-space (spaces or tabs) we get the following:
  1009.  
  1010. awk '{print $1}' access_log # ip address (%h)
  1011. awk '{print $2}' access_log # RFC 1413 identity (%l)
  1012. awk '{print $3}' access_log # userid (%u)
  1013. awk '{print $4,5}' access_log # date/time (%t)
  1014. awk '{print $9}' access_log # status code (%>s)
  1015. awk '{print $10}' access_log # size (%b)
  1016.  
  1017. You might notice that we've missed out some items. To get to them we need to set the delimiter to the " character which changes the way the lines are 'exploded' and allows the following:
  1018.  
  1019. awk -F\" '{print $2}' access_log # request line (%r)
  1020. awk -F\" '{print $4}' access_log # referer
  1021. awk -F\" '{print $6}' access_log # user agent
  1022.  
  1023.  
  1024. awk -F\" '{print $6}' access_log \
  1025. | sed 's/(\([^;]\+; [^;]\+\)[^)]*)/(\1)/' \
  1026. | sort | uniq -c | sort -fr
  1027.  
  1028.  
  1029. The next step is to start filtering the output so you can narrow down on a certain page or referer. Would you like to know which pages Google has been requesting from your site?
  1030.  
  1031. awk -F\" '($6 ~ /Googlebot/){print $2}' access_log | awk '{print $2}'
  1032. Or who's been looking at your guestbook?
  1033.  
  1034. awk -F\" '($2 ~ /guestbook\.html/){print $6}' access_log
  1035.  
  1036.  
  1037. Reference:
  1038. https://blog.nexcess.net/2011/01/21/one-liners-for-apache-log-files/
  1039.  
  1040. # top 20 URLs from the last 5000 hits
  1041. tail -5000 ./access_log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  1042. tail -5000 ./access_log | awk '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1043.  
  1044. # top 20 URLS excluding POST data from the last 5000 hits
  1045. tail -5000 ./access_log | awk -F"[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  1046. tail -5000 ./access_log | awk -F"[ ?]" '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1047.  
  1048. # top 20 IPs from the last 5000 hits
  1049. tail -5000 ./access_log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
  1050. tail -5000 ./access_log | awk '{freq[$1]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1051.  
  1052. # top 20 URLs requested from a certain ip from the last 5000 hits
  1053. IP=1.2.3.4; tail -5000 ./access_log | grep $IP | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  1054. IP=1.2.3.4; tail -5000 ./access_log | awk -v ip=$IP ' $1 ~ ip {freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1055.  
  1056. # top 20 URLS requested from a certain ip excluding, excluding POST data, from the last 5000 hits
  1057. IP=1.2.3.4; tail -5000 ./access_log | fgrep $IP | awk -F "[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  1058. IP=1.2.3.4; tail -5000 ./access_log | awk -F"[ ?]" -v ip=$IP ' $1 ~ ip {freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1059.  
  1060. # top 20 referrers from the last 5000 hits
  1061. tail -5000 ./access_log | awk '{print $11}' | tr -d '"' | sort | uniq -c | sort -rn | head -20
  1062. tail -5000 ./access_log | awk '{freq[$11]++} END {for (x in freq) {print freq[x], x}}' | tr -d '"' | sort -rn | head -20
  1063.  
  1064. # top 20 user agents from the last 5000 hits
  1065. tail -5000 ./access_log | cut -d\ -f12- | sort | uniq -c | sort -rn | head -20
  1066.  
  1067. # sum of data (in MB) transferred in the last 5000 hits
  1068. tail -5000 ./access_log | awk '{sum+=$10} END {print sum/1048576}'
  1069.  
  1070.  
  1071. ##############
  1072. # Cisco Logs #
  1073. ##############
  1074.  
  1075. wget https://s3.amazonaws.com/StrategicSec-Files/LogAnalysis/cisco.log
  1076.  
  1077.  
  1078. AWK Basics
  1079. ----------
  1080. To quickly demonstrate the print feature in awk, we can instruct it to show only the 5th word of each line. Here we will print $5. Only the last 4 lines are being shown for brevity.
  1081.  
  1082. cat cisco.log | awk '{print $5}' | tail -n 4
  1083.  
  1084.  
  1085.  
  1086.  
  1087. Looking at a large file would still produce a large amount of output. A more useful thing to do might be to output every entry found in “$5”, group them together, count them, then sort them from the greatest to least number of occurrences. This can be done by piping the output through “sort“, using “uniq -c” to count the like entries, then using “sort -rn” to sort it in reverse order.
  1088.  
  1089. cat cisco.log | awk '{print $5}'| sort | uniq -c | sort -rn
  1090.  
  1091.  
  1092.  
  1093.  
  1094. While that’s sort of cool, it is obvious that we have some garbage in our output. Evidently we have a few lines that aren’t conforming to the output we expect to see in $5. We can insert grep to filter the file prior to feeding it to awk. This insures that we are at least looking at lines of text that contain “facility-level-mnemonic”.
  1095.  
  1096. cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk '{print $5}' | sort | uniq -c | sort -rn
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102. Now that the output is cleaned up a bit, it is a good time to investigate some of the entries that appear most often. One way to see all occurrences is to use grep.
  1103.  
  1104. cat cisco.log | grep %LINEPROTO-5-UPDOWN:
  1105.  
  1106. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| awk '{print $10}' | sort | uniq -c | sort -rn
  1107.  
  1108. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10}' | sort | uniq -c | sort -rn
  1109.  
  1110. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10 " changed to " $14}' | sort | uniq -c | sort -rn
  1111.  
  1112.  
  1113.  
  1114.  
  1115. #################################
  1116. # Using Python for log analysis #
  1117. #################################
  1118.  
  1119.  
  1120.  
  1121.  
  1122. ###########################################
  1123. # Python Basics Lesson 1: Simple Printing #
  1124. ###########################################
  1125.  
  1126. >>> print 1
  1127.  
  1128. >>> print hello
  1129.  
  1130. >>> print "hello"
  1131.  
  1132. >>> print "Today we are learning Python."
  1133.  
  1134.  
  1135.  
  1136. ###################################################
  1137. # Python Basics Lesson 2: Simple Numbers and Math #
  1138. ###################################################
  1139.  
  1140. >>> 2+2
  1141.  
  1142. >>> 6-3
  1143.  
  1144. >>> 18/7
  1145.  
  1146. >>> 18.0/7
  1147.  
  1148. >>> 18.0/7.0
  1149.  
  1150. >>> 18/7
  1151.  
  1152. >>> 9%4
  1153.  
  1154. >>> 8%4
  1155.  
  1156. >>> 8.75%.5
  1157.  
  1158. >>> 6.*7
  1159.  
  1160. >>> 6*6*6
  1161.  
  1162. >>> 6**3
  1163.  
  1164. >>> 5**12
  1165.  
  1166. >>> -5**4
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173. #####################################
  1174. # Python Basics Lesson 3: Variables #
  1175. #####################################
  1176.  
  1177. >>> x=18
  1178.  
  1179. >>> x+15
  1180.  
  1181. >>> x**3
  1182.  
  1183. >>> y=54
  1184.  
  1185. >>> x+y
  1186.  
  1187. >>> age=input("Enter number here: ")
  1188. 43
  1189.  
  1190. >>> age+32
  1191.  
  1192. >>> age**3
  1193.  
  1194. >>> fname = raw_input("Enter your first name: ")
  1195.  
  1196. >>> lname = raw_input("Enter your first name: ")
  1197.  
  1198. >>> fname = raw_input("Enter your name: ")
  1199. Enter your name: Joe
  1200.  
  1201. >>> lname = raw_input("Enter your name: ")
  1202. Enter your name: McCray
  1203.  
  1204. >>> print fname
  1205. Joe
  1206.  
  1207. >>> print lname
  1208. McCray
  1209.  
  1210. >>> print fname lname
  1211.  
  1212. >>> print fname+lname
  1213. JoeMcCray
  1214.  
  1215.  
  1216.  
  1217. NOTE:
  1218. Use "input() for integers and expressions, and use raw_input() when you are dealing with strings.
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224. #################################################
  1225. # Python Basics Lesson 4: Modules and Functions #
  1226. #################################################
  1227.  
  1228. >>> 5**4
  1229.  
  1230. >>> pow(5,4)
  1231.  
  1232. >>> abs(-18)
  1233.  
  1234. >>> abs(5)
  1235.  
  1236. >>> floor(18.7)
  1237.  
  1238. >>> import math
  1239.  
  1240. >>> math.floor(18.7)
  1241.  
  1242. >>> math.sqrt(81)
  1243.  
  1244. >>> joe = math.sqrt
  1245.  
  1246. >>> joe(9)
  1247.  
  1248. >>> joe=math.floor
  1249.  
  1250. >>> joe(19.8)
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260. ###################################
  1261. # Python Basics Lesson 5: Strings #
  1262. ###################################
  1263.  
  1264. >>> "XSS"
  1265.  
  1266. >>> 'SQLi'
  1267.  
  1268. >>> "Joe's a python lover"
  1269.  
  1270. >>> 'Joe\'s a python lover'
  1271.  
  1272. >>> "Joe said \"InfoSec is fun\" to me"
  1273.  
  1274. >>> a = "Joe"
  1275.  
  1276. >>> b = "McCray"
  1277.  
  1278. >>> a, b
  1279.  
  1280. >>> a+b
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289. ########################################
  1290. # Python Basics Lesson 6: More Strings #
  1291. ########################################
  1292.  
  1293. >>> num = 10
  1294.  
  1295. >>> num + 2
  1296.  
  1297. >>> "The number of open ports found on this system is " + num
  1298.  
  1299. >>> num = str(18)
  1300.  
  1301. >>> "There are " + num + " vulnerabilities found in this environment."
  1302.  
  1303. >>> num2 = 46
  1304.  
  1305. >>> "As of 08/20/2012, the number of states that enacted the Security Breach Notification Law is " + `num2`
  1306.  
  1307.  
  1308.  
  1309. NOTE:
  1310. Use "input() for integers and expressions, and use raw_input() when you are dealing with strings.
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318. ###############################################
  1319. # Python Basics Lesson 7: Sequences and Lists #
  1320. ###############################################
  1321.  
  1322. >>> attacks = ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  1323.  
  1324. >>> attacks
  1325. ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  1326.  
  1327. >>> attacks[3]
  1328. 'SQL Injection'
  1329.  
  1330. >>> attacks[-2]
  1331. 'Cross-Site Scripting'
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338. ########################################
  1339. # Python Basics Level 8: If Statement #
  1340. ########################################
  1341. >>> attack="SQLI"
  1342. >>> if attack=="SQLI":
  1343. print 'The attacker is using SQLI'
  1344.  
  1345. >>> attack="XSS"
  1346. >>> if attack=="SQLI":
  1347. print 'The attacker is using SQLI'
  1348.  
  1349.  
  1350. #############################
  1351. # Reference Videos To Watch #
  1352. #############################
  1353. Here is your first set of youtube videos that I'd like for you to watch:
  1354. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 1-10)
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360. #####################################
  1361. # Lesson 9: Intro to Log Analysis #
  1362. #####################################
  1363.  
  1364. Login to your StrategicSec Ubuntu machine. You can download the VM from the following link:
  1365.  
  1366. https://s3.amazonaws.com/StrategicSec-VMs/Strategicsec-Ubuntu-VPN-163.zip
  1367. username: strategicsec
  1368. password: strategicsec
  1369.  
  1370. Then execute the following commands:
  1371. ---------------------------------------------------------------------------------------------------------
  1372.  
  1373.  
  1374. wget https://s3.amazonaws.com/SecureNinja/Python/access_log
  1375.  
  1376.  
  1377. cat access_log | grep 141.101.80.188
  1378.  
  1379. cat access_log | grep 141.101.80.187
  1380.  
  1381. cat access_log | grep 108.162.216.204
  1382.  
  1383. cat access_log | grep 173.245.53.160
  1384.  
  1385. ---------------------------------------------------------
  1386.  
  1387. Google the following terms:
  1388. - Python read file
  1389. - Python read line
  1390. - Python read from file
  1391.  
  1392.  
  1393.  
  1394.  
  1395. ########################################################
  1396. # Lesson 10: Use Python to read in a file line by line #
  1397. ########################################################
  1398.  
  1399.  
  1400. Reference:
  1401. http://cmdlinetips.com/2011/08/three-ways-to-read-a-text-file-line-by-line-in-python/
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408. Let's have some fun.....
  1409.  
  1410.  
  1411. >>> f = open('access_log', "r")
  1412.  
  1413. >>> lines = f.readlines()
  1414.  
  1415. >>> print lines
  1416.  
  1417. >>> lines[0]
  1418.  
  1419. >>> lines[10]
  1420.  
  1421. >>> lines[50]
  1422.  
  1423. >>> lines[1000]
  1424.  
  1425. >>> lines[5000]
  1426.  
  1427. >>> lines[10000]
  1428.  
  1429. >>> print len(lines)
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439. ---------------------------------------------------------
  1440. vi logread1.py
  1441.  
  1442.  
  1443. ## Open the file with read only permit
  1444. f = open('access_log', "r")
  1445.  
  1446. ## use readlines to read all lines in the file
  1447. ## The variable "lines" is a list containing all lines
  1448. lines = f.readlines()
  1449.  
  1450. print lines
  1451.  
  1452.  
  1453. ## close the file after reading the lines.
  1454. f.close()
  1455.  
  1456. ---------------------------------------------------------
  1457.  
  1458.  
  1459. Google the following:
  1460. - python difference between readlines and readline
  1461. - python readlines and readline
  1462.  
  1463.  
  1464.  
  1465.  
  1466.  
  1467. #################################
  1468. # Lesson 11: A quick challenge #
  1469. #################################
  1470.  
  1471. Can you write an if/then statement that looks for this IP and print "Found it"?
  1472.  
  1473.  
  1474. 141.101.81.187
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481. ---------------------------------------------------------
  1482. Hint 1: Use Python to look for a value in a list
  1483.  
  1484. Reference:
  1485. http://www.wellho.net/mouth/1789_Looking-for-a-value-in-a-list-Python.html
  1486.  
  1487.  
  1488.  
  1489.  
  1490. ---------------------------------------------------------
  1491. Hint 2: Use Python to prompt for user input
  1492.  
  1493. Reference:
  1494. http://www.cyberciti.biz/faq/python-raw_input-examples/
  1495.  
  1496.  
  1497.  
  1498.  
  1499. ---------------------------------------------------------
  1500. Hint 3: Use Python to search for a string in a list
  1501.  
  1502. Reference:
  1503. http://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509. Here is my solution:
  1510. -------------------
  1511. $ python
  1512. >>> f = open('access_log', "r")
  1513. >>> lines = f.readlines()
  1514. >>> ip = '141.101.81.187'
  1515. >>> for string in lines:
  1516. ... if ip in string:
  1517. ... print(string)
  1518.  
  1519.  
  1520.  
  1521.  
  1522. Here is one student's solution - can you please explain each line of this code to me?
  1523. -------------------------------------------------------------------------------------
  1524. #!/usr/bin/python
  1525.  
  1526. f = open('access_log')
  1527.  
  1528. strUsrinput = raw_input("Enter IP Address: ")
  1529.  
  1530. for line in iter(f):
  1531. ip = line.split(" - ")[0]
  1532. if ip == strUsrinput:
  1533. print line
  1534.  
  1535. f.close()
  1536.  
  1537.  
  1538.  
  1539.  
  1540. -------------------------------
  1541.  
  1542. Working with another student after class we came up with another solution:
  1543.  
  1544. #!/usr/bin/env python
  1545.  
  1546.  
  1547. # This line opens the log file
  1548. f=open('access_log',"r")
  1549.  
  1550. # This line takes each line in the log file and stores it as an element in the list
  1551. lines = f.readlines()
  1552.  
  1553.  
  1554. # This lines stores the IP that the user types as a var called userinput
  1555. userinput = raw_input("Enter the IP you want to search for: ")
  1556.  
  1557.  
  1558.  
  1559. # This combination for loop and nested if statement looks for the IP in the list called lines and prints the entire line if found.
  1560. for ip in lines:
  1561. if ip.find(userinput) != -1:
  1562. print ip
  1563.  
  1564.  
  1565.  
  1566. ##################################################
  1567. # Lesson 12: Look for web attacks in a log file #
  1568. ##################################################
  1569.  
  1570. 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.
  1571. Supported attacks:
  1572. 1. SQL Injection
  1573. 2. Local File Inclusion
  1574. 3. Remote File Inclusion
  1575. 4. Cross-Site Scripting
  1576.  
  1577.  
  1578.  
  1579. wget https://s3.amazonaws.com/SecureNinja/Python/scan_log.py
  1580.  
  1581. The usage for scan_log.py is simple. You feed it an apache log file.
  1582.  
  1583. cat scan_log.py | less (use your up/down arrow keys to look through the file)
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589. ################################
  1590. # Log Analysis with Powershell #
  1591. ################################
  1592.  
  1593. VM for these labs
  1594. -----------------
  1595. https://s3.amazonaws.com/StrategicSec-VMs/Win7x64.zip
  1596. username: workshop
  1597. password: password
  1598.  
  1599.  
  1600. You can do the updates in the Win7 VM (yes, it is a lot of updates).
  1601.  
  1602. You'll need to create directory in the Win7 VM called "c:\ps"
  1603.  
  1604. #####################
  1605. # Powershell Basics #
  1606. #####################
  1607.  
  1608. PowerShell is Microsoft’s new scripting language that has been built in since the release Vista.
  1609.  
  1610. PowerShell file extension end in .ps1 .
  1611.  
  1612. An important note is that you cannot double click on a PowerShell script to execute it.
  1613.  
  1614. To open a PowerShell command prompt either hit Windows Key + R and type in PowerShell or Start -> All Programs -> Accessories -> Windows PowerShell -> Windows PowerShell.
  1615.  
  1616. dir
  1617. cd
  1618. ls
  1619. cd c:\
  1620.  
  1621.  
  1622. To obtain a list of cmdlets, use the Get-Command cmdlet
  1623.  
  1624. Get-Command
  1625.  
  1626.  
  1627.  
  1628. You can use the Get-Alias cmdlet to see a full list of aliased commands.
  1629.  
  1630. Get-Alias
  1631.  
  1632.  
  1633.  
  1634. Don't worry you won't blow up your machine with Powershell
  1635. Get-Process | stop-process What will this command do?
  1636. Get-Process | stop-process -whatif
  1637.  
  1638.  
  1639. To get help with a cmdlet, use the Get-Help cmdlet along with the cmdlet you want information about.
  1640.  
  1641. Get-Help Get-Command
  1642.  
  1643. Get-Help Get-Service –online
  1644.  
  1645. Get-Service -Name TermService, Spooler
  1646.  
  1647. Get-Service –N BITS
  1648.  
  1649. Start-Transcript
  1650.  
  1651. PowerShell variables begin with the $ symbol. First lets create a variable
  1652.  
  1653. $serv = Get-Service –N Spooler
  1654.  
  1655. To see the value of a variable you can just call it in the terminal.
  1656.  
  1657. $serv
  1658.  
  1659. $serv.gettype().fullname
  1660.  
  1661.  
  1662. Get-Member is another extremely useful cmdlet that will enumerate the available methods and properties of an object. You can pipe the object to Get-Member or pass it in
  1663.  
  1664. $serv | Get-Member
  1665.  
  1666. Get-Member -InputObject $serv
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672. Let’s use a method and a property with our object.
  1673.  
  1674. $serv.Status
  1675. $serv.Stop()
  1676. $serv.Refresh()
  1677. $serv.Status
  1678. $serv.Start()
  1679. $serv.Refresh()
  1680. $serv.Status
  1681.  
  1682.  
  1683.  
  1684.  
  1685. Methods can return properties and properties can have sub properties. You can chain them together by appending them to the first call.
  1686.  
  1687.  
  1688.  
  1689. #############################
  1690. # Simple Event Log Analysis #
  1691. #############################
  1692.  
  1693. Step 1: Dump the event logs
  1694. ---------------------------
  1695. The first thing to do is to dump them into a format that facilitates later processing with Windows PowerShell.
  1696.  
  1697. To dump the event log, you can use the Get-EventLog and the Exportto-Clixml cmdlets if you are working with a traditional event log such as the Security, Application, or System event logs.
  1698. If you need to work with one of the trace logs, use the Get-WinEvent and the ExportTo-Clixml cmdlets.
  1699.  
  1700. Get-EventLog -LogName application | Export-Clixml Applog.xml
  1701.  
  1702. type .\Applog.xml
  1703.  
  1704. $logs = "system","application","security"
  1705.  
  1706. The % symbol is an alias for the Foreach-Object cmdlet. It is often used when working interactively from the Windows PowerShell console
  1707.  
  1708. $logs | % { get-eventlog -LogName $_ | Export-Clixml "$_.xml" }
  1709.  
  1710.  
  1711.  
  1712. Step 2: Import the event log of interest
  1713. ----------------------------------------
  1714. To parse the event logs, use the Import-Clixml cmdlet to read the stored XML files.
  1715. Store the results in a variable.
  1716. Let's take a look at the commandlets Where-Object, Group-Object, and Select-Object.
  1717.  
  1718. The following two commands first read the exported security log contents into a variable named $seclog, and then the five oldest entries are obtained.
  1719.  
  1720. $seclog = Import-Clixml security.xml
  1721.  
  1722. $seclog | select -Last 5
  1723.  
  1724.  
  1725. Cool trick from one of our students named Adam. This command allows you to look at the logs for the last 24 hours:
  1726.  
  1727. Get-EventLog Application -After (Get-Date).AddDays(-1)
  1728.  
  1729. You can use '-after' and '-before' to filter date ranges
  1730.  
  1731. One thing you must keep in mind is that once you export the security log to XML, it is no longer protected by anything more than the NFTS and share permissions that are assigned to the location where you store everything.
  1732. By default, an ordinary user does not have permission to read the security log.
  1733.  
  1734.  
  1735. Step 3: Drill into a specific entry
  1736. -----------------------------------
  1737. To view the entire contents of a specific event log entry, choose that entry, send the results to the Format-List cmdlet, and choose all of the properties.
  1738.  
  1739.  
  1740. $seclog | select -first 1 | fl *
  1741.  
  1742. The message property contains the SID, account name, user domain, and privileges that are assigned for the new login.
  1743.  
  1744.  
  1745. ($seclog | select -first 1).message
  1746.  
  1747. (($seclog | select -first 1).message).gettype()
  1748.  
  1749.  
  1750.  
  1751. In the *nix world you often want a count of something (wc -l).
  1752. How often is the SeSecurityPrivilege privilege mentioned in the message property?
  1753. To obtain this information, pipe the contents of the security log to a Where-Object to filter the events, and then send the results to the Measure-Object cmdlet to determine the number of events:
  1754.  
  1755. $seclog | ? { $_.message -match 'SeSecurityPrivilege'} | measure
  1756.  
  1757. If you want to ensure that only event log entries return that contain SeSecurityPrivilege in their text, use Group-Object to gather the matches by the EventID property.
  1758.  
  1759.  
  1760. $seclog | ? { $_.message -match 'SeSecurityPrivilege'} | group eventid
  1761.  
  1762. Because importing the event log into a variable from the stored XML results in a collection of event log entries, it means that the count property is also present.
  1763. Use the count property to determine the total number of entries in the event log.
  1764.  
  1765. $seclog.Count
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772. ############################
  1773. # Simple Log File Analysis #
  1774. ############################
  1775.  
  1776.  
  1777. You'll need to create the directory c:\ps and download sample iss log http://pastebin.com/raw.php?i=LBn64cyA
  1778.  
  1779.  
  1780. mkdir c:\ps
  1781. cd c:\ps
  1782. (new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=LBn64cyA", "c:\ps\u_ex1104.log")
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791. ###############################################
  1792. # Intrusion Analysis Using Windows PowerShell #
  1793. ###############################################
  1794.  
  1795. Download sample file http://pastebin.com/raw.php?i=ysnhXxTV into the c:\ps directory
  1796.  
  1797.  
  1798.  
  1799.  
  1800.  
  1801. (new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=ysnhXxTV", "c:\ps\CiscoLogFileExamples.txt")
  1802.  
  1803. Select-String 192.168.208.63 .\CiscoLogFileExamples.txt
  1804.  
  1805.  
  1806.  
  1807.  
  1808. The Select-String cmdlet searches for text and text patterns in input strings and files. You can use it like Grep in UNIX and Findstr in Windows.
  1809.  
  1810. Select-String 192.168.208.63 .\CiscoLogFileExamples.txt | select line
  1811.  
  1812.  
  1813.  
  1814.  
  1815. To see how many connections are made when analyzing a single host, the output from that can be piped to another command: Measure-Object.
  1816.  
  1817. Select-String 192.168.208.63 .\CiscoLogFileExamples.txt | select line | Measure-Object
  1818.  
  1819.  
  1820.  
  1821. To select all IP addresses in the file expand the matches property, select the value, get unique values and measure the output.
  1822.  
  1823. Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique | Measure-Object
  1824.  
  1825.  
  1826.  
  1827. Removing Measure-Object shows all the individual IPs instead of just the count of the IP addresses. The Measure-Object command counts the IP addresses.
  1828.  
  1829. Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select -ExpandProperty value | Sort-Object -Unique
  1830.  
  1831.  
  1832. In order to determine which IP addresses have the most communication the last commands are removed to determine the value of the matches. Then the group command is issued on the piped output to group all the IP addresses (value), and then sort the objects by using the alias for Sort-Object: sort count –des.
  1833. This sorts the IP addresses in a descending pattern as well as count and deliver the output to the shell.
  1834.  
  1835. Select-String “\b(?:\d{1,3}\.){3}\d{1,3}\b” .\CiscoLogFileExamples.txt | select -ExpandProperty matches | select value | group value | sort count -des
  1836.  
  1837.  
  1838.  
  1839.  
  1840. This will get the setting for logs in the windows firewall which should be enabled in GPO policy for analysis.
  1841. The command shows that the Firewall log is at:
  1842. %systemroot%\system32\LogFiles\Firewall\pfirewall.log, in order to open the file PowerShell will need to be run with administrative privileges.
  1843.  
  1844.  
  1845. First step is to get the above command into a variable using script logic.
  1846. Thankfully PowerShell has a built-in integrated scripting environment, PowerShell.ise.
  1847.  
  1848. netsh advfirewall show allprofiles | Select-String FileName | select -ExpandProperty line | Select-String “%systemroot%.+\.log" | select -ExpandProperty matches | select -ExpandProperty value | sort –uniq
  1849.  
  1850.  
  1851. ##############################################
  1852. # Parsing Log files using windows PowerShell #
  1853. ##############################################
  1854.  
  1855. Download the sample IIS log http://pastebin.com/LBn64cyA
  1856.  
  1857.  
  1858. (new-object System.Net.WebClient).DownloadFile("http://pastebin.com/raw.php?i=LBn64cyA", "c:\ps\u_ex1104.log")
  1859.  
  1860. Get-Content ".\*log" | ? { ($_ | Select-String "WebDAV")}
  1861.  
  1862.  
  1863.  
  1864. The above command would give us all the WebDAV requests.
  1865.  
  1866. To filter this to a particular user name, use the below command:
  1867.  
  1868. Get-Content ".\*log" | ? { ($_ | Select-String "WebDAV") -and ($_ | Select-String "OPTIONS")}
  1869.  
  1870.  
  1871.  
  1872. Some more options that will be more commonly required :
  1873.  
  1874. For Outlook Web Access : Replace WebDAV with OWA
  1875.  
  1876. For EAS : Replace WebDAV with Microsoft-server-activesync
  1877.  
  1878. For ECP : Replace WebDAV with ECP
  1879.  
  1880.  
  1881.  
  1882. To find out the count of the EWS request we can go ahead and run the below command
  1883.  
  1884. (Get-Content ".\*log" | ? { ($_ | Select-String "WebDAV") -and ($_ | Select-String "Useralias")}).count
  1885.  
  1886.  
  1887. Explain to me how this script works.
  1888.  
  1889.  
  1890. create new paste / dealsnew! / api / trends / syntax languages / faq / tools / privacy / cookies / contact / dmca / advertise on pastebin / scraping / go
  1891. Site design & logo © 2016 Pastebin; user contributions (pastes) licensed under cc by-sa 3.0 -- Dedicated Server Hosting by Steadfast Top
Add Comment
Please, Sign In to add comment