PYTHON
講師:堇姬 @ Izcc-ctf
2023.10.6
堇姬Naup(網管/美宣)
成電二年級/幽夜工作室繪師/台灣好厲駭學員
CKCSC36
DC : naup_sumire_hime
IG : ckcsc36th_naup
涉獵C++、C、python、遊戲(tkinter、pygame)、資安(Web、Crypto)、AI、flask、html/css/js、 PHP、DC bot。
喜歡看輕小說、動畫、Vtuber、打音遊,也喜歡看百合,就是一個長年混跡ACG的宅女。
夢想是可以成為很電的駭客跟繪師,也想自己寫出一個AI老婆。
簡單規劃一下課程
- python
- linux
-
web
- 基礎概念&開發者工具
- xss
- 模擬請求
- path traversal
- SQL injection
- 一個RCE
-
crypto
-
Block Cipher
- ECB
- CBC
- RSA
-
Block Cipher
9/22
10/20 python
10/27 linux
11/3 -
11/10 |
11/17 |
|WEB
12/1 |
12/8 -
12/15 -
12/22 | CRYPTO
12/29 -
while迴圈
a=1
while a<12:
print(a)
a+=1
while <條件> :
要執行的程式碼
1
2
3
4
5
6
7
8
9
10
11
while True:
print("I LOVE LOLI")
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
I LOVE LOLI
...
for迴圈
for 變數 in 容器:
要執行的函式
d=[0, 1, 2, 3, 4, 5]
for i in d:
print(i)
0
1
2
3
4
5
range(起始值, 終止值, 遞增(減)值)
可以創建一個列表
range(10):0,1,2,3,4,5,6,7,8,9
range(1, 11):1,2,3,4,5,6,7,8,9,10
range(0, 10, 2):0,2,4,6,8
for i in range(5):
print(i)
0
1
2
3
4
各種資料結構
是由一堆元素所組成的資料結構
-
串列(List)->[]
可排序、可修改、可重複
-
元組(Tuple)->()
可排序、不可修改、可重複
-
字典(Dictionary)->{ "key" : "value" }
不可排序、可修改、不可重複
-
集合(Set)->{}
不可排序、無索引、不可重複
a=['水銀燈', '真紅' ] #List
b=('八六', '右田日日姬', 123) #Tuple
c={1:'一月', 2:'二月', 3:'三月'} #Dictionary
d={'常陸茉子', '朝武芳乃',"叢雨", 123, 123} #Set
print(a[1])
print(b[2])
print(c[3])
for i in d:
print(i)
函式(function)
輸入資料給它就會產生結果的東西
abc
輸入(input)
輸出(output)
函式
def abc(a):
return a+1
print(abc(2))
輸出3
函式名字
參數
回傳
呼叫函式
Try...except
try:
a = input('輸入數字:')
print(a + 1)
except:
print('發生錯誤')
print('hello')
當 try 區段內的程式發生錯誤時,就會執行 except 裡的內容,如果 try 的程式沒有錯誤,就不會執行 except 的內容
bytes
python的byte 表示的是多個以一個byte為單位組成的東西 而每個byte可以是0~255。
a = b'Hello, world!'
b = bytes([65, 66, 67, 68, 69])
c = "Hello, world!".encode()
print(type(a),type(b),type(c))
如何創建bytes
a=b'ABCDE'
print(a[1])
可以用索引抓到他
bytearray
bytes是不可變,但可以轉成bytearray來改動他
a=bytearray(b"aaaa")
a[1]=67
print(a)
好用的函式
-
strip()
-
split()
指定分隔字符對字串進行切片
移除字串頭尾指定的字符,莫認為空格
a="bbaabbb"
print(a.strip("b"))
a="abaaabh"
print(a.split("b"))
-
eval()
-
map()
執行字串符
def a(b,c):
return b+c
print(eval("1+a(1,2)"))
對指定序列做映射
def f(x):
return x**2
print(list(map(f,[1,2,3,4,5])))
def f(x):
return x**2
print(list(map(str,[1,2,3,4,5])))
-
encode()
-
decode()
已指定方式對字串符進行編碼
已指定方式對字串符進行解碼
a="abaaabh"
print(a.encode())
a=b"abaaabh"
print(a.decode())
-
pow()
pow(x,y)
pow(x,y,z)
-
hex()
十進制轉十六進制
print(hex(2))
-
long_to_bytes()
-
bytes_to_long()
bytes轉ASCII
ASCII轉bytes
pip install pycryptodome
from Crypto.Util.number import *
from Crypto.Util.number import *
print(bytes_to_long(b"1"))
from Crypto.Util.number import *
print(long_to_bytes(49))
位元運算
-
AND(&)
8 & 29 = 8
8 (Decimal) = 01000 (Binary)
29 (Decimal) = 11101 (Binary)
01000 & 11101 = 01000 -> 8
print(8 & 29)
-
OR( | )
8 | 29 = 8
8 (Decimal) = 01000 (Binary)
29 (Decimal) = 11101 (Binary)
01000 | 11101 = 11101 -> 29
print(8 | 29)
-
XOR( ^ )
8 ^ 29 = 8
8 (Decimal) = 01000 (Binary)
29 (Decimal) = 11101 (Binary)
01000 ^ 11101 = 10101 -> 21
print(8 ^ 29)
XOR特性
-
交換律
p ^ q = q ^ p
-
結合律
p ^ q ^ n = p ^ ( q ^ n )
-
恆等律
p ^ 0 = p
-
歸零律
p ^ p = 0
-
自反
p ^ p ^ q = q
-
<<
number = 1
print(number)
print(number << 1)
print(number << 2)
print(number << 3)
00000001 → 1
00000010 → 2
00000100 → 4
00001000 → 8
00000010 → 2
00000100 → 4
00001000 → 8
左移指定位數,左邊被擠出去的位元會被丟棄,而右邊補上 0
-
>>
a = 60 # 60 = 0011 1100
c = a >> 2; # 15 = 0000 1111
print(c)
會將所有位元往右移指定位數,右邊被擠出去的位元會被丟棄
PORT(連接埠)
用於實現不同設備之間的連接和數據傳輸,並確保數據被正確傳輸到目的地,並且可以用來區分不同服務
通訊埠號
可用不同的數字來分辨不同的通訊埠,而小於 1024 號的每個埠通常都有自己預設使用的portocol。(0~65535)
-
20 和 21:檔案傳輸通訊協定 (FTP)。
-
22:安全殼層 (SSH)。
-
25:簡易郵件傳輸通訊協定 (SMTP)。
-
53:Domain Name System (DNS)。
-
80:超文字傳輸通訊協定 (HTTP)。
-
443:HTTP 安全 (HTTPS)。
pwntools
pip install pwntools
from pwn import *
r=remote("ckcsc.net",20000)
r.interactive()
remote() 遠端連線到ckcsc.net,port是20000
- r.send(payload) : 送出 payload
- r.sendline(payload) : 送出 payload 並換行
- r.sendafter(p1, p2) : 接收到 p1 之後送出 p2
- r.sendlineafter(p1, p2) : 接收到 p1 之後送出 p2 並換行
-
r.recv(n) : 接收 n 個 bytes
-
r.recvuntil(payload) : 接收到 payload 為止
-
r.recvline() : 接收一行(到換行為止)
-
r.recvlines(n) : 接收 n 行
送出資料
接收資料
-
r.interactive() : 切換到互動模式
-
r.close() : 關閉連線
其他用法
r.interactive()
一但你與目標建立了連結,你就可以使用這個來和目標進行交互,就像你再用terminal直接跟目標系統交互一樣
-
發送命令
-
接收輸出
...
練習
welcome
直接nc即可
from pwn import *
r=remote("lab.scist.org", 33340)
r.interactive()
或是使用pwntools連上lab.scist.org 33340
Count
from pwn import *
r=remote("lab.scist.org", 33342)
r.recvlines(3)
for i in range(1,101):
n=r.recvline(1)
print(n)
r.sendline(str(i))
r.interactive()
要從1輸入到100
solve.py
Nth
分析一下他要找到第nth大的數字並送出
from pwn import *
r=remote("lab.scist.org", 33341)
r.recvlines(8)
f=r.recvline().strip().split(b":")[1]
m=sorted(list(map(int,f.split())))
nth=int(r.recvline().strip().split(b":")[1])
r.sendlineafter(b"answer >",str(m[len(m)-nth]).encode())
r.interactive()
solve.py
Guess
分析一下他要在1到1073741824中找到一個數字是正確答案定送出,他會回傳這個達二太大或太小。
暴力查找會太久所以要使用二分搜來找到答案
from pwn import *
r=remote("lab.scist.org",33343)
left=0
right=1073741824
r.recvlines(2)
while True:
mid= (left+right)//2
r.sendlineafter(b'guess >',str(mid).encode())
ans=r.recvline().strip()
print(ans)
if ans==b"Too small !":
left=mid
elif ans==b"Too big !":
right=mid
else:
break
r.interactive()
solve.py
Calculator
簡單看一下,他會給你一行算式,但你不知道他是+ - 或是 *,所以你可以暴力去看是哪個在輸出就行了
from pwn import *
r=remote("lab.scist.org", 33344)
r.recvlines(3)
for i in range(100):
Qu=r.recvline()
print(Qu)
c=r.recvline().strip().split()
num1=int(c[0].decode())
num2=int(c[2].decode())
num3=int(c[4].decode())
if num1+num2==num3:
r.sendlineafter(b"which operator (+/-/*) ?",b"+")
elif num1-num2==num3:
r.sendlineafter(b"which operator (+/-/*) ?",b"-")
else:
r.sendlineafter(b"which operator (+/-/*) ?",b"*")
r.interactive()
solve.py
python ctf
By naup96321
python ctf
- 102