# 成為駭客的前一哩路 ## 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 下載 * 確保系統軟體套件清單是最新的。輸入以下命令: ```linux sudo apt update && sudo apt upgrade -y ```  * 一旦軟體套件清單已經更新,你可以使用以下命令安裝 Snort: ``` sudo apt-get install snort ```  * 下載到一半會跳出這個配置介面:  > 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 邦幫忙](https://ithelp.ithome.com.tw/articles/10333128)* **也可以先空著之後再設定** * 查看網路: ```linux ip addr ``` * 下載完後查看是否安裝成功 ```linux snort --version ```  ### Viewing Files * 利用 cat 顯示在 /etc/snort 路徑中 Snort 設定檔 (*snort.conf*) *因為有跨到 root 資料夾,所以需要 sudo權限* ```linux sudo cat /etc/snort/snort.conf ```  * 可以看到所有檔案內容被顯示出來,但並不方便及實際去找我們需要的內容 ### Finding the Head * 瀏覽檔案的開頭,預設值是顯示前 10 行  * 也可以自訂行數 `head -[行數] 檔名`,以下舉例顯示 20 行 ```linux sudo head -20 /etc/snort/snort.conf ```  ### Finding the Tail * 瀏覽檔案的尾端,語法跟`head`一樣,預設也是 10 行 ```linux sudo tail /etc/snort/snort.conf ```  ### Numbering the Lines * 瀏覽檔案內容並顯示對應的行數 ```linux sudo nl /etc/snort/snort.conf ```  * 補充:`wc`只顯示 檔案行數、字數、及位元組數 ```linux sudo wc /etc/snort/snort.conf ```  ### Filting Text with `grep` * `grep` 常作為文字操作指令,在這裡的功能就跟 `Ctrl + F` 一樣,從檔案中篩選出需要的文字 舉例:找檔案中包含 "output" 這個字的行 ```linux sudo cat /etc/snort/snort.conf | grep output ```  不用自己一行行找 ### Hacker Challenge: Using `grep`, `nl`, `tail`, and `head` * **目標:顯示 "# Step #6: Configure output plugins" 的前五行** 可以有很多種解法,希望你可以找出第二種 *因為`nl`無法標示空格,而tail -n+(num)會包含空格,所以使用`nl`時要加`-ba`* * Step 1 ```linux sudo nl -ba /etc/snort/snort.conf | grep output ```  可以看到 "# Step #6: Configure output plugins" 在第 544 行,然後不包含該行的前 5 行是第 539 行 * Step 2 ```linux tail -n+539 /etc/snort/snort.conf | head -n 6 ```  * `tail -n+(num)` 是指從num行開始顯示 * 由此,我們找出了"# Step #6: Configure output plugins" 的前五行 ### Using sed to Find and Replace * `sed` 的功能如同 Windows 的尋找並取代,就從以下例子解釋 1. 在 snort.conf 中尋找 mysql ```linux sudo cat /etc/snort/snort.conf | grep mysql ```  2. 將 mysql 更改成 MySQL 並存入 snort2.conf (路徑設在/home/(username)) ``` sudo sed s/mysql/MySQL/g /etc/snort/snort.conf > snort2.conf ```  * s/ 表示代換(substitution),中間的 / 分別為 **代換掉** 及 **欲代換**,由 /g 代表全域執行 3. 查看 snort2.conf ``` cat snort2.conf | grep MySQL ```  * 如果你只想替換第一個出現的字,可以把 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 ```   * `less` 指令功能跟`more`類似,但它不只可以上下瀏覽,也可以利用`/`篩選你想要查的字,然後按`q`退出 ``` sudo less /etc/snort/snort.conf ```   ### Summary * 作者說`grep` 跟 `less`很重要跟不可或缺 > 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 的密碼 * 相關網站 [Installing & Configuring Snort by HackerSploit](https://www.youtube.com/watch?app=desktop&v=U6xMp-MIEfA) [Snort 101: How to Install and Configure Snort // Cybersecurity Tools](https://www.youtube.com/watch?v=UAKq1S-VxJ8) [動手架設入侵偵測系統吧~Snort 介紹、安裝教學](https://ithelp.ithome.com.tw/articles/10333128) --- <div style="display: flex; justify-content: space-around"> <a href="https://hackmd.io/@MatchaCode/linuxbasics" role="button"> 上一篇: Chapter 1</a> <a href="https://hackmd.io/@MatchaCode/HJvNRJr7p" role="button"> 下一篇: Chapter 3</a> </div>
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up