---
###### tags: `資訊之芽`
---
# pwntools
![](https://i.imgur.com/c7Pn9ym.jpg =300x)
src: 天下女人心 某一集
資芽北區2023 講師YuKai 洪郁凱
----
# Outline
- Intro
- Classic Crypto
- Modern Crypto
- Practice
---
# Before we start...
請不要觸法!
【刑法】36章——妨害電腦使用罪
【個人資料保護法】
[ref](https://isms.yuntech.edu.tw/index.php?option=com_content&task=view&id=11544&Itemid=3201)
---
# Intro
除了Crypto之外...
- Socket 溝通
- asm 反編譯
- gdb 偵錯
- elf
- ROP Chain
----
# Intro
What is CTF
Capture the Flag
----
# Intro
What is CTF
(jeopardy-style challenges)
- 理解題目如何隱藏Flag
- 找出解密的方法
- 還原Flag
----
# Intro
What is CTF
- Web
- Pwn
- Crypto
- Reverse
- Misc
----
# Intro
`format:FLAG{...}`
```python=
flag = "" # Unknown
cipher = flag[::-1]
print(cipher) # "}5u03gr0G_5i_n1aK{GALF"
```
----
# Intro
`format:FLAG{...}`
```python=
flag = "" # Unknown
cipher = flag[::-1]
print(cipher) # "}5u03gr0G_5i_n1aK{GALF"
```
answer: `FLAG{Ka1n_i5_G0rg30u5}`
----
# Intro
[picoCTF](https://picoctf.org)
---
# Crypto
密碼學 Cryptography
`study of techniques for secure communication in the presence of adversarial behavior.`
1. Hash functions (Unkeyed)
2. Symmetric-key cryptography
3. Public-key cryptography
----
# Crypto
凱薩密碼 Caesar
assume key = 2
```
"A" => "C"
"B" => "D"
"X" => "Z"
"Y" => "A"
...
```
----
# Crypto 練習
給定字串跟一個key(-25~25)
根據key產生/解開結果字串
----
# Crypto 練習
Hint:
```python=
str.upper()
ord("A")
chr(65)
```
----
# Crypto 練習
試著加密以下字串
```python=
"Sprout" # Key = 3
"Taylor Swift" # Key = 13
"Gnlybe Fjvsg" # Key = 13
```
When key = 13, it's ROT13!
----
# Crypto
`picoCTF{...}`
[link](https://play.picoctf.org/practice/challenge/373?page=1&search=ROT)
![](https://hackmd.io/_uploads/ByGU1DDr3.png)
----
# Crypto
凱薩密碼 Caesar
Weakness
- Key能被爆蒐
- 字頻分析
----
# Register
Use your code to solve registration problem!
[Practice website](https://cryptohack.org)
---
# PlayFair
https://en.wikipedia.org/wiki/Playfair_cipher
[PlayNice](https://play.picoctf.org/practice/challenge/114?category=2&originalEvent=34&page=1)
----
# PlayFair
將0-9,a-z隨意塞在6x6的方格內
當作加密的key
----
# PlayFair
將要加密的英數字串全轉為小寫
並拆成兩兩一組,按照下列規則插入X
- 若一組內有疊字,在兩字之中插入X
- 若有不成組的字,在之後插入X
----
# PlayFair
舉例:
happy2023
ha px py 2023
----
# PlayFair
開始加密!
![](https://i.imgur.com/PRT1wlC.png =25%x)
----
# PlayFair
每組字都可能:
- 在不同行與列
- 在同行不同列
- 在同列不同行
----
# PlayFair
在不同行與列
各自換成同列的另個角落
ha => do
px => lu
23 => 6b
![](https://i.imgur.com/PRT1wlC.png =25%x)
----
# PlayFair
在同列不同行
各自換成同列右一個字
ut => xb
![](https://i.imgur.com/PRT1wlC.png =25%x)
----
# PlayFair
在同行不同列
各自換成同行下一個字
p1 => 1u
![](https://i.imgur.com/PRT1wlC.png =25%x)
----
# PlayFair
最後把各組字接起來就完成了!
`doluh1wb6b`
腦力激盪:怎麼把他還原呢?
----
# PlayFair Hint
不同行不同列 => 不同行不同列
同行不同列 => 同行不同列
不同行同列 => 不同行同列
----
# PlayFair Hint
不同行不同列 => 一樣找各自角落
同行不同列 => 往上找
不同行同列 => 往左找
---
# PlayNice 實戰
從伺服器拿到加密用的訊息
試圖解出正確密碼並拿到Flag!
----
# PlayNice 實戰
![](https://i.imgur.com/Lm0b37b.png)
----
# PlayNice 實戰
1. nc
2. pwntools recv/send
3. PlayNice decryption
----
# nc
連上遠端機器用的工具
`nc mercury.picoctf.net 21003`
----
# pwntools recv/send
pwntools也有對應的工具幫忙收發訊息
```python=
from pwn import *
conn = remote('mercury.picoctf.net', 21003)
alphabet = conn.recvuntil("\n") # 接收直到換行
# alphabet = conn.recvline()
alphabet = alphabet.decode() # 將Byte轉換成字串 方便操作
...
conn.sendline(msg) # 放字串
```
----
# PlayNice decryption
看扣時間!
- generate_square
- encrypt_string
----
# PlayNice decryption
修改加密步驟
- 向右 => 向左
- 向下 => 向上
----
# PlayNice decryption
得到Flag了!
---
# RSA
超有名且泛用的加密方式
(應該只能大致講過)
----
# RSA
- Ron **R**ivest
- Adi **S**hamir
- Leonard **A**dleman
----
# RSA
為何有用?
:因數分解很難算
----
# RSA
運作方式
```
Alice <--明文訊息-- Bob
```
不安全!
----
# RSA
```
Alice --公鑰--> 其他人 + Bob
(拿著私鑰)
```
Alice負責生出公私鑰
發給其他人鎖頭
----
# RSA
```
Alice <--用公鑰加密後的訊息-- + Bob
(拿著私鑰)
```
Alice用私鑰解密訊息
**全世界只有Alice有私鑰能解密!**
---
# RSA
Alice如何製作公私鑰?
步驟:
- 挑兩個很大的質數(p和q)
- 計算r = (p-1)(q-1), N = pq
- 選一個比r小的整數e,並確保e,r互質
- 找整數d,使得ed % N = 1
- 完成!公鑰(N,e),私鑰(N,d)
----
# RSA
- 如何加解密
- 為什麼能用公鑰加密,私鑰解密
----
# RSA
加密
假設要傳送一串數字c,公鑰$(N,e)$
加密後為$enc=c^{e}{\bmod {N}}$
----
# RSA
解密
收到加密後的$enc$後,私鑰$(N,d)$
還原數字$c={enc}^{d}{\bmod {N}} = {c}^{ed}{\bmod {N}}$
----
# RSA
為何能解密?
$c = {enc}^{d}{\bmod {N}} = enc=c^{ed}{\bmod {N}}$
$ed{\bmod {N}} = 1, ed - 1$為N的倍數
----
# RSA
為何能解密?
Euler定理:$a^{c*N}{\bmod {N}} = 1$
a跟N互質,c為任意整數
$c^{ed}{\bmod {N}} = c * c^{hN} {\bmod {N}} = c * 1 {\bmod {N}}$
更多正確性細節可以參考[Wiki](https://zh.wikipedia.org/zh-tw/RSA加密演算法)
---
# RSA練習
[link](https://play.picoctf.org/practice/challenge/188?page=1&search=RSA)
![](https://hackmd.io/_uploads/Hkxh4CNVh.png)
----
# RSA練習
`flag ** 3 mod N = enc`
`flag ** 3 = t*N + enc`
----
# RSA練習
判斷是否為e次方的數字
Tool:`gmpy2.iroot(n, e)`
---
# 其他
你們好棒!
{"metaMigratedAt":"2023-06-18T00:51:20.090Z","metaMigratedFrom":"Content","title":"pwntools","breaks":true,"contributors":"[{\"id\":\"9a14c697-53b2-4214-a12a-512e946e0176\",\"add\":5354,\"del\":530}]"}