Command Cheat Sheet
Python 基本用語
- Bytes $\to$ Hex
1
2
3>>> example_str = b'\x17\x10\x06Ar\xe4G\xc9\xb5\xd7y\xbc' >>> example_str.hex() '1710064172e447c9b5d779bc'
- Hex $\to$ String
1
2
3
4
5
6
7
8
9
10
11
12>>> bytes.fromhex('68656c6c6f').decode('utf-8') 'hello' >>> # or >>> import binascii >>> binascii.unhexlify('68656c6c6f') b'hello" >>> # or >>> import codecs >>> decode_hex = codecs.getdecoder("hex_codec") >>> decode_hex(s)[0] b'hello'
- String $\to$ Hex
1
2
3>>> str= 'linuxhint'.encode('utf-8') >>> str.hex() '6c696e757868696e74'
- Hex(String Type) $\to$ Decimal
1
2
3>>> a = '123456' >>> int(a, 16) 1193046
- Decimal $\to$ Hex
1
2
3>>> a = 1234 >>> hex(a) '0x4d2'
- Hex $\to$ Binary
1
2>>> bin(int('abc', 16))[2:].zfill(8) '101010111100'
- String $\to$ Binary
:::info
if you’d like to do this transformation, 1st conversion is better
:::
1
2
3
4
5# string to hex to binary >>> bin(int('I love CNS'.encode('utf-8').hex(), 16))[2:].zfill(8) '1001001001000000110110001101111011101100110010100100000010000110100111001010011' >>> ''.join(format(ord(x), 'b') for x in 'I love CNS') '10010011000001101100110111111101101100101100000100001110011101010011'
- Byte $\to$ String
1
2>>> b'abc\n'.decode('utf-8') 'abc\n'
- Binary $\to$ Hex
1
2
3
4>>> hex(int('010110', 2)) '0x16' >>> hex(int('0000010010001101', 2)) '0x48d'
- Binary $\to$ Hex $\to$ String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37def binToHexa(n): bnum = int(n) temp = 0 mul = 1 count = 1 hexaDeciNum = ['0'] * 100 i = 0 while bnum != 0: rem = bnum % 10 temp = temp + (rem*mul) if count % 4 == 0: if temp < 10: hexaDeciNum[i] = chr(temp+48) else: hexaDeciNum[i] = chr(temp+55) mul = 1 temp = 0 count = 1 i = i+1 else: mul = mul*2 count = count+1 bnum = int(bnum/10) if count != 1: hexaDeciNum[i] = chr(temp+48) if count == 1: i = i-1 hex_string = '' while i >= 0: hex_string += hexaDeciNum[i] i = i-1 if hex_string == '': hex_string = '00' return hex_string plaintext_hex = binToHexa(plaintext_bin).encode().hex() print(bytes.fromhex(plaintext_hex).decode())
- Decimal(int type) $\to$ Hex(String type)
1
2
3
4
5
6>>> '{0:0>2x}'.format(0) '00' >>> '{0:0>2x}'.format(255) 'ff' >>> '{:x}'.format(290275030195850039473456618367455885069965748851278076756743720446703314517401359267322769037469251445384426639837648598397) '7069636f4354467b6d347962335f54683073655f6d337335346733735f3472335f646966757272656e745f313737323733357d'
- String(
str
type) $\iff$ Decimal>>> chr(97) 'a' >>> ord('a') 97
- Decimal $\to$ Binary
>>> bin(10) '0b1010'
- Decimal $\to$ Bytes Type
1
2
3
4>>> bytes([10]) b'\n' >>> bytes([70]) b'F'
- Array $\to$ List
>>> import numpy as np >>> a = np.array([1,2,3]) >>> a.tolist() [1, 2, 3]
Python 組合技
- XOR Two Decimal
1
2
3
4
5
6
7
8
9
10>>> from itertools import cycle >>> def hex_xor(s1, s2): ... b = bytearray() ... for c1, c2 in zip(bytes.fromhex(s1), cycle(bytes.fromhex(s2))): ... b.append(c1 ^ c2) ... return b.hex() >>> s1 = 'aaab' >>> s2 = 'ccbc' >>> hex_xor(s1, s2) '6617'
- Decimal $\to$ Ascii String
1
2
3>>> tmp = 4028375274964940959047587304025089628177332141172593013450629550958369516176531641246900741346661851279741 >>> bytes.fromhex('{:x}'.format(tmp)).decode('utf-8') 'picoCTF{p0ll4rd_f4ct0r1z4at10n_FTW_7c8625a1}'
Python 酷酷的寫法
- string倒續輸出
1
2>>> "galf"[::-1] 'flag'
- 取2的補數(取有號數的負號)
1
2
3
4
5
6
7>>> import ctypes >>> a = 0x17c7cc6e >>> ctypes.c_int32(a).value 398969966 >>> b = 0xc158a854 >>> ctypes.c_int32(b).value -1051154348
Linux 奇技淫巧語法
- 設定英文+數字的亂碼 - Linux - tr語法
$ tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 16
- 計算行數 - Linux 使用 wc 指令計算字數、行數教學與範例
1
2$ cat document.xml | wc -l 10
- 透過搜尋特定字串找尋哪一個file含有相關字串 - CyberDefenders: Spotlight
1
2$ grep -i -r '{specific strings}' * # grep -i -r 'eno' *
- sort - Linux 的 sort 排序指令教學與常用範例整理
1
2# 倒序 $ ls -l | sort -r
- uniq - Linux 的 uniq 指令教學與範例:刪除重複文字行、去除相同的內容
- cut - Linux 的 cut 擷取部份字元、欄位指令教學與常用範例整理
1
2# 用key word切分,再用-f選出要顯示哪一塊 $ cut -d {key word} -f {number} # cut -d '"' -f 6
- diff - Linux diff 命令
1
2
3
4
5
6
7
8
9
10
11
12
13$ diff log2013.log log2014.log -y -W 50 2013-01 2013-01 2013-02 2013-02 2013-03 | 2014-03 2013-04 2013-04 2013-05 2013-05 2013-06 2013-06 2013-07 2013-07 2013-08 | 2013-07 2013-09 2013-09 2013-10 2013-10 > 2013-11 > 2013-12
-y
或--side-by-side
: 以並列的方式顯示文件的異同之處</br>-W<寬度>
或--width<寬度>
: 在使用-y
參數時,指定欄寬</br>|
表示前後2個文件內容有不同</br><
表示後面文件比前面文件少了1行內容</br>>
表示後面文件比前面文件多了1行內容