# picoCTF Forensics
使用工具 :
- `ghex` => linux圖形化十六進制編輯器
- `oeg` => linux打開和瀏覽圖片文件
- `Xpdf` => 輕量化的Linux PDF閱讀器
- `wireshark`
- `aircrack-ng` =>能根據捕獲的數據包對 WEP 密鑰進行破解
- `zsteg` => 破解資料隱藏
- `stepic` => 也是將資料的內容提取出來
- `binwalk` => 是一個針對二進位檔案的分析工具,可用來找出檔案中是否隱藏了其他資料(例如壓縮檔、圖片、可執行檔等)
- `dd` => 是 Linux 和 Unix 系統中一個非常強大的命令列工具,主要用途是在設備之間複製位元組資料,可以用來`備份與還原磁碟`,`建立開機碟`,`擷取或寫入 ISO 映像檔`
# Eazy
## RED

輸入 `zsteg red.png`

## **Secret of the Polyglot**

Hints : This problem can be solved by just opening the file in different ways
xpdf flag2of2-final.pdf

mv flag2of2-final.pdf flag2of2-final.png

# Medium
## **Lookey here**

cat anthem.flag.txt | grep pico

## **endianness-v2**

先來看檔案的類型和標頭
`file challengefile`
`exiftool challengefile`
看起來header(標頭)有錯

用ghex看一下

忘記JPEG header是什麼,google一下長這樣
"FF D8 FF E0"

所以要把challengefile Header的"E0 FF D8 FF" -> "FF D8 FF E0"
後來發現單純只改Header是不行的,要reverse整個challengefile
```python
def reverse_hex_bytes(file_path, output_path):
with open(file_path, "rb") as f:
data = f.read()
reversed_data = bytearray()
for i in range(0, len(data), 4):
reversed_data.extend(data[i:i+4][::-1])
with open(output_path, "wb") as f:
f.write(reversed_data)
reverse_hex_bytes("challengefile", "reversed_output")
```


## **PcapPoisoning**

`.pcap`是一種用於儲存網路封包資料的檔案格式
wireshark開啟檔案

tcp contains "pico"

## **FindAndOpen**

看了其他Info,就只有這個不一樣,最後有'=',感覺很像base64


## **Redaction gone wrong**

檔案一開起來就長這樣

丟到word把字體全部反白就會找到flag了

## **Packets Primer**

找一下就找到了

## **Eavesdrop**

Hints : All we know is that this packet capture includes a chat conversation and a file transfer.
```
- chat conversation
Hey, how do you decrypt this file again?
You're serious?
Yeah, I'm serious
*sigh* openssl des3 -d -salt -in file.des3 -out file.txt -k supersecretpassword123
Ok, great, thanks.
Let's use Discord next time, it's more secure.
C'mon, no one knows we use this program like this!
Whatever.
Hey.
Yeah?
Could you transfer the file to me again?
Oh great. Ok, over 9002?
Yeah, listening.
Sent it
Got it.
You're unbelievable
```
有找到data,但不是很熟wireshark,不太知道要則麼複製data的value,參考了下方的影片
https://www.youtube.com/watch?v=DJfT2fI88YE&ab_channel=MustaphaAitIchou
把下方data加入到file.des3

解密
```
sudo openssl des3 -d -salt -in file.des3 -out file.txt -k supersecretpassword123
```

## **WPA-ing Out**

Hints:
1. Finding the IEEE 802.11 wireless protocol used in the wireless traffic packet capture is easier with wireshark, the JAWS of the network.
2. Aircrack-ng can make a pcap file catch big air...and crack a password.
Hint2就有說可以用aircrack-ng暴力破解password
Aircrack-ng 能根據捕獲的數據包對 WEP 密鑰進行破解

## **Milkslap** (待補)

Hint:Look at the problem category


問題的category css??

```
wget http://mercury.picoctf.net:29522/concat_v.png
```
## shark on wire 1

Hints:
1. Try using a tool like Wireshark
2. What are streams?
stream 是一個用來描述一組相關封包的概念
wireshark好像通過分析header,將屬於同一session or 連線的封包分類

這時我在猜UPD的streams

矇了這樣就結束了???

## flags are stepic

Hint : In the country that doesn't exist, the flag persists
所以要找不是國家的flag

`Ctrl + U` 找到那個國家Upanzi, Republic The

wget `url+/flags/upz.png`

用了zsteg噴出一些不相關的東西
這時注意到題目 stepic ,不知道是什麼鬼東西
問了一下GPT

ok 下方是script
```cpp=
import stepic
from PIL import Image
image = Image.open("upz.png")
message = stepic.decode(image)
print("Message: ",message)
```

## So Meta


## WhitePages

還真的是whitepage白的

輸入 `ghex whitepages.txt`
可以觀察到內文有`\xe2\x80\x83`很特別

如果把它替換成`0`,而其他替換成`1`
來試看看
```python=
from pwn import *
with open('whitepages.txt','rb') as f:
data = f.read()
data = data.replace(b'\xe2\x80\x83',b'0').replace(b' ',b'1')
data = data.decode("ascii")
#print(data)
print(unbits(data).decode("ascii"))
```

參考資料:https://github.com/kevinjycui/picoCTF-2019-writeup/tree/master/Forensics/WhitePages
## extensions

exiftool flag.txt
事實上這個檔案是.png

mv flag.txt flag.png
xdg-open flag.png

## What Lies Within

zsteg buildings.png

## like 1000

他說 a lot 需要解壓縮,像是下方這樣
會產出子壓縮檔

script
```python=
import subprocess
n=1000
while (n > 0):
tar_file = str(n) + ".tar"
subprocess.run(["tar","xvf",tar_file])
print(f"{n} Finished!")
n = n - 1
```


## hideme

zsteg 沒找出東西
```
binwalk flag.png
```


```
dd if=flag.png of=flag.zip skip=41
```


## Enhance

MIME Type : image/svg+xml

xml用cat也可以print出來
cat drawing.flag.svg

## Mob psycho

Hints:
1. Did you know you can unzip APK files?
2. Now you have the whole host of shell tools for searching these files.
unzip mobpsycho.apk -d psycho
```
ls -R | grep flag -B 20
```
`-R` : 遞迴(Recursive)列出目錄內容
`-B 20` : --before-context=20的簡寫

[Link](https://gchq.github.io/CyberChef/)

## Torrent Analyze

從來沒碰過 Tor
後來查了一下
Torrent是一種 P2P (Peer-to-Peer)檔案分享協議
- 不依賴單一伺服器,檔案由多個使用者共同分享
- 每個使用者可以下載也可以上傳檔案
`.torrent`檔案中包含:
- 檔案資訊
- info_hash (唯一識別這個 torrent 的值)
- Tracker 位址(協助找到 peers)
主要分為三個 :
1. seeds (做種者):
- 已經完整下載整個檔案的人
- 提供檔案給其他peer下載
- Seed越多,下載速度通常越快
2. peers (對等節點/已連線但未完成的人):
- 正在下載檔案的人
- 同時也會上傳自己已下載的部分給其他peers
- Peer中包含 leechers(還沒有完成下載的人)
3. leechers :
- 正在下載檔案,但尚未完整擁有
### protocol
**BT-DHT** : 用來找peers的協定,當tracker不存在時,根據 `info_hash` 尋找誰有這個torrent
**BT-uTP** : BitTorrent傳輸的一種資料傳輸協定,建立在UDP上
`bt-dht contains "info_hash"`

```python=
`bt-dht contains "info_hash"`

import pyshark
cap = pyshark.FileCapture('torrent.pcap',display_filter = 'bt-dht contains "info_hash"')
info_hash_list = set()
for i in cap:
#print(i.layers[3])
info_hash = i.layers[3].get_field_by_showname('info_hash').showname_value ## 真的不知道則麼抓 info hash 跑去看了一下別人這行
info_hash_list.add(info_hash)
for j in info_hash_list:
print(j)
```
hash 值
d59b1ce3bf41f1d282c1923544629062948afadd
7af6be54c2ed4dcb8d17bf599516b97bb66c0bfd
17c1e42e811a83f12c697c21bed9c72b5cb3000d
17c02f9957ea8604bc5a04ad3b56766a092b5556
e2467cbf021192c241367b892230dc1e05c0580e
078e18df4efe53eb39d3425e91d1e9f4777d85ac
17c0c2c3b7825ba4fbe2f8c8055e000421def12c
17d62de1495d4404f6fb385bdfd7ead5c897ea22
### 參考
https://github.com/noamgariani11/picoCTF-2024-Writeup/blob/main/Forensics/Mob-psycho.md