Advertisement
joemccray

InfoSec Addicts Intro to Linux

Jan 8th, 2017
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #####################################
  2. # InfoSecAddicts Intro to Linux #
  3. # By Joe McCray #
  4. #####################################
  5.  
  6.  
  7.  
  8. ##########
  9. # VMWare #
  10. ##########
  11. - For this workshop you'll need the latest version of VMWare Workstation (Windows), Fusion (Mac), or Player.
  12.  
  13. - http://www.vmware.com/ap/products/player.html
  14.  
  15.  
  16. - Although you can get the VM to run in VirtualBox, I will not be supporting this configuration for this class.
  17.  
  18.  
  19. ##########################
  20. # Download the attack VM #
  21. ##########################
  22. https://s3.amazonaws.com/infosecaddictsvirtualmachines/InfoSecAddictsVM.zip
  23. user: infosecaddicts
  24. pass: infosecaddicts
  25.  
  26. - Here is a good set of slides for getting started with Linux:
  27. http://www.slideshare.net/olafusimichael/linux-training-24086319
  28.  
  29.  
  30.  
  31.  
  32. - Log in to your Ubuntu host with the following credentials:
  33. user: infosecaddicts
  34. pass: infosecaddicts
  35.  
  36.  
  37.  
  38. - I prefer to use Putty to SSH into my Ubuntu host on pentests and I'll be teaching this class in the same manner that I do pentests.
  39. - You can download Putty from here:
  40. - http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
  41.  
  42.  
  43. - For the purpose of this workshop 192.168.230.128 is my Ubuntu IP address so anytime you see that IP you'll know that's my Ubuntu host
  44.  
  45.  
  46.  
  47. ########################
  48. # Basic Linux Commands #
  49. ########################
  50. cd ~
  51.  
  52. pwd
  53.  
  54. whereis pwd
  55.  
  56. which pwd
  57.  
  58. sudo find / -name pwd
  59.  
  60. /bin/pwd
  61.  
  62. mkdir LinuxBasics
  63.  
  64. cd LinuxBasics
  65.  
  66. touch one two three
  67.  
  68. ls -l t (without pressing the Enter key, press the Tab key twice. What happens?)
  69.  
  70. h (and again without pressing the Enter key, press the Tab key twice. What happens?)
  71.  
  72. Press the 'Up arrow key' (What happens?)
  73.  
  74. Press 'Ctrl-A' (What happens?)
  75.  
  76. ls
  77.  
  78. clear (What happens?)
  79.  
  80. echo one > one
  81.  
  82. cat one (What happens?)
  83.  
  84. man cat (What happens?)
  85. q
  86.  
  87. cat two
  88.  
  89. cat one > two
  90.  
  91. cat two
  92.  
  93. cat one two > three
  94.  
  95. cat three
  96.  
  97. echo four >> three
  98.  
  99. cat three (What happens?)
  100.  
  101. wc -l three
  102.  
  103. man wc
  104. q
  105.  
  106. cat three | grep four
  107.  
  108. cat three | grep one
  109.  
  110. man grep
  111. q
  112.  
  113.  
  114. man ps
  115. q
  116.  
  117. ps
  118.  
  119. ps aux
  120.  
  121. ps aux | less
  122.  
  123. Press the 'Up arrow key' (What happens?)
  124.  
  125. Press the 'Down arrow key' (What happens?)
  126. q
  127.  
  128. top
  129.  
  130.  
  131.  
  132. #########
  133. # Files #
  134. #########
  135. cd ~
  136.  
  137. pwd
  138.  
  139. ls
  140.  
  141. cd LinuxBasics
  142.  
  143. pwd
  144.  
  145. cd ~
  146.  
  147. pwd
  148.  
  149. cd LinuxBasics
  150.  
  151. ls
  152.  
  153. mkdir files
  154.  
  155. cp one files/
  156.  
  157. ls files/
  158.  
  159. cd files/
  160.  
  161. cp ../two .
  162.  
  163. ls
  164.  
  165. cp ../three .
  166.  
  167. ls
  168.  
  169. tar cvf files.tar *
  170.  
  171. ls
  172.  
  173. gzip files.tar
  174.  
  175. ls
  176.  
  177. rm -rf one two three
  178.  
  179. ls
  180.  
  181. tar -zxvf files.tar.gz
  182.  
  183. rm -rf files.tar.gz
  184.  
  185. sudo apt install -y zip unzip
  186.  
  187. zip data *
  188.  
  189. unzip -l data.zip
  190.  
  191. unzip data.zip -d /tmp
  192.  
  193. unzip -l data.zip
  194.  
  195.  
  196.  
  197. ############
  198. # VIM Demo #
  199. ############
  200. cd ~
  201. sudo apt install -y vim
  202. infosecaddicts
  203.  
  204. cd LinuxBasics
  205.  
  206. mkdir vimlesson
  207.  
  208. cd vimlesson
  209.  
  210. vi lesson1.sh
  211.  
  212. i (press "i" to get into INSERT mode and then paste in the lines below)
  213.  
  214. #!/bin/bash
  215.  
  216. echo "This is my first time using vi to create a shell script"
  217. echo " "
  218. echo " "
  219. echo " "
  220. sleep 5
  221. echo "Ok, now let's clear the screen"
  222. sleep 3
  223.  
  224.  
  225. ---------------don't put this line in your script----------------------------
  226.  
  227. ESC (press the ESC key to get you out of INSERT mode)
  228.  
  229. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  230.  
  231.  
  232. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  233.  
  234.  
  235.  
  236. vi lesson1.sh
  237.  
  238. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  239.  
  240. set number (typing "set number" immediately after SHIFT: will add line numbers to vim).
  241.  
  242. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  243.  
  244.  
  245.  
  246.  
  247. vi lesson1.sh
  248.  
  249. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  250.  
  251. set number (typing "set number" immediately after SHIFT: will add line numbers to vim).
  252.  
  253.  
  254. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  255.  
  256. /echo (typing "/echo" immediately after SHIFT: will search the file for the word echo).
  257.  
  258. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  259.  
  260. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  261.  
  262.  
  263.  
  264.  
  265. vi lesson1.sh
  266.  
  267. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  268.  
  269. set number (typing "set number" immediately after SHIFT: will add line numbers to vim).
  270.  
  271.  
  272. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  273.  
  274. 4 (typing "4" immediately after SHIFT: will take you to line number 4).
  275.  
  276. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  277.  
  278. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  279.  
  280.  
  281.  
  282.  
  283. vi lesson1.sh
  284.  
  285. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  286.  
  287. set number (typing "set number" immediately after SHIFT: will add line numbers to vim).
  288.  
  289.  
  290. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  291.  
  292. 4 (typing "4" immediately after SHIFT: will take you to line number 4).
  293.  
  294. dd (typing "dd" will delete the line that you are on)
  295.  
  296. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  297.  
  298. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  299.  
  300.  
  301.  
  302.  
  303. vi lesson1.sh
  304.  
  305. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  306.  
  307. set number (typing "set number" immediately after SHIFT: will add line numbers to vim).
  308.  
  309.  
  310. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  311.  
  312. 4 (typing "4" immediately after SHIFT: will take you to line number 4).
  313.  
  314. dd (typing "dd" will delete the line that you are on)
  315.  
  316. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  317.  
  318. syntax on (typing "syntax on" immediately after SHIFT: will turn on syntax highlighting
  319.  
  320. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  321.  
  322. set tabstop=5 (typing "set tabstop=5" immediately after SHIFT: will set your tabs to 5 spaces
  323.  
  324. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  325.  
  326. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  327.  
  328.  
  329.  
  330.  
  331. vi .vimrc
  332. i (press "i" to get into INSERT mode and then paste in the lines below)
  333.  
  334.  
  335. set number
  336. syntax on
  337. set tabstop=5
  338.  
  339.  
  340. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  341.  
  342. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349. vi lesson1.sh
  350.  
  351. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  352.  
  353. echo $MYVIMRC (typing "echo $MYVIMRC" immediately after SHIFT: will display the path to your new .vimrc file
  354.  
  355. [SHIFT+:] (press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
  356.  
  357. wq (typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
  358.  
  359.  
  360. ###############
  361. # Permissions #
  362. ###############
  363. cd ~
  364.  
  365. pwd
  366.  
  367. ls
  368.  
  369. cd LinuxBasics
  370.  
  371. ls -l one
  372.  
  373. We can determine a lot from examining the results of this command. The file "one" is owned by user "me".
  374. Now "me" has the right to read and write this file.
  375. The file is owned by the group "me". Members of the group "me" can also read and write this file.
  376. Everybody else can read this file
  377.  
  378.  
  379.  
  380. ls -l /bin/bash
  381.  
  382.  
  383. Here we can see:
  384.  
  385. The file "/bin/bash" is owned by user "root". The superuser has the right to read, write, and execute this file.
  386. The file is owned by the group "root". Members of the group "root" can also read and execute this file. Everybody else can read and execute this file
  387.  
  388.  
  389. The next command you need to know is "chmod"
  390. rwx rwx rwx = 111 111 111
  391. rw- rw- rw- = 110 110 110
  392. rwx --- --- = 111 000 000
  393.  
  394. and so on...
  395.  
  396. rwx = 111 in binary = 7
  397. rw- = 110 in binary = 6
  398. r-x = 101 in binary = 5
  399. r-- = 100 in binary = 4
  400.  
  401. ls -l one
  402.  
  403. chmod 600 one
  404.  
  405. ls -l one
  406.  
  407. sudo useradd testuser
  408. infosecaddicts
  409.  
  410. sudo passwd testuser
  411.  
  412. testuser
  413. testuser
  414.  
  415. sudo chown testuser one
  416. infosecaddicts
  417.  
  418. ls -l one
  419.  
  420. sudo chgrp testuser one
  421. infosecaddicts
  422.  
  423. ls -l one
  424.  
  425. id
  426.  
  427. su testuser
  428. testuser
  429.  
  430.  
  431. Here is a table of numbers that covers all the common settings. The ones beginning with "7" are used with programs (since they enable execution) and the rest are for other kinds of files.
  432.  
  433. Value Meaning
  434. 777 (rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
  435.  
  436. 755 (rwxr-xr-x) The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
  437.  
  438. 700 (rwx------) The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
  439.  
  440. 666 (rw-rw-rw-) All users may read and write the file.
  441.  
  442. 644 (rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
  443.  
  444. 600 (rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.
  445.  
  446.  
  447.  
  448. Directory permissions
  449. ---------------------
  450. The chmod command can also be used to control the access permissions for directories. In most ways, the permissions scheme for directories works the same way as they do with files. However, the execution permission is used in a different way. It provides control for access to file listing and other things. Here are some useful settings for directories:
  451.  
  452. Value Meaning
  453. 777 (rwxrwxrwx) No restrictions on permissions.
  454. Anybody may list files, create new files in the directory and delete files in the directory.
  455. Generally not a good setting.
  456.  
  457.  
  458.  
  459. 755 (rwxr-xr-x) The directory owner has full access.
  460. All others may list the directory, but cannot create files nor delete them.
  461. This setting is common for directories that you wish to share with other users.
  462.  
  463.  
  464.  
  465. 700 (rwx------) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.
  466.  
  467. ######################
  468. # Process Management #
  469. ######################
  470. top
  471.  
  472. sudo apt install -y htop
  473. infosecaddicts
  474.  
  475. htop
  476.  
  477. ps
  478.  
  479. ps aux
  480.  
  481. ps -A
  482.  
  483. ps -A | less
  484.  
  485. ps axjf
  486.  
  487. pstree
  488.  
  489. pgrep bash
  490.  
  491. pgrep init
  492.  
  493. ps aux | grep apache
  494.  
  495. You can list all of the signals that are possible to send with kill by typing:
  496.  
  497. kill -l
  498.  
  499. sudo kill -HUP pid_of_apache
  500.  
  501. The pkill command works in almost exactly the same way as kill, but it operates on a process name instead:
  502.  
  503. pkill -9 ping
  504. The above command is the equivalent of:
  505.  
  506. kill -9 `pgrep ping`
  507.  
  508.  
  509.  
  510.  
  511.  
  512. ####################
  513. # MD5 Hashing Demo #
  514. ####################
  515. cd ~/LinuxBasics
  516. mkdir hashdemo
  517. cd hashdemo
  518. echo test > test.txt
  519. cat test.txt
  520. md5sum test.txt
  521. echo hello >> test.txt
  522. cat test.txt
  523. md5sum test.txt
  524. cd ..
  525.  
  526.  
  527.  
  528.  
  529. #################################
  530. # Symmetric Key Encryption Demo #
  531. #################################
  532. cd ~/LinuxBasics
  533. mkdir gpgdemo
  534. cd gpgdemo
  535. echo test > test.txt
  536. cat test.txt
  537. gpg -c test.txt
  538. password
  539. password
  540. ls | grep test
  541. cat test.txt
  542. cat test.txt.gpg
  543. rm -rf test.txt
  544. ls | grep test
  545. gpg -o output.txt test.txt.gpg
  546. cat output.txt
  547.  
  548.  
  549. #########################################################################################################################
  550. # Asymmetric Key Encryption Demo #
  551. # #
  552. # Configure random number generator #
  553. # https://www.howtoforge.com/helping-the-random-number-generator-to-gain-enough-entropy-with-rng-tools-debian-lenny #
  554. #########################################################################################################################
  555.  
  556. sudo apt install -y rng-tools
  557. infosecaddicts
  558.  
  559. /etc/init.d/rng-tools start
  560.  
  561. sudo rngd -r /dev/urandom
  562. infosecaddicts
  563.  
  564.  
  565. echo hello > file1.txt
  566. echo goodbye > file2.txt
  567. echo green > file3.txt
  568. echo blue > file4.txt
  569.  
  570. tar czf files.tar.gz *.txt
  571.  
  572. gpg --gen-key
  573. 1
  574. 1024
  575. 0
  576. y
  577. John Doe
  578. --blank comment--
  579. O
  580. password
  581. password
  582.  
  583.  
  584.  
  585. gpg --armor --output file-enc-pubkey.txt --export 'John Doe'
  586.  
  587. cat file-enc-pubkey.txt
  588.  
  589. gpg --armor --output file-enc-privkey.asc --export-secret-keys 'John Doe'
  590.  
  591. cat file-enc-privkey.asc
  592.  
  593. gpg --encrypt --recipient 'John Doe' files.tar.gz
  594.  
  595. rm -rf files.tar.gz *.txt
  596.  
  597. ls
  598.  
  599. tar -zxvf files.tar.gz.gpg
  600.  
  601. gpg --output output.tar.gz --decrypt files.tar.gz.gpg
  602. password
  603.  
  604. tar -zxvf output.tar.gz
  605.  
  606. ls
  607.  
  608.  
  609.  
  610.  
  611.  
  612. ############################
  613. # Encryption using OpenSSL #
  614. ############################
  615. openssl genrsa -out private_key.pem 1024
  616. openssl rsa -in private_key.pem -out public_key.pem -outform PEM -pubout
  617.  
  618.  
  619. echo hello > encrypt.txt
  620. openssl rsautl -encrypt -inkey public_key.pem -pubin -in encrypt.txt -out encrypt.dat
  621.  
  622. cat encrypt.dat
  623.  
  624. rm -rf encrypt.txt
  625.  
  626. ls
  627.  
  628. openssl rsautl -decrypt -inkey private_key.pem -in encrypt.dat -out decrypt.txt
  629.  
  630. cat decrypt.txt
  631.  
  632.  
  633.  
  634. ###############################
  635. # Secure File/Folder Deletion #
  636. ###############################
  637. sudo apt install -y secure-delete wipe
  638.  
  639. wget https://www.sans.org/security-resources/tcpip.pdf
  640.  
  641. file tcpip.pdf
  642.  
  643. sudo srm tcpip.pdf
  644.  
  645. wget https://www.sans.org/security-resources/tcpip.pdf
  646.  
  647. shred tcpip.pdf
  648.  
  649. wget https://www.sans.org/security-resources/tcpip.pdf
  650.  
  651. wipe tcpip.pdf
  652.  
  653.  
  654.  
  655.  
  656.  
  657. #################
  658. # IPTables Demo #
  659. #################
  660. cd ~
  661.  
  662. - Delete Existing Rules
  663. ---------------------
  664. sudo /sbin/iptables -F
  665. infosecaddicts
  666.  
  667. (or)
  668.  
  669. sudo /sbin/iptables --flush
  670. infosecaddicts
  671.  
  672.  
  673.  
  674. - Set Default Chain Policies
  675. --------------------------
  676. iptables -P INPUT DROP
  677. iptables -P FORWARD DROP
  678. iptables -P OUTPUT DROP
  679.  
  680.  
  681.  
  682. - Delete Existing Rules
  683. ---------------------
  684. sudo /sbin/iptables -F
  685. infosecaddicts
  686.  
  687. (or)
  688.  
  689. sudo /sbin/iptables --flush
  690. infosecaddicts
  691.  
  692.  
  693.  
  694.  
  695. sudo /bin/bash
  696.  
  697.  
  698.  
  699. - Block a Specific ip-address
  700. -----------------------------
  701. BLOCK_THIS_IP="1.2.3.4"
  702. iptables -A INPUT -s "$BLOCK_THIS_IP" -j DROP
  703.  
  704.  
  705. iptables -A INPUT -i eth0 -s "$BLOCK_THIS_IP" -j DROP
  706. iptables -A INPUT -i eth0 -p tcp -s "$BLOCK_THIS_IP" -j DROP
  707.  
  708.  
  709. - Allow ALL Incoming SSH
  710. ------------------------
  711. iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
  712. iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
  713.  
  714.  
  715. - Allow Incoming SSH only from a Sepcific Network
  716. -------------------------------------------------
  717. iptables -A INPUT -i eth0 -p tcp -s 192.168.100.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
  718. iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
  719.  
  720.  
  721. - Allow Incoming HTTP and HTTPS
  722. -------------------------------
  723. iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
  724. iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
  725.  
  726.  
  727. iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
  728. iptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
  729.  
  730.  
  731.  
  732. - Combine Multiple Rules Together using MultiPorts
  733. --------------------------------------------------
  734. iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPT
  735. iptables -A OUTPUT -o eth0 -p tcp -m multiport --sports 22,80,443 -m state --state ESTABLISHED -j ACCEPT
  736.  
  737.  
  738. - Allow Outgoing SSH
  739. --------------------
  740. iptables -A OUTPUT -o eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
  741. iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
  742.  
  743.  
  744. - Allow Outgoing SSH only to a Specific Network
  745. -----------------------------------------------
  746. The following rules allow outgoing ssh connection only to a specific network. i.e You an ssh only to 192.168.100.0/24 network from the inside.
  747.  
  748. iptables -A OUTPUT -o eth0 -p tcp -d 192.168.100.0/24 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
  749. iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
  750.  
  751.  
  752.  
  753. - Allow Outgoing HTTPS
  754. ----------------------
  755. The following rules allow outgoing secure web traffic. This is helpful when you want to allow internet traffic for your users. On servers, these rules are also helpful when you want to use wget to download some files from outside.
  756.  
  757. iptables -A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
  758. iptables -A INPUT -i eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
  759.  
  760.  
  761.  
  762.  
  763. Load Balance Incoming Web Traffic
  764. ---------------------------------
  765. You can also load balance your incoming web traffic using iptables firewall rules.
  766.  
  767. This uses the iptables nth extension. The following example load balances the HTTPS traffic to three different ip-address. For every 3th packet, it is load balanced to the appropriate server (using the counter 0).
  768.  
  769. iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 0 -j DNAT --to-destination 192.168.1.101:443
  770. iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 1 -j DNAT --to-destination 192.168.1.102:443
  771. iptables -A PREROUTING -i eth0 -p tcp --dport 443 -m state --state NEW -m nth --counter 0 --every 3 --packet 2 -j DNAT --to-destination 192.168.1.103:443
  772.  
  773.  
  774.  
  775. Allow Ping from Outside to Inside
  776. ---------------------------------
  777. The following rules allow outside users to be able to ping your servers.
  778.  
  779. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  780. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  781.  
  782.  
  783.  
  784. Allow Ping from Inside to Outside
  785. ---------------------------------
  786. The following rules allow you to ping from inside to any of the outside servers.
  787.  
  788. iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
  789. iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
  790.  
  791.  
  792.  
  793. Allow Loopback Access
  794. ---------------------
  795. You should allow full loopback access on your servers. i.e access using 127.0.0.1
  796.  
  797. iptables -A INPUT -i lo -j ACCEPT
  798. iptables -A OUTPUT -o lo -j ACCEPT
  799.  
  800.  
  801.  
  802. Allow Internal Network to External network
  803. ------------------------------------------
  804. On the firewall server where one ethernet card is connected to the external, and another ethernet card connected to the internal servers, use the following rules to allow internal network talk to external network.
  805.  
  806. In this example, eth1 is connected to external network (internet), and eth0 is connected to internal network (For example: 192.168.1.x).
  807.  
  808. iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
  809.  
  810.  
  811.  
  812. Allow outbound DNS
  813. ------------------
  814. The following rules allow outgoing DNS connections.
  815.  
  816. iptables -A OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT
  817. iptables -A INPUT -p udp -i eth0 --sport 53 -j ACCEPT
  818.  
  819.  
  820.  
  821. Allow Rsync From a Specific Network
  822. -----------------------------------
  823. The following rules allows rsync only from a specific network.
  824.  
  825. iptables -A INPUT -i eth0 -p tcp -s 192.168.101.0/24 --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT
  826. iptables -A OUTPUT -o eth0 -p tcp --sport 873 -m state --state ESTABLISHED -j ACCEPT
  827.  
  828.  
  829.  
  830. Allow MySQL connection only from a specific network
  831. ---------------------------------------------------
  832. If you are running MySQL, typically you don’t want to allow direct connection from outside. In most cases, you might have web server running on the same server where the MySQL database runs.
  833.  
  834. However DBA and developers might need to login directly to the MySQL from their laptop and desktop using MySQL client. In those case, you might want to allow your internal network to talk to the MySQL directly as shown below.
  835.  
  836. iptables -A INPUT -i eth0 -p tcp -s 192.168.100.0/24 --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
  837. iptables -A OUTPUT -o eth0 -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
  838.  
  839.  
  840.  
  841. Allow Sendmail or Postfix Traffic
  842. ---------------------------------
  843. The following rules allow mail traffic. It may be sendmail or postfix.
  844.  
  845. iptables -A INPUT -i eth0 -p tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT
  846. iptables -A OUTPUT -o eth0 -p tcp --sport 25 -m state --state ESTABLISHED -j ACCEPT
  847.  
  848.  
  849. Allow IMAP and IMAPS
  850. --------------------
  851. The following rules allow IMAP/IMAP2 traffic.
  852.  
  853. iptables -A INPUT -i eth0 -p tcp --dport 143 -m state --state NEW,ESTABLISHED -j ACCEPT
  854. iptables -A OUTPUT -o eth0 -p tcp --sport 143 -m state --state ESTABLISHED -j ACCEPT
  855.  
  856.  
  857. The following rules allow IMAPS traffic.
  858.  
  859. iptables -A INPUT -i eth0 -p tcp --dport 993 -m state --state NEW,ESTABLISHED -j ACCEPT
  860. iptables -A OUTPUT -o eth0 -p tcp --sport 993 -m state --state ESTABLISHED -j ACCEPT
  861.  
  862.  
  863.  
  864.  
  865. Allow POP3 and POP3S
  866. --------------------
  867. The following rules allow POP3 access.
  868.  
  869. iptables -A INPUT -i eth0 -p tcp --dport 110 -m state --state NEW,ESTABLISHED -j ACCEPT
  870. iptables -A OUTPUT -o eth0 -p tcp --sport 110 -m state --state ESTABLISHED -j ACCEPT
  871. The following rules allow POP3S access.
  872.  
  873. iptables -A INPUT -i eth0 -p tcp --dport 995 -m state --state NEW,ESTABLISHED -j ACCEPT
  874. iptables -A OUTPUT -o eth0 -p tcp --sport 995 -m state --state ESTABLISHED -j ACCEPT
  875.  
  876.  
  877.  
  878. Port Forwarding
  879. ---------------
  880. The following example routes all traffic that comes to the port 442 to 22. This means that the incoming ssh connection can come from both port 22 and 422.
  881.  
  882. iptables -t nat -A PREROUTING -p tcp -d 192.168.102.37 --dport 422 -j DNAT --to 192.168.102.37:22
  883.  
  884. If you do the above, you also need to explicitly allow incoming connection on the port 422.
  885.  
  886. iptables -A INPUT -i eth0 -p tcp --dport 422 -m state --state NEW,ESTABLISHED -j ACCEPT
  887. iptables -A OUTPUT -o eth0 -p tcp --sport 422 -m state --state ESTABLISHED -j ACCEPT
  888.  
  889.  
  890. Log Dropped Packets
  891. -------------------
  892. You might also want to log all the dropped packets. These rules should be at the bottom.
  893.  
  894. First, create a new chain called LOGGING.
  895.  
  896. iptables -N LOGGING
  897. Next, make sure all the remaining incoming connections jump to the LOGGING chain as shown below.
  898.  
  899. iptables -A INPUT -j LOGGING
  900. Next, log these packets by specifying a custom “log-prefix”.
  901.  
  902. iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables Packet Dropped: " --log-level 7
  903. Finally, drop these packets.
  904.  
  905. iptables -A LOGGING -j DROP
  906.  
  907.  
  908.  
  909. #########################
  910. # Ubuntu Perfect Server #
  911. #########################
  912.  
  913. Reference:
  914. https://www.howtoforge.com/tutorial/ubuntu-perfect-server-with-apache-php-myqsl-pureftpd-bind-postfix-doveot-and-ispconfig/
  915.  
  916.  
  917. deb http://de.archive.ubuntu.com/ubuntu/ yakkety main restricted
  918. deb http://de.archive.ubuntu.com/ubuntu/ yakkety-updates main restricted
  919. deb http://de.archive.ubuntu.com/ubuntu/ yakkety universe
  920. deb http://de.archive.ubuntu.com/ubuntu/ yakkety-updates universe
  921. deb http://de.archive.ubuntu.com/ubuntu/ yakkety-updates multiverse
  922.  
  923.  
  924. Then run
  925. apt-get update
  926.  
  927. to update the apt package database and
  928. apt-get upgrade
  929.  
  930. to install the latest updates (if there are any). If you see that a new kernel gets installed as part of the updates, you should reboot the system afterwards:
  931. reboot
  932.  
  933. Change the Default Shell
  934. /bin/sh is a symlink to /bin/dash, however we need /bin/bash, not /bin/dash. Therefore, we do this:
  935. dpkg-reconfigure dash
  936.  
  937. Use dash as the default system shell (/bin/sh)? <-- No
  938.  
  939. Disable AppArmor
  940. ----------------
  941. AppArmor is a security extension (similar to SELinux) that should provide extended security. In my opinion, you don't need it to configure a secure system, and it usually causes more problems than advantages (think of it after you have done a week of trouble-shooting because some service wasn't working as expected, and then you find out that everything was ok, only AppArmor was causing the problem). Therefore, I disable it (this is a must if you want to install ISPConfig later on).
  942. We can disable it like this:
  943.  
  944. service apparmor stop
  945. update-rc.d -f apparmor remove
  946. apt-get remove apparmor apparmor-utils
  947.  
  948.  
  949. apt-get -y install ntp ntpdate
  950.  
  951. Install Postfix, Dovecot, MariaDB, rkhunter and binutils
  952. --------------------------------------------------------
  953. For installing postfix, we need to ensure that sendmail is not installed and running. To stop and remove sendmail run this command:
  954.  
  955. service sendmail stop; update-rc.d -f sendmail remove
  956.  
  957.  
  958.  
  959.  
  960. #######################
  961. # Hardening Ubuntu 16 #
  962. #######################
  963.  
  964. This guide is intended as a relatively easy step by step guide to:
  965.  
  966. Harden the security on an Ubuntu 16.04 LTS server by installing and configuring the following:
  967.  
  968. Install and configure Firewall - ufw
  969. Secure shared memory - fstab
  970. SSH - Key based login, disable root login and change port
  971. Apache SSL - Disable SSL v3 support
  972. Protect su by limiting access only to admin group
  973. Harden network with sysctl settings
  974. Disable Open DNS Recursion and Remove Version Info - Bind9 DNS
  975. Prevent IP Spoofing
  976. Harden PHP for security
  977. Restrict Apache Information Leakage
  978. Install and configure Apache application firewall - ModSecurity
  979. Protect from DDOS (Denial of Service) attacks with ModEvasive
  980. Scan logs and ban suspicious hosts - DenyHosts and Fail2Ban
  981. Intrusion Detection - PSAD
  982. Check for RootKits - RKHunter and CHKRootKit
  983. Scan open Ports - Nmap
  984. Analyse system LOG files - LogWatch
  985. Apparmor - Application Armor
  986. Audit your system security - Tiger and Tripwire
  987. Requirements:
  988.  
  989. Ubuntu 16.04 LTS or later server with a standard LAMP stack installed.
  990. 1. Firewall - UFW
  991.  
  992. A good place to start is to install a Firewall.
  993. UFW - Uncomplicated Firewall is a basic firewall that works very well and easy to configure with its Firewall configuration tool - gufw, or use Shorewall, fwbuilder, or Firestarter.
  994. Use Firestarter GUI to configure your firewall or refer to the Ubuntu Server Guide, UFW manual pages or the Ubuntu UFW community documentation.
  995. Install UFW and enable, open a terminal window and enter :
  996. sudo apt-get install ufw
  997. Allow SSH and Http services.
  998. sudo ufw allow ssh
  999. sudo ufw allow http
  1000. Enable the firewall.
  1001. sudo ufw enable
  1002. Check the status of the firewall.
  1003. sudo ufw status verbose
  1004. 2. Secure shared memory.
  1005.  
  1006. Shared memory can be used in an attack against a running service. Modify /etc/fstab to make it more secure.
  1007. Open a Terminal Window and enter the following :
  1008. sudo vi /etc/fstab
  1009. Add the following line and save. You will need to reboot for this setting to take effect :
  1010. Note : This only is works in Ubuntu 12.10 or later - For earlier Ubuntu versions replace /run/shm with /dev/shm
  1011. Save and Reboot when done
  1012. tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0
  1013. 3. SSH Hardening - key based login, disable root login and change port.
  1014.  
  1015. The best way to secure SSH is to use public/private key based login. See SSH/OpenSSH/Keys
  1016. If you have to use password authentication, the easiest way to secure SSH is to disable root login and change the SSH port to something different than the standard port 22.
  1017. Before disabling the root login create a new SSH user and make sure the user belongs to the admin group (see step 4. below regarding the admin group).
  1018. if you change the SSH port keep the port number below 1024 as these are priviledged ports that can only be opened by root or processes running as root.
  1019. If you change the SSH port also open the new port you have chosen on the firewall and close port 22.
  1020. Open a Terminal Window and enter :
  1021. sudo vi /etc/ssh/sshd_config
  1022. Change or add the following and save.
  1023. Port <ENTER YOUR PORT>
  1024. Protocol 2
  1025. PermitRootLogin no
  1026. DebianBanner no
  1027. Restart SSH server, open a Terminal Window and enter :
  1028. sudo service ssh restart
  1029. 4. Apache SSL Hardening - disable SSL v2/v3 support.
  1030.  
  1031. The SSL v2/v3 protocol has been proven to be insecure.
  1032. We will disable Apache support for the protocol and force the use of the newer protocols.
  1033. Open a Terminal Window and enter :
  1034. sudo vi /etc/apache2/mods-available/ssl.conf
  1035. Change this line from :
  1036. SSLProtocol all -SSLv3
  1037. To the following and save.
  1038. SSLProtocol all -SSLv2 -SSLv3
  1039. Restart the Apache server, open a Terminal Window and enter :
  1040. sudo service apache2 restart
  1041. 5. Protect su by limiting access only to admin group.
  1042.  
  1043. To limit the use of su by admin users only we need to create an admin group, then add users and limit the use of su to the admin group.
  1044. Add a admin group to the system and add your own admin username to the group by replacing <YOUR ADMIN USERNAME> below with your admin username.
  1045. Open a terminal window and enter:
  1046. sudo groupadd admin
  1047. sudo usermod -a -G admin <YOUR ADMIN USERNAME>
  1048. sudo dpkg-statoverride --update --add root admin 4750 /bin/su
  1049. 6. Harden network with sysctl settings.
  1050.  
  1051. The /etc/sysctl.conf file contain all the sysctl settings.
  1052. Prevent source routing of incoming packets and log malformed IP's enter the following in a terminal window:
  1053. sudo vi /etc/sysctl.conf
  1054. Edit the /etc/sysctl.conf file and un-comment or add the following lines :
  1055. # IP Spoofing protection
  1056. net.ipv4.conf.all.rp_filter = 1
  1057. net.ipv4.conf.default.rp_filter = 1
  1058.  
  1059. # Ignore ICMP broadcast requests
  1060. net.ipv4.icmp_echo_ignore_broadcasts = 1
  1061.  
  1062. # Disable source packet routing
  1063. net.ipv4.conf.all.accept_source_route = 0
  1064. net.ipv6.conf.all.accept_source_route = 0
  1065. net.ipv4.conf.default.accept_source_route = 0
  1066. net.ipv6.conf.default.accept_source_route = 0
  1067.  
  1068. # Ignore send redirects
  1069. net.ipv4.conf.all.send_redirects = 0
  1070. net.ipv4.conf.default.send_redirects = 0
  1071.  
  1072. # Block SYN attacks
  1073. net.ipv4.tcp_syncookies = 1
  1074. net.ipv4.tcp_max_syn_backlog = 2048
  1075. net.ipv4.tcp_synack_retries = 2
  1076. net.ipv4.tcp_syn_retries = 5
  1077.  
  1078. # Log Martians
  1079. net.ipv4.conf.all.log_martians = 1
  1080. net.ipv4.icmp_ignore_bogus_error_responses = 1
  1081.  
  1082. # Ignore ICMP redirects
  1083. net.ipv4.conf.all.accept_redirects = 0
  1084. net.ipv6.conf.all.accept_redirects = 0
  1085. net.ipv4.conf.default.accept_redirects = 0
  1086. net.ipv6.conf.default.accept_redirects = 0
  1087.  
  1088. # Ignore Directed pings
  1089. net.ipv4.icmp_echo_ignore_all = 1
  1090. To reload sysctl with the latest changes, enter:
  1091. sudo sysctl -p
  1092. 7. Disable Open DNS Recursion and Remove Version Info - BIND DNS Server.
  1093.  
  1094. Open a Terminal and enter the following :
  1095. sudo vi /etc/bind/named.conf.options
  1096. Add the following to the Options section :
  1097. recursion no;
  1098. version "Not Disclosed";
  1099. Restart BIND DNS server. Open a Terminal and enter the following :
  1100. sudo service bind9 restart
  1101. 8. Prevent IP Spoofing.
  1102.  
  1103. Open a Terminal and enter the following :
  1104. sudo vi /etc/host.conf
  1105. Add or edit the following lines :
  1106. order bind,hosts
  1107. nospoof on
  1108. 9. Harden PHP for security.
  1109.  
  1110. Edit the php.ini file :
  1111. sudo vi /etc/php5/apache2/php.ini
  1112. Add or edit the following lines an save :
  1113. disable_functions = exec,system,shell_exec,passthru
  1114. register_globals = Off
  1115. expose_php = Off
  1116. display_errors = Off
  1117. track_errors = Off
  1118. html_errors = Off
  1119. magic_quotes_gpc = Off
  1120. mail.add_x_header = Off
  1121. session.name = NEWSESSID
  1122. Restart Apache server. Open a Terminal and enter the following :
  1123. sudo service apache2 restart
  1124. 10. Restrict Apache Information Leakage.
  1125.  
  1126. Edit the Apache2 configuration security file :
  1127. sudo vi /etc/apache2/conf-available/security.conf
  1128. Add or edit the following lines and save :
  1129. ServerTokens Prod
  1130. ServerSignature Off
  1131. TraceEnable Off
  1132. Header unset ETag
  1133. Header always unset X-Powered-By
  1134. FileETag None
  1135. Restart Apache server. Open a Terminal and enter the following :
  1136. sudo service apache2 restart
  1137. 11. Web Application Firewall - ModSecurity.
  1138.  
  1139. See : How to install apache2 mod_security and mod_evasive on Ubuntu 12.04 LTS server
  1140. 12. Protect from DDOS (Denial of Service) attacks - ModEvasive
  1141.  
  1142. See : How to install apache2 mod_security and mod_evasive on Ubuntu 12.04 LTS server
  1143. 13. Scan logs and ban suspicious hosts - DenyHosts and Fail2Ban.
  1144.  
  1145. DenyHosts is a python program that automatically blocks SSH attacks by adding entries to /etc/hosts.deny. DenyHosts will also inform Linux administrators about offending hosts, attacked users and suspicious logins.
  1146. Open a Terminal and enter the following :
  1147. sudo apt-get install denyhosts
  1148. After installation edit the configuration file /etc/denyhosts.conf and change the email, and other settings as required.
  1149. To edit the admin email settings open a terminal window and enter:
  1150. sudo vi /etc/denyhosts.conf
  1151. Change the following values as required on your server :
  1152. ADMIN_EMAIL = root@localhost
  1153. SMTP_HOST = localhost
  1154. SMTP_PORT = 25
  1155. #SMTP_USERNAME=foo
  1156. #SMTP_PASSWORD=bar
  1157. SMTP_FROM = DenyHosts nobody@localhost
  1158. #SYSLOG_REPORT=YES
  1159. Fail2ban is more advanced than DenyHosts as it extends the log monitoring to other services including SSH, Apache, Courier, FTP, and more.
  1160. Fail2ban scans log files and bans IPs that show the malicious signs -- too many password failures, seeking for exploits, etc.
  1161. Generally Fail2Ban then used to update firewall rules to reject the IP addresses for a specified amount of time, although any arbitrary other action could also be configured.
  1162. Out of the box Fail2Ban comes with filters for various services (apache, courier, ftp, ssh, etc).
  1163. Open a Terminal and enter the following :
  1164. sudo apt-get install fail2ban
  1165. After installation edit the configuration file /etc/fail2ban/jail.local and create the filter rules as required.
  1166. To edit the settings open a terminal window and enter:
  1167. sudo vi /etc/fail2ban/jail.conf
  1168. Activate all the services you would like fail2ban to monitor by changing enabled = false to enabled = true
  1169. For example if you would like to enable the SSH monitoring and banning jail, find the line below and change enabled from false to true. Thats it.
  1170. [sshd]
  1171.  
  1172. enabled = true
  1173. port = ssh
  1174. filter = sshd
  1175. logpath = /var/log/auth.log
  1176. maxretry = 3
  1177. If you have selected a non-standard SSH port in step 3 then you need to change the port setting in fail2ban from ssh which by default is port 22, to your new port number, for example if you have chosen 1234 then port = 1234
  1178. [sshd]
  1179.  
  1180. enabled = true
  1181. port = <ENTER YOUR SSH PORT NUMBER HERE>
  1182. filter = sshd
  1183. logpath = /var/log/auth.log
  1184. maxretry = 3
  1185. If you would like to receive emails from Fail2Ban if hosts are banned change the following line to your email address.
  1186. destemail = root@localhost
  1187. and change the following line from :
  1188. action = %(action_)s
  1189. to:
  1190. action = %(action_mwl)s
  1191. You can also create rule filters for the various services that you would like fail2ban to monitor that is not supplied by default.
  1192. sudo vi /etc/fail2ban/jail.local
  1193. Good instructions on how to configure fail2ban and create the various filters can be found on HowtoForge - click here for an example
  1194. When done with the configuration of Fail2Ban restart the service with :
  1195. sudo service fail2ban restart
  1196. You can also check the status with.
  1197. sudo fail2ban-client status
  1198. 14. Intrusion Detection - PSAD.
  1199.  
  1200. Cipherdyne PSAD is a collection of three lightweight system daemons that run on Linux machines and analyze iptables log messages to detect port scans and other suspicious traffic.
  1201. To install the latest version from the source files follow these instruction : How to install PSAD Intrusion Detection on Ubuntu 12.04 LTS server
  1202. OR install the older version from the Ubuntu software repositories, open a Terminal and enter the following :
  1203. sudo apt-get install psad
  1204. Then for basic configuration see How to install PSAD Intrusion Detection on Ubuntu 12.04 LTS server and follow from step 2:
  1205. 15. Check for rootkits - RKHunter and CHKRootKit.
  1206.  
  1207. Both RKHunter and CHKRootkit basically do the same thing - check your system for rootkits. No harm in using both.
  1208. Open a Terminal and enter the following :
  1209. sudo apt-get install rkhunter chkrootkit
  1210. To run chkrootkit open a terminal window and enter :
  1211. sudo chkrootkit
  1212. To update and run RKHunter. Open a Terminal and enter the following :
  1213. sudo rkhunter --update
  1214. sudo rkhunter --propupd
  1215. sudo rkhunter --check
  1216. 16. Scan open ports - Nmap.
  1217.  
  1218. Nmap ("Network Mapper") is a free and open source utility for network discovery and security auditing.
  1219. Open a Terminal and enter the following :
  1220. sudo apt-get install nmap
  1221. Scan your system for open ports with :
  1222. nmap -v -sT localhost
  1223. SYN scanning with the following :
  1224. sudo nmap -v -sS localhost
  1225. 17. Analyse system LOG files - LogWatch.
  1226.  
  1227. Logwatch is a customizable log analysis system. Logwatch parses through your system's logs and creates a report analyzing areas that you specify. Logwatch is easy to use and will work right out of the package on most systems.
  1228. Open a Terminal and enter the following :
  1229. sudo apt-get install logwatch libdate-manip-perl
  1230. To view logwatch output use less :
  1231. sudo logwatch | less
  1232. To email a logwatch report for the past 7 days to an email address, enter the following and replace [email protected] with the required email. :
  1233. sudo logwatch --mailto [email protected] --output mail --format html --range 'between -7 days and today'
  1234. 18. Apparmor - Application Armor.
  1235.  
  1236. More information can be found here. Ubuntu Server Guide - Apparmor
  1237. It is installed by default since Ubuntu 7.04.
  1238. Open a Terminal and enter the following :
  1239. sudo apt-get install apparmor apparmor-profiles
  1240. Check to see if things are running :
  1241. sudo apparmor_status
  1242. 19. Audit your system security - Tiger and Tripwire.
  1243.  
  1244. Tiger is a security tool that can be use both as a security audit and intrusion detection system.
  1245. Tripwire is a host-based intrusion detection system (HIDS) that checks file and folder integrity.
  1246. Open a Terminal and enter the following :
  1247. sudo apt-get install tiger tripwire
  1248. To setup Tripwire good installation guides can be found on Digital Ocean here and on Unixmen here
  1249. To run tiger enter :
  1250. sudo tiger
  1251. All Tiger output can be found in the /var/log/tiger
  1252. To view the tiger security reports, open a Terminal and enter the following :
  1253. sudo less /var/log/tiger/security.report.*
  1254.  
  1255. ##############################################
  1256. # Log Analysis with Linux command-line tools #
  1257. ##############################################
  1258. - The following command line executables are found in the Mac as well as most Linux Distributions.
  1259.  
  1260. cat – prints the content of a file in the terminal window
  1261. grep – searches and filters based on patterns
  1262. awk – can sort each row into fields and display only what is needed
  1263. sed – performs find and replace functions
  1264. sort – arranges output in an order
  1265. uniq – compares adjacent lines and can report, filter or provide a count of duplicates
  1266.  
  1267.  
  1268.  
  1269. ###############
  1270. # Apache Logs #
  1271. ###############
  1272.  
  1273. Reference:
  1274. http://www.the-art-of-web.com/system/logs/
  1275.  
  1276. wget https://s3.amazonaws.com/SecureNinja/Python/access_log
  1277.  
  1278.  
  1279. - You want to list all user agents ordered by the number of times they appear (descending order):
  1280.  
  1281. awk -F\" '{print $6}' access_log | sort | uniq -c | sort -fr
  1282.  
  1283.  
  1284.  
  1285. - Using the default separator which is any white-space (spaces or tabs) we get the following:
  1286.  
  1287. awk '{print $1}' access_log # ip address (%h)
  1288. awk '{print $2}' access_log # RFC 1413 identity (%l)
  1289. awk '{print $3}' access_log # userid (%u)
  1290. awk '{print $4,5}' access_log # date/time (%t)
  1291. awk '{print $9}' access_log # status code (%>s)
  1292. awk '{print $10}' access_log # size (%b)
  1293.  
  1294. - 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:
  1295.  
  1296. awk -F\" '{print $2}' access_log # request line (%r)
  1297. awk -F\" '{print $4}' access_log # referer
  1298. awk -F\" '{print $6}' access_log # user agent
  1299.  
  1300.  
  1301. awk -F\" '{print $6}' access_log \
  1302. | sed 's/(\([^;]\+; [^;]\+\)[^)]*)/(\1)/' \
  1303. | sort | uniq -c | sort -fr
  1304.  
  1305.  
  1306. - 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?
  1307.  
  1308. awk -F\" '($6 ~ /Googlebot/){print $2}' access_log | awk '{print $2}'
  1309. Or who's been looking at your guestbook?
  1310.  
  1311. awk -F\" '($2 ~ /guestbook\.html/){print $6}' access_log
  1312.  
  1313.  
  1314. Reference:
  1315. https://blog.nexcess.net/2011/01/21/one-liners-for-apache-log-files/
  1316.  
  1317. # top 20 URLs from the last 5000 hits
  1318. tail -5000 ./access_log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  1319. tail -5000 ./access_log | awk '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1320.  
  1321. # top 20 URLS excluding POST data from the last 5000 hits
  1322. tail -5000 ./access_log | awk -F"[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  1323. tail -5000 ./access_log | awk -F"[ ?]" '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1324.  
  1325. # top 20 IPs from the last 5000 hits
  1326. tail -5000 ./access_log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
  1327. tail -5000 ./access_log | awk '{freq[$1]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
  1328.  
  1329. # top 20 URLs requested from a certain ip from the last 5000 hits
  1330. IP=1.2.3.4; tail -5000 ./access_log | grep $IP | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
  1331. 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
  1332.  
  1333. # top 20 URLS requested from a certain ip excluding, excluding POST data, from the last 5000 hits
  1334. IP=1.2.3.4; tail -5000 ./access_log | fgrep $IP | awk -F "[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
  1335. 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
  1336.  
  1337. # top 20 referrers from the last 5000 hits
  1338. tail -5000 ./access_log | awk '{print $11}' | tr -d '"' | sort | uniq -c | sort -rn | head -20
  1339. tail -5000 ./access_log | awk '{freq[$11]++} END {for (x in freq) {print freq[x], x}}' | tr -d '"' | sort -rn | head -20
  1340.  
  1341. # top 20 user agents from the last 5000 hits
  1342. tail -5000 ./access_log | cut -d\ -f12- | sort | uniq -c | sort -rn | head -20
  1343.  
  1344. # sum of data (in MB) transferred in the last 5000 hits
  1345. tail -5000 ./access_log | awk '{sum+=$10} END {print sum/1048576}'
  1346.  
  1347.  
  1348. ##############
  1349. # Cisco Logs #
  1350. ##############
  1351.  
  1352. wget https://s3.amazonaws.com/StrategicSec-Files/LogAnalysis/cisco.log
  1353.  
  1354.  
  1355. AWK Basics
  1356. ----------
  1357. - 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.
  1358.  
  1359. cat cisco.log | awk '{print $5}' | tail -n 4
  1360.  
  1361.  
  1362.  
  1363.  
  1364. - 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.
  1365.  
  1366. cat cisco.log | awk '{print $5}'| sort | uniq -c | sort -rn
  1367.  
  1368.  
  1369.  
  1370.  
  1371. - 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”.
  1372.  
  1373. cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk '{print $5}' | sort | uniq -c | sort -rn
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379. - 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.
  1380.  
  1381. cat cisco.log | grep %LINEPROTO-5-UPDOWN:
  1382.  
  1383. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| awk '{print $10}' | sort | uniq -c | sort -rn
  1384.  
  1385. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10}' | sort | uniq -c | sort -rn
  1386.  
  1387. cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10 " changed to " $14}' | sort | uniq -c | sort -rn
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397. ##############################
  1398. # Linux For InfoSec Homework #
  1399. ##############################
  1400. In order to receive your certificate of attendance you must complete the all of the quizzes on the http://linuxsurvival.com/linux-tutorial-introduction/ website.
  1401.  
  1402.  
  1403. Submit the results via email in an MS Word document with (naming convention example: YourFirstName-YourLastName-Linux-For-InfoSec-Homework.docx)
  1404.  
  1405.  
  1406.  
  1407.  
  1408. ##############################
  1409. # Linux For InfoSe Challenge #
  1410. ##############################
  1411.  
  1412. In order to receive your certificate of proficiency you must complete all of the tasks covered in the Linux For InfoSec pastebin (http://pastebin.com/b5SxBRf6).
  1413.  
  1414. Submit the results via email in an MS Word document with (naming convention example: YourFirstName-YourLastName-Linux-For-InfoSec-Challenge.docx)
  1415.  
  1416.  
  1417.  
  1418.  
  1419. IMPORTANT NOTE:
  1420. Your homework/challenge must be submitted via email to both (joe-at-strategicsec-.-com and kasheia-at-strategicsec-.-com) by Sunday October 16th at midnight EST.
  1421.  
  1422.  
  1423. #########################################################################
  1424. # What kind of Linux am I on and how can I find out? #
  1425. # Great reference: #
  1426. # https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/ #
  1427. #########################################################################
  1428. - What’s the distribution type? What version?
  1429. -------------------------------------------
  1430. cat /etc/issue
  1431. cat /etc/*-release
  1432. cat /etc/lsb-release # Debian based
  1433. cat /etc/redhat-release # Redhat based
  1434.  
  1435.  
  1436.  
  1437. - What’s the kernel version? Is it 64-bit?
  1438. -------------------------------------------
  1439. cat /proc/version
  1440. uname -a
  1441. uname -mrs
  1442. rpm -q kernel
  1443. dmesg | grep Linux
  1444. ls /boot | grep vmlinuz-
  1445.  
  1446.  
  1447.  
  1448. - What can be learnt from the environmental variables?
  1449. ----------------------------------------------------
  1450. cat /etc/profile
  1451. cat /etc/bashrc
  1452. cat ~/.bash_profile
  1453. cat ~/.bashrc
  1454. cat ~/.bash_logout
  1455. env
  1456. set
  1457.  
  1458.  
  1459. - What services are running? Which service has which user privilege?
  1460. ------------------------------------------------------------------
  1461. ps aux
  1462. ps -ef
  1463. top
  1464. cat /etc/services
  1465.  
  1466.  
  1467. - Which service(s) are been running by root? Of these services, which are vulnerable - it’s worth a double check!
  1468. ---------------------------------------------------------------------------------------------------------------
  1469. ps aux | grep root
  1470. ps -ef | grep root
  1471.  
  1472.  
  1473.  
  1474. - What applications are installed? What version are they? Are they currently running?
  1475. ------------------------------------------------------------------------------------
  1476. ls -alh /usr/bin/
  1477. ls -alh /sbin/
  1478. dpkg -l
  1479. rpm -qa
  1480. ls -alh /var/cache/apt/archivesO
  1481. ls -alh /var/cache/yum/
  1482.  
  1483.  
  1484. - Any of the service(s) settings misconfigured? Are any (vulnerable) plugins attached?
  1485. ------------------------------------------------------------------------------------
  1486. cat /etc/syslog.conf
  1487. cat /etc/chttp.conf
  1488. cat /etc/lighttpd.conf
  1489. cat /etc/cups/cupsd.conf
  1490. cat /etc/inetd.conf
  1491. cat /etc/apache2/apache2.conf
  1492. cat /etc/my.conf
  1493. cat /etc/httpd/conf/httpd.conf
  1494. cat /opt/lampp/etc/httpd.conf
  1495. ls -aRl /etc/ | awk '$1 ~ /^.*r.*/
  1496.  
  1497.  
  1498.  
  1499. - What jobs are scheduled?
  1500. ------------------------
  1501. crontab -l
  1502. ls -alh /var/spool/cron
  1503. ls -al /etc/ | grep cron
  1504. ls -al /etc/cron*
  1505. cat /etc/cron*
  1506. cat /etc/at.allow
  1507. cat /etc/at.deny
  1508. cat /etc/cron.allow
  1509. cat /etc/cron.deny
  1510. cat /etc/crontab
  1511. cat /etc/anacrontab
  1512. cat /var/spool/cron/crontabs/root
  1513.  
  1514.  
  1515. - Any plain text usernames and/or passwords?
  1516. ------------------------------------------
  1517. grep -i user [filename]
  1518. grep -i pass [filename]
  1519. grep -C 5 "password" [filename]
  1520. find . -name "*.php" -print0 | xargs -0 grep -i -n "var $password" # Search for Joomla passwords
  1521.  
  1522.  
  1523. - What NIC(s) does the system have? Is it connected to another network?
  1524. ---------------------------------------------------------------------
  1525. /sbin/ifconfig -a
  1526. cat /etc/network/interfaces
  1527. cat /etc/sysconfig/network
  1528.  
  1529.  
  1530. - What are the network configuration settings? What can you find out about this network? DHCP server? DNS server? Gateway?
  1531. ------------------------------------------------------------------------------------------------------------------------
  1532. cat /etc/resolv.conf
  1533. cat /etc/sysconfig/network
  1534. cat /etc/networks
  1535. iptables -L
  1536. hostname
  1537. dnsdomainname
  1538.  
  1539. - What other users & hosts are communicating with the system?
  1540. -----------------------------------------------------------
  1541. lsof -i
  1542. lsof -i :80
  1543. grep 80 /etc/services
  1544. netstat -antup
  1545. netstat -antpx
  1546. netstat -tulpn
  1547. chkconfig --list
  1548. chkconfig --list | grep 3:on
  1549. last
  1550. w
  1551.  
  1552.  
  1553.  
  1554. - Whats cached? IP and/or MAC addresses
  1555. -------------------------------------
  1556. arp -e
  1557. route
  1558. /sbin/route -nee
  1559.  
  1560.  
  1561. - Who are you? Who is logged in? Who has been logged in? Who else is there? Who can do what?
  1562. ------------------------------------------------------------------------------------------
  1563. id
  1564. who
  1565. w
  1566. last
  1567. cat /etc/passwd | cut -d: # List of users
  1568. grep -v -E "^#" /etc/passwd | awk -F: '$3 == 0 { print $1}' # List of super users
  1569. awk -F: '($3 == "0") {print}' /etc/passwd # List of super users
  1570. cat /etc/sudoers
  1571. sudo -l
  1572.  
  1573.  
  1574.  
  1575. - What sensitive files can be found?
  1576. ----------------------------------
  1577. cat /etc/passwd
  1578. cat /etc/group
  1579. cat /etc/shadow
  1580. ls -alh /var/mail/
  1581.  
  1582.  
  1583.  
  1584. - Anything “interesting” in the home directorie(s)? If it’s possible to access
  1585. ----------------------------------------------------------------------------
  1586. ls -ahlR /root/
  1587. ls -ahlR /home/
  1588.  
  1589.  
  1590. - Are there any passwords in; scripts, databases, configuration files or log files? Default paths and locations for passwords
  1591. ---------------------------------------------------------------------------------------------------------------------------
  1592. cat /var/apache2/config.inc
  1593. cat /var/lib/mysql/mysql/user.MYD
  1594. cat /root/anaconda-ks.cfg
  1595.  
  1596.  
  1597. - What has the user being doing? Is there any password in plain text? What have they been edting?
  1598. -----------------------------------------------------------------------------------------------
  1599. cat ~/.bash_history
  1600. cat ~/.nano_history
  1601. cat ~/.atftp_history
  1602. cat ~/.mysql_history
  1603. cat ~/.php_history
  1604.  
  1605.  
  1606.  
  1607. - What user information can be found?
  1608. -----------------------------------
  1609. cat ~/.bashrc
  1610. cat ~/.profile
  1611. cat /var/mail/root
  1612. cat /var/spool/mail/root
  1613.  
  1614.  
  1615. - Can private-key information be found?
  1616. -------------------------------------
  1617. cat ~/.ssh/authorized_keys
  1618. cat ~/.ssh/identity.pub
  1619. cat ~/.ssh/identity
  1620. cat ~/.ssh/id_rsa.pub
  1621. cat ~/.ssh/id_rsa
  1622. cat ~/.ssh/id_dsa.pub
  1623. cat ~/.ssh/id_dsa
  1624. cat /etc/ssh/ssh_config
  1625. cat /etc/ssh/sshd_config
  1626. cat /etc/ssh/ssh_host_dsa_key.pub
  1627. cat /etc/ssh/ssh_host_dsa_key
  1628. cat /etc/ssh/ssh_host_rsa_key.pub
  1629. cat /etc/ssh/ssh_host_rsa_key
  1630. cat /etc/ssh/ssh_host_key.pub
  1631. cat /etc/ssh/ssh_host_key
  1632.  
  1633.  
  1634. - Any settings/files (hidden) on website? Any settings file with database information?
  1635. ------------------------------------------------------------------------------------
  1636. ls -alhR /var/www/
  1637. ls -alhR /srv/www/htdocs/
  1638. ls -alhR /usr/local/www/apache22/data/
  1639. ls -alhR /opt/lampp/htdocs/
  1640. ls -alhR /var/www/html/
  1641.  
  1642.  
  1643. - Is there anything in the log file(s) (Could help with “Local File Includes”!)
  1644. -----------------------------------------------------------------------------
  1645. cat /etc/httpd/logs/access_log
  1646. cat /etc/httpd/logs/access.log
  1647. cat /etc/httpd/logs/error_log
  1648. cat /etc/httpd/logs/error.log
  1649. cat /var/log/apache2/access_log
  1650. cat /var/log/apache2/access.log
  1651. cat /var/log/apache2/error_log
  1652. cat /var/log/apache2/error.log
  1653. cat /var/log/apache/access_log
  1654. cat /var/log/apache/access.log
  1655. cat /var/log/auth.log
  1656. cat /var/log/chttp.log
  1657. cat /var/log/cups/error_log
  1658. cat /var/log/dpkg.log
  1659. cat /var/log/faillog
  1660. cat /var/log/httpd/access_log
  1661. cat /var/log/httpd/access.log
  1662. cat /var/log/httpd/error_log
  1663. cat /var/log/httpd/error.log
  1664. cat /var/log/lastlog
  1665. cat /var/log/lighttpd/access.log
  1666. cat /var/log/lighttpd/error.log
  1667. cat /var/log/lighttpd/lighttpd.access.log
  1668. cat /var/log/lighttpd/lighttpd.error.log
  1669. cat /var/log/messages
  1670. cat /var/log/secure
  1671. cat /var/log/syslog
  1672. cat /var/log/wtmp
  1673. cat /var/log/xferlog
  1674. cat /var/log/yum.log
  1675. cat /var/run/utmp
  1676. cat /var/webmin/miniserv.log
  1677. cat /var/www/logs/access_log
  1678. cat /var/www/logs/access.log
  1679. ls -alh /var/lib/dhcp3/
  1680. ls -alh /var/log/postgresql/
  1681. ls -alh /var/log/proftpd/
  1682. ls -alh /var/log/samba/
  1683.  
  1684. - Note: auth.log, boot, btmp, daemon.log, debug, dmesg, kern.log, mail.info, mail.log, mail.warn, messages, syslog, udev, wtmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement