# Happy Linux Day
https://github.com/MyFirstSecurity2020/HappyLinuxDay
https://hackmd.io/@ss8651twtw/linux
ss8651twtw
---
## 環境資訊
- Ubuntu 18.04
- [回家練習環境](https://drive.google.com/file/d/1mDmjexzPsFuI8p7EzY0Uy1xZ0-Fj-vkj/view?usp=sharing)
---
## 基礎 Linux 操作
補充說明 幫助入門
----
### 為啥要使用 Linux
https://www.opensourceforu.com/2020/03/reasons-to-use-linux/
1. 免費開源容易客製化
2. 方便多人多工作業
3. 系統穩定好管理
----
### 名詞說明
- 核心 kernel
- 殼層 shell
- 檔案 file
- 目錄 path
- 指令 command
- 參數 argument
----
### 指令和參數是啥抹東東
指令 -> (便利商店店員)
參數 -> (告訴他要做各種不同的事)
----
### 哪裡輸入指令
- 主選單 -> 附屬應用程式 -> 終端機
- 搜尋 "終端機" 或 "terminal"
----
### 該用什麼指令完成想做的事

----
### 查詢指令使用說明
- man (menu page for command)
- **[tldr](https://github.com/tldr-pages/tldr) (simple and useful help page for command)**
- Google!
```shell
sudo apt install tldr
```
----
### 基本指令
```shell
pwd, cd, ls, file, chmod, cat, grep, find, mv, cp,
rm, mkdir, echo, strings, ssh, sudo, apt, wget
```
----
### 檔案目錄
- 當前目錄 .
- 上一層目錄 \.\.
- 家目錄 ~
- 根目錄 /
----
### 輸入輸出重導向
- \| (pipe)
- \< (input)
- \> (output)
- \>> (append)
----
### | (pipe)
將 cmd1 的輸出丟給 cmd2 當輸入
```shell
cmd1 | cmd2
```
----
### < (input)
將 file 當成 cmd 的輸入
```shell
cmd < file
```
----
### > (output)
將 cmd 的結果輸出至 file (會覆寫檔案)
```shell
cmd > file
```
----
### >> (append)
將 cmd 的結果輸出至 file (接續輸出至檔案)
```shell
cmd >> file
```
----
### vim
強大的文字編輯器
https://vim.rtorr.com/
----
### 踹踹看
- Linux 103
- execme - 50 pts
- Meow - 50 pts
- Linux 101
- Linux CTF 1 - 10 pts
- Linux CTF 2 - 10 pts
----
### 參考解答
- Linux 103 - execme - 50 pts
```shell
# 確認檔案類型
file execme
# execme: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=B9WP1Tjv-IQ-_ESBrKH2/OhEtEOshrz0yRVHNwnc9/9nghBn86vg4IA43uFiNQ/I0_ttISs8oEUEzgrvgas, stripped
# 得知是執行檔檢查是否有執行權限
ls -l execme
# -rw-rw-r-- 1 ss8651twtw ss8651twtw 1445888 Apr 22 07:47 execme
# 發現原先沒有執行權限使用 chmod 加上執行權限
chmod +x execme
# 執行檔案就會看到 flag 印出
./execme
```
----
### 參考解答
- Linux 103 - Meow - 50 pts
```shell
# 確認檔案類型
file meow
# meow: ASCII text
# 得知是文字檔印出檔案內容就會看到 flag
cat meow
```
----
### 參考解答
- Linux 101 - Linux CTF 1 - 10 pts
```shell
# 使用 ssh 指定使用者帳號連線到遠端伺服器
ssh lab@XXX.XXX.XXX.XXX -p 5500
# 觀察當前目錄下有什麼檔案
ls
# base64.txt flag hex.txt
# 印出 flag 檔案內容
cat flag
```
----
### 參考解答
- Linux 101 - Linux CTF 2 - 10 pts
```shell
# 使用 ssh 指定使用者帳號連線到遠端伺服器
ssh lab@XXX.XXX.XXX.XXX -p 5500
# 觀察當前目錄下有什麼檔案
ls -a
# . .. .bash_logout .bashrc .hidden .profile base64.txt flag hex.txt
# 印出 .hidden 檔案內容
cat .hidden
```
---
## 檔案分析
檔案類型 檔案資訊
----
### 檔案格式 (File Signature)
https://en.wikipedia.org/wiki/List_of_file_signatures
```shell
# make a hexdump or do the reverse
xxd <file>
```
----
### 分析指令
```shell
# determine file type
file <file>
# print the strings of printable characters in files
strings <file>
```
----
### 踹踹看
- Linux binary analysis
- EasyCTF_hexedit - 25 pts
----
### 參考解答
- Linux binary analysis - EasyCTF_hexedit - 25 pts
```shell
# 確認檔案類型
file hexedit
# hexedit: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.24, BuildID[sha1]=be7897ed315b5c5a619013ec7c84c47a5278c9ee, not stripped
# 得知是執行檔檢查是否有執行權限
ls -l hexedit
# -rw-r--r-- 1 ss8651twtw ss8651twtw 8566 Apr 22 08:07 hexedit
# 發現原先沒有執行權限使用 chmod 加上執行權限
chmod +x hexedit
# 執行檔案並沒有看到 flag
./hexedit
# Find the flag!
# 嘗試尋找檔案中的可視字串並篩選包含 easyctf 的字串就可以看到 flag
strings hexedit | grep easyctf
```
---
## 隱寫術 (Steganography)
資訊隱藏 隱秘書寫
----
### 舉個例子
<style>
.haha {
color: rgba(0, 0, 0, 0);
}
</style>
<span class="haha">哈哈我就是 🌰 啦</span>
----
### 現代隱寫術
將「隱秘檔案 (secret data)」藏到
「載體檔案 (cover file)」中
----
### 載體檔案
- 文件
- 圖片
- 音訊
- 影片
- ...
----
### 分析方法
1. 確認檔案格式 (可以使用 file, xxd 指令)
2. 拆開多個檔案 (可以使用 dd 指令)
3. 檢查內容資訊

----
### 分析工具
- [binwalk](https://github.com/ReFirmLabs/binwalk)
- [stego-toolkit](https://github.com/DominicBreuker/stego-toolkit)
----
### 踹踹看
- 隱寫術101
- STEG(必) - 20 pts
- sCTF 2016 Q1 : banana-boy-20(必) - 50 pts
----
### 參考解答
- 隱寫術101 - STEG(必) - 20 pts
1. 喜好設定
2. 檢視
3. 顯示非列印字元將「隱藏文字」打勾
----
### 參考解答
- 隱寫術101 - sCTF 2016 Q1 : banana-boy-20(必) - 50 pts
```shell
# 將檔案中夾帶各種不同類型的檔案抽取出來
binwalk -D ".*" carter.jpeg
# DECIMAL HEXADECIMAL DESCRIPTION
# --------------------------------------------------------------------------------
# 0 0x0 JPEG image data, JFIF standard 1.01
# 382 0x17E Copyright string: "Copyright (c) 1998 Hewlett-Packard Company"
# 3192 0xC78 TIFF image data, big-endian, offset of first image directory: 8
# 140147 0x22373 JPEG image data, JFIF standard 1.01
# 140177 0x22391 TIFF image data, big-endian, offset of first image directory: 8
# 查看隱藏的圖片就可以看到 flag
```
---
## C/C++ 程式開發
設計撰寫 編譯執行
----
### 流程
1. 設計撰寫程式 (文字檔)
2. 編譯器編譯處理 (執行檔)
3. 執行程式
----
### 編譯器
- gcc 編譯處理 C 程式
- g++ 編譯處理 C++ 程式
----
### 編譯處理
1. 預處理 (preprocess)
2. 編譯 (compile)
3. 組譯 (assemble)
4. 連結 (link)
```shell
# generate an executable file (a.out)
gcc <file.c>
g++ <file.cpp>
```
----
### 預處理
引入套件 (include) 並代換處理定義的巨集 (define)
```shell
gcc -E <file.c>
g++ -E <file.cpp>
```
----
### 編譯
將高階語言轉換為低階語言
(C/C++ 轉換為組合語言)
```shell
# generate an assembly file (*.s)
gcc -S -masm=intel <file.c>
g++ -S -masm=intel <file.cpp>
```
----
### 組譯
將組合語言 (assembly code)
轉換為機器語言 (machine code)
```shell
# generate an object file (*.obj or *.o)
gcc -c <file.s>
g++ -c <file.s>
```
----
### 連結
連結目的檔 (object file)
生成執行檔 (executable file)
```shell
# generate an executable file (a.out)
gcc <file.o>
g++ <file.o>
```
---
## 逆向工程 (Reverse Engineering)
靜態分析 動態除錯
----
### 靜態分析
- readelf
- objdump
- [Radare2](https://rada.re/n/)
- [Ghidra](https://ghidra-sre.org/)
----
### 組合語言
- [指令小抄](https://www.bencode.net/blob/nasmcheatsheet.pdf)
- AT&T 與 Intel 格式

----
### 動態除錯
- [GDB](https://www.gnu.org/software/gdb/)
----
### 踹踹看
- Reverse
- EasyCTF_adder - 25 pts
----
### 參考解答
- Reverse - EasyCTF_adder - 25 pts
```shell
# 確認檔案類型
file adder
# adder: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5247f49239e8007694bfa30ed2f96bb961f04c5c, not stripped
# 得知是執行檔檢查是否有執行權限
ls -l adder
# -rw-r--r-- 1 ss8651twtw ss8651twtw 13440 Apr 22 08:21 adder
# 發現原先沒有執行權限使用 chmod 加上執行權限
chmod +x adder
# 執行檔案依提示輸入三個數字後程式輸出 nope. 就結束了
./adder
# Enter three numbers!
# 111 222 333
# nope.
```
----
### 參考解答
- Reverse - EasyCTF_adder - 25 pts
1. 開啟 Ghidra 反編譯分析
2. 檢視 main function
```c
undefined8 main(void) {
...
if (local_1c + local_14 + local_18 == 0x539) {
std::operator<<((basic_ostream *)std::cout,"easyctf{");
print_ptr(local_10);
puts("}");
}
else {
std::operator<<((basic_ostream *)std::cout,"nope.\n");
}
...
}
```
----
### 參考解答
- Reverse - EasyCTF_adder - 25 pts
```shell
# 執行程式並輸入三個數字
# 將三個數字和設為 0x539 (1337)
./adder
# Enter three numbers!
# 0 0 1337
```
---
## Reference
### 基礎 Linux 操作
- https://www.opensourceforu.com/2020/03/reasons-to-use-linux/
- http://www.ee.surrey.ac.uk/Teaching/Unix/
- http://wiki.ubuntu-tw.org/index.php?title=GNU/Linux_%E5%B8%B8%E7%94%A8%E6%8C%87%E4%BB%A4
- https://www.javatpoint.com/linux-tutorial
- https://www.fossmint.com/awesome-linux-software/
----
### 基礎 Linux 操作
- https://linux.die.net/man/1/man
- https://github.com/tldr-pages/tldr
- https://github.com/GitSquared/edex-ui
- https://vim.rtorr.com/
----
### 隱寫術 (Steganography)
- https://en.wikipedia.org/wiki/Steganography
- https://github.com/ReFirmLabs/binwalk
- https://github.com/DominicBreuker/stego-toolkit
----
### C/C++ 程式開發
- https://linux.die.net/man/1/gcc
- https://linux.die.net/man/1/g++
----
### 檔案分析
- https://en.wikipedia.org/wiki/List_of_file_signatures
- https://linux.die.net/man/1/xxd
- https://linux.die.net/man/1/file
- https://linux.die.net/man/1/strings
----
### 逆向工程 (Reverse Engineering)
- https://linux.die.net/man/1/readelf
- https://linux.die.net/man/1/objdump
- https://rada.re/n/
- https://ghidra-sre.org/
- https://www.bencode.net/blob/nasmcheatsheet.pdf
- https://www.gnu.org/software/gdb/
---
## 問卷
http://gg.gg/0424TP
---
## Thanks for Listening
  
{"metaMigratedAt":"2023-06-15T10:43:54.893Z","metaMigratedFrom":"YAML","title":"Happy Linux Day","breaks":true,"description":"Happy Linux Day","lang":"zh-tw","dir":"ltr","robots":"index, follow","GA":"UA-119880828-2","disqus":"ss8651twtw","slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"db4f0000-df30-46e6-b8e1-e38dca2b241f\",\"add\":10824,\"del\":2004}]"}