Try   HackMD

成為駭客的前一哩路

Chapter 2 - Text Manipulation

In Linux, nearly everything you deal with directly is a file, and most often these will be text files.

安裝 Snort

  • Snort - from NIDS (Network Intrusion Detection System)
    Snort是一套開放原始碼的網路入侵預防軟體與網路入侵檢測軟體,而本章會以此作為例子

使用 Ubuntu 下載

  • 確保系統軟體套件清單是最新的。輸入以下命令:
sudo apt update && sudo apt upgrade -y

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 一旦軟體套件清單已經更新,你可以使用以下命令安裝 Snort:
sudo apt-get install snort

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 下載到一半會跳出這個配置介面:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  1. Snort 也會要求要指定本地網路的地址範圍(通常我們會指定自己的本地網路的 IP 地址範圍),以便 Snort 知道哪些流量被視為內部網路。
  2. 假設我提供了一個 CIDR 表示法的地址範圍:192.168.0.0/14。
  3. 這個地址範圍表示的是 192.168.0.0 到 192.168.255.255 之間的所有 IP 地址,這是一個典型的私有 IP 地址範圍,通常在家庭或企業網路中使用。
  4. 不過,若是你的內部網路不在這個範圍內,也可以根據你的實際情況提供正確的地址範圍。

印用自 動手架設入侵偵測系統吧~Snort 介紹、安裝教學 - iT 邦幫忙

也可以先空著之後再設定

  • 查看網路:
ip addr
  • 下載完後查看是否安裝成功
snort --version

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Viewing Files

  • 利用 cat 顯示在 /etc/snort 路徑中 Snort 設定檔 (snort.conf)

因為有跨到 root 資料夾,所以需要 sudo權限

sudo cat /etc/snort/snort.conf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 可以看到所有檔案內容被顯示出來,但並不方便及實際去找我們需要的內容

Finding the Head

  • 瀏覽檔案的開頭,預設值是顯示前 10 行

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 也可以自訂行數 head -[行數] 檔名,以下舉例顯示 20 行
sudo head -20 /etc/snort/snort.conf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Finding the Tail

  • 瀏覽檔案的尾端,語法跟head一樣,預設也是 10 行
sudo tail /etc/snort/snort.conf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Numbering the Lines

  • 瀏覽檔案內容並顯示對應的行數
sudo nl /etc/snort/snort.conf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 補充:wc只顯示 檔案行數、字數、及位元組數
sudo wc /etc/snort/snort.conf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Filting Text with grep

  • grep 常作為文字操作指令,在這裡的功能就跟 Ctrl + F 一樣,從檔案中篩選出需要的文字

舉例:找檔案中包含 "output" 這個字的行

sudo cat /etc/snort/snort.conf | grep output

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

不用自己一行行找

Hacker Challenge: Using grep, nl, tail, and head

  • 目標:顯示 "# Step #6: Configure output plugins" 的前五行

可以有很多種解法,希望你可以找出第二種

因為nl無法標示空格,而tail -n+(num)會包含空格,所以使用nl時要加-ba

  • Step 1
sudo nl -ba /etc/snort/snort.conf | grep output

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

可以看到 "# Step #6: Configure output plugins" 在第 544 行,然後不包含該行的前 5 行是第 539 行

  • Step 2
tail -n+539 /etc/snort/snort.conf | head -n 6

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • tail -n+(num) 是指從num行開始顯示
  • 由此,我們找出了"# Step #6: Configure output plugins" 的前五行

Using sed to Find and Replace

  • sed 的功能如同 Windows 的尋找並取代,就從以下例子解釋
  1. 在 snort.conf 中尋找 mysql
sudo cat /etc/snort/snort.conf | grep mysql

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  1. 將 mysql 更改成 MySQL 並存入 snort2.conf (路徑設在/home/(username))
sudo sed s/mysql/MySQL/g /etc/snort/snort.conf > snort2.conf

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • s/ 表示代換(substitution),中間的 / 分別為 代換掉欲代換,由 /g 代表全域執行
  1. 查看 snort2.conf
cat snort2.conf | grep MySQL

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 如果你只想替換第一個出現的字,可以把 g 去掉
sudo sed s/mysql/MySQL/ /etc/snort/snort.conf > snort2.conf
  • 如果要去掉第 n 個出現的字,可以在最後的 / 後面加 n
sudo sed s/mysql/MySQL/n /etc/snort/snort.conf > snort2.conf

Viewing Files with more and less

  • more 指令可以讓你在瀏覽檔案時使用Enter
sudo more /etc/snort/snort.conf

image

image

  • less 指令功能跟more類似,但它不只可以上下瀏覽,也可以利用/篩選你想要查的字,然後按q退出
sudo less /etc/snort/snort.conf

image

image

Summary

  • 作者說grepless很重要跟不可或缺

We’ve touched on a few of the most useful methods in this chapter, but I suggest you try each one out and develop your own feel and preferences.

Exercise

  1. 瀏覽至 /usr/share/metasploit-framework/data/wordlists。這是一個多個單字清單的目錄,可用於使用最受歡迎的滲透測試和駭客框架 Metasploit 在各種受密碼保護的裝置中暴力破解密碼
  2. 使用 cat 指令查看 password.lst 檔案的內容
  3. 使用 more 指令顯示 password.lst 文件
  4. 使用 less 指令查看 password.lst 文件
  5. 使用 nl 指令在 password.lst 中的密碼上新增行號。應該有大約 88,396 個密碼
  6. 使用 tail 指令查看 password.lst 文件中的最後 20 個密碼
  7. 使用 cat 指令顯示 password.lst 文件,然後將其通過管道傳遞給 find 命令,以找出所有包含 123 的密碼

上一篇: Chapter 1 下一篇: Chapter 3