owned this note
owned this note
Published
Linked with GitHub
資訊安全暑訓營
===
## 高階網站安全
###### 2019/07/16
> [講義](https://drive.google.com/file/d/1E6m7Nm-2ONeyBRjyw62eGzRi8rYpYQP-/view?usp=sharing)
> 由於上課沒有把講義上完,筆記目前只整理出前半的部分,後半正在趕工中。
### URL 構成
::: success
<scheme>://<netloc>/<path>?<query>#<fragment>
http://kaibro.tw/a.php?gg=in#yo
:::
- scheme : http
- netloc : baibro.tw
- path : a.php
- query : gg=in
- fragment : yo
### HTTP Protocol
#### HTTP Method
- 表示Request的目的
- GET / POST / PUT / DELETE / OPTIONS ...
- GET
- 跟server要東西
- 參數會出現在網址上
- POST
- 送東西給server
- 參數不會出現在網址
- 常用於登入, 上傳檔案
#### Request
- 讓瀏覽器打kaibro.tw, 送出的Request:
:::info
GET / HTTP/1.1
Host: kaibro.tw
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0)
Gecko/20100101 Firefox/56.0
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3
Connection: close
Upgrade-Insecure-Requests: 1
:::
###### 詳細內容介紹請看講義16~36頁
#### Statuus Code
- Server回傳狀態代碼
- 1xx : **有收到請求,但仍要繼續處理**
- 2xx : **成功,好棒棒**
- 3xx : **重導向相關的訊息**
- 4xx : **Client端發生錯誤**
- 5xx : **Server端發生錯誤**
#### Cookie
- 網站用來紀錄資料
> 存放在Client小檔案
- 紀錄帳號資訊
- session id
- 使用者是否登入
> 用在投放廣告
### 資訊蒐集( Information Leak )
- robots.txt
- .git / .svn
- .DS_Store
- .index.php.swp
- index.php~
#### robots.txt
- 顯示隱藏路徑
- 有時可找到一些較難猜的目錄、檔案
- CTF老梗
#### git / svn
- 版本管理系統
- 常見線上部署環境忘記砍掉
- 還原 source code
- 工具
- [scrabble](https://github.com/denny0223/scrabble)
- [GitHack](https://github.com/lijiejie/GitHack)
#### .DS_Store
- Apple系統常見隱藏檔
- 能洩漏目錄資訊,如資料夾文件名等
- 工具
- [ds_store_exp](https://github.com/lijiejie/ds_store_exp)
- [原理分析](https://0day.work/parsing-the-ds_store-file-format/)
#### Google Hacking
> 利用 Google search
- site :
- 指定特定網站
- intext :
- 搜索網頁正文出現的字串
- intitle :
- 搜索網頁標題
- filetype : / ext :
- 搜索特定類型副檔名
#### GitHub Hacking
> Search前人破解的東西
#### 其他 (CTF使用請注意規則)
- 掃 Port
- [Nmap](https://nmap.org/), [masscan](https://github.com/robertdavidgraham/masscan)
- 掃目錄/檔案
- [dirsearch](https://github.com/maurosoria/dirsearch), [DirBuster](https://www.owasp.org/index.php/Category:OWASP_DirBuster_Project), ...
- 掃子域名
- [subDomainBrute](https://github.com/lijiejie/subDomainsBrute), [Sublist3r](https://github.com/aboul3la/Sublist3r), ...
#### 社工庫( Social Engineering Database )
- 由洩漏的資料庫組成
- [Have I Been Pwned](https://haveibeenpwned.com/)
### PHP
#### PHP Feature
- Hacker-friendly language
- 早期CTF常出現
#### 弱型別
- '123' == 123 ?
- True
- 'kaibro' == 0 ?
- True
- '0010e2' == '1e3' ?
- True
- 0 == false ?
- True
#### Array 特性
- strcmp([],[])
- NULL
- shal([])
- NULL
- strlen([])
- NULL
### 前端安全
#### 同源政策
- 同域要求同協議、同域名、同端口
##### 跨域
- JSONP
- 透過script可以跨域的特性載入資料
- CORS
- 添加一些HTTP Header來標示跨域請求
#### XSS (Cross Site Scripting)
> 輸入被當Javascript執行
- 反射型 XSS
- 儲存型 XSS
- DOM XSS
#### CSRF (Cross-site request forgery)
> 觸發非預期 Request
>
#### 瀏覽器層面防禦
- IE/Edge: XSS Filter
- Chrome/Safari: XSS Auditor
- CSP (Content Security Policy)
### Command Injection
> 常出現在網頁直接跑 System Command 的狀況
#### 基本招式
### SQL Injection
> 一種資料庫查詢語言
### 資料補充
- [http](https://notfalse.net/http-series)
- [php-1](https://www.php.net/manual/zh/index.php)
- [php-2](https://github.com/w181496/Web-CTF-Cheatsheet)
- [CORS](https://developer.mozilla.org/zh-TW/docs/Web/HTTP/CORS)
## Crypto *~~自己加油...666~~*
###### 2019/07/17
> [講義](https://drive.google.com/file/d/1guWxIBm22TnpzR_CUR0UnnozF0oKBmZP/view?usp=sharing)
> [共筆](https://hackmd.io/a42SAsXjRdmIDSsT1TQQgg?view#Mode)
> ***被擊倒 =.= 自己看ㄅ***
### pwntools
r.send("XXX")
###### 沒Enter
r.sendline("XXX")
###### 有Enter
r.sendlineafter('a',"XXX")
###### 接收到a才傳XXX
r.recv()
R.recvline()
###### \n
r.recvuntil('\n')
###### 接收到哪裡
r.recvrepeat(0.3)
###### 0.3秒後不接收
A xor B = C
A xor C = B
B xor C = A
打RSA好用的自動化工具
[RsaCtfTool](https://github.com/Ganapati/RsaCtfTool)
## 逆向工程 Reverse
###### 2019/07/18
[講義](https://drive.google.com/file/d/1tzTPKLIVcdg2g3yiBGikTdfPxUUmEp9e/view?usp=sharing)
### 起手式
#### 指令 - file
:::info
用途 : 用來查看檔案類型
:::
:::success
shell : file binary
> binary在這為檔案名
:::
:::warning
output :
binary: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
dynamically linked, interpreter /lib/ld-, for GNU/Linux 2.6.32,
BuildID[sha1]=89e53b704c36245e1ab38a71c9f1349898e373ea, stripped
:::
#### 指令 - strings
:::info
用途 : 印出檔案中的可視字串
:::
:::success
shell : strings binary
> binary在這為檔案名
:::
:::warning
output :
/lib/ld-linux.so.2
;pL6$^
libc.so.6
...
:::
---
:::info
進階用法 : 印出檔案中的可視字串,長度大於等於10
:::
:::success
shell : strings -n 10 binary
> binary在這為檔案名
:::
:::warning
output :
/lib/ld-linux.so.2
_IO_stdin_used
__stack_chk_fail
...
:::
---
:::info
進階用法 : 印出檔案中的可視字串,且結果中包含"name"
:::
:::success
shell : strings binary | grep "name"
> binary在這為檔案名
:::
:::warning
output :
Out of list name (0 <= index < 10)!!!
Input this name:
Input new name:l
...
:::
#### 指令 - strace
:::info
用途 : 查看程式執行時的system call 和 signal
:::
:::success
shell : strace ./binary
> binary在這為檔案名
:::
:::warning
output :
execve("./hexedit", ["./hexedit"], 0x7ffd6c827460 /* 38 vars */) = 0
brk(NULL) = 0xe37000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
...
:::
#### 指令 - ltrace
:::info
用途 : 查看程式執行時的library call
:::
:::success
shell : ltrace ./binary
> binary在這為檔案名
:::
:::warning
output :
__libc_start_main(0x40052d, 1, 0x7ffc83150168, 0x400550 <unfinished ...>)
puts("Find the flag!")
+++ exited (status 0) +++
:::
#### 指令 - objdump
:::info
用途 : 查看反組譯資訊
:::
:::success
shell : objdump -d ./binary
> binary在這為檔案名
:::
:::warning
output :
adder: file format elf64-x86-64
Disassembly of section .init:
0000000000400778 <_init>:
400778: 48 83 ec 08 sub $0x8,%rsp
40077c: 48 8b 05 75 18 20 00 mov 0x201875(%rip),%rax # 601ff8 <__gmon_start__>
400783: 48 85 c0 test %rax,%rax
...
:::
---
:::info
進階用法 : 查看反組譯資訊,並以intel格式導向less方便閱讀
:::
:::success
shell : objdump -M intel -d ./binary | less
> binary在這為檔案名
:::
:::warning
output :
adder: file format elf64-x86-64
Disassembly of section .init:
0000000000400778 <_init>:
400778: 48 83 ec 08 sub rsp,0x8
40077c: 48 8b 05 75 18 20 00 mov rax,QWORD PTR [rip+0x201875] # 601ff8 <__gmon_start__>
400783: 48 85 c0 test rax,rax
...
:::
### 組合語言
#### Intel vs. AT&T syntax
> 一般通常是用Intel syntax
> 詳細差異請看講義18頁
#### [General Purpose Register](https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture)
> 老師只丟wiki給我們
> 圖片我放不上去
> 講義19~20頁
#### RIP
- instruction pointer
- 指向下一個要執行的指令
#### RSP
- stack pointer
- 指向stack的頂端
#### 編譯組合語言
:::info
shell:
```=
nasm -f elf64 test.asm #編譯
ld -m elf_x86_64 -o test test.o #鏈結
./test #執行
```
:::
#### 函式呼叫
> 講義25~31
> 全部圖解
- 條件判斷式
> 32頁自己看八
- 迴圈
> 33頁自己看八
### 程式在記憶體中的樣子
#### 原始碼
:::success
C :
```cpp=
#include <iostream>
int main() {
int a = 0x17, b = 0x4, c;
c = a + b;
printf("%d\n", c);
}
```
:::
:::success
assembly :
```=
...
mov DWORD PTR [rbp-0x1c],0x17
mov DWORD PTR [rbp-0x18],0x4
mov edx,DWORD PTR [rbp-0x1c]
mov eax,DWORD PTR [rbp-0x18]
add eax,edx
mov DWORD PTR [rbp-0x14],eax
```
:::
#### 編譯&執行
:::success
shell :
```shell=
gcc test.c -o test # 編譯
./test # 執行
```
:::
#### 程式在記憶體長怎樣
- 開兩個terminal,來看一下程式跑起來在記憶體中長甚麼樣子
:::danger
shell :
```shell=
./test
```
:::
---
:::danger
shell :
```shell=
cat /proc/$(pidof test)/maps
```
:::
> 講義38~39有漂亮的圖片自己看吧
### 動態除錯 - gdb
#### gdb - 使用介紹
::: info
下斷點 :
**B**reak **main**
**B**reak ***0x4004da**
:::
---
:::info
執行程式 :
**r**un
:::
---
:::info
繼續執行 :
**c**ontinue
:::
---
:::info
執行完該函式 :
**fin**ish
:::
---
:::info
執行一條 instruction (不會追進函式) :
**ni**
:::
---
:::info
執行一條 instruction ( 會追進函式 ) :
**si**
:::
---
:::info
離開 :
**q**uit
:::
---
:::info
列出變數的值 :
**p**rint **$rsp**
:::
---
:::info
印出變數指向的記憶體的值 :
**x/[length][format] [address expression]**
**x/10gx $rsp
x/20wx 0x7fffffffe5f0
x/s 0x7fffffffe8d0**
> [使用介紹](http://visualgdb.com/gdbreference/commands/x)
:::
---
:::info
查看記憶體 :
**vm**map
:::
---
:::info
查看保護機制
**checks**ec
:::
#### gdb - 套件安裝
- [peda](https://github.com/longld/peda)
- [Pwngdb](https://github.com/scwuaptx/Pwngdb)
- 裝這兩個套件可以讓 gdb 更好用
:::info
安裝 :
shell
```shell=
git clone https://github.com/longld/peda.git ~/peda
git clone https://github.com/scwuaptx/Pwngdb.git
cp ~/Pwngdb/.gdbinit ~/
```
:::
- 介面展示
> 講義47頁\
#### 工具推薦
##### 反編譯
- [IDA Pro](https://www.hex-rays.com/products/ida/support/download.shtml)
> 要錢
- [Ghidra](https://ghidra-sre.org/)
> 免費
## PWN
###### 2019/07/19
### 工具使用 - pwntools
#### 介紹
- python 套件
- 專門給打 pwn 的人用的
- [介紹文](https://github.com/Gallopsled/pwntools)
- [教學資源-1](http://docs.pwntools.com/en/stable/intro.html)
- [教學資源-2](https://github.com/Gallopsled/pwntools-tutorial)
#### 安裝
- [Github](https://github.com/arthaud/python3-pwntools)
:::info
shell :
```shell=
sudo apt-get update
sudo apt-get install python3 python3-dev python3-pip git
sudo pip3 install --upgrade git+https://github.com/arthaud/python3-pwntools.git
```
:::
#### 與server連線
:::info
python :
```python=
from pwn import *
r = remote('127.0.0.1',20000)
r.interactive()
```
> r.interactive()為互動模式,使用起來跟 nc 差不多
:::
#### 常用函式
::: info
```python=
# 從伺服器接收所有 '\n' 之前的 data
r.recvline()
# 發送 data 給伺服器
r.send('hello')
# 發送 data + '\n' 給伺服器
r.sendline('hello')
# 從伺服器接收所有 hello 之前的 data
r.recvuntil('hello')
# 從伺服器接收所有 hello 之前的 data 後發送 world
r.sendlineafter('hello', 'world')
```
:::
---
:::info
```python=
# 將數字轉成 32 bits = 4 bytes 的字串 ( little endian )
p32(0xdeadbeef)
# 將數字轉成 64 bits = 8 bytes 的字串 ( little endian )
p64(0xdeadbeef)
# 將 32 bits = 4 bytes 的字串轉成數字 ( little endian )
u32(b'\xef\xbe\xad\xde')
# 將 64 bits = 8 bytes 的字串轉成數字 ( little endian )
u64(b'\xef\xbe\xad\xde\x00\x00\x00\x00')
```
:::
---
:::info
```python=
# load libc
libc = ELF('./libc')
# libc base address
libc.address
# libc functions
libc.symbols[b'system']
```
:::
#### 本地端 debug
:::info
```python=
from pwn import *
context.arch = 'amd64'
context.terminal = ['tmux', 'splitw', '-h']
r = process('./test')
gdb.attach(proc.pidof(r)[0])
```
> tmux 要先安裝
:::
### 攻擊技巧 - butter overflow
#### 用C寫一個簡單的程式
:::info
```cpp=
#include<stdio.h>
void magic() {
system("/bin/sh")
}
int main() {
char buffer[8];
scanf("%s",buffer); // 讀取使用者輸入
printf("%s\n",buffer); // 印出使用者輸入
return 0;
}
```
:::
---
站存檔 :
> ubuntu
> ubuntu linux
---
> 編輯器
> ZSH (oh my zsh)
---
- [NTHU](https://hackmd.io/PBaX-EWYROC-UuQKlwhDnA)