DASCTF Oct X 吉林工师 逆向

魔法叠加

题目给了一个pyc和txt,先uncompyle6反编译一下,发现错误,那应该是pyc文件错误,修改文件头

11

题目这是2.7的版本,修改一下,1个字节0x73为TYPE_CODE字段, 表示该字段为string格式,这段就是字节码的位置,7102这个多了,删除掉,修改34为32.

12

修改好后可以正常反编译

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
37
38
39
40
41
42
43
44
45
46
47
# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
# Embedded file name: ./2.py
# Compiled at: 2021-10-20 11:56:04
# Size of source mod 2**32: 1928 bytes
import struct
O0O00O00O00O0O00O = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"']

def encode(O000O00000OO00OOO):
""""""
OOOO00OOO00O000OO = 0
OOOOOOOOOO00O0OOO = 0
OO0OOO000000OOOOO = ''
for O0O0OO0OOOOOOOO00 in range(len(O000O00000OO00OOO)):
O000O0OOOOO00O0O0 = O000O00000OO00OOO[O0O0OO0OOOOOOOO00:O0O0OO0OOOOOOOO00 + 1]
OOOO00OOO00O000OO |= struct.unpack('B', O000O0OOOOO00O0O0)[0] << OOOOOOOOOO00O0OOO
OOOOOOOOOO00O0OOO += 8
if OOOOOOOOOO00O0OOO > 13:
OO00O0OO00OOO000O = OOOO00OOO00O000OO & 8191
if OO00O0OO00OOO000O > 88:
OOOO00OOO00O000OO >>= 13
OOOOOOOOOO00O0OOO -= 13
else:
OO00O0OO00OOO000O = OOOO00OOO00O000OO & 16383
OOOO00OOO00O000OO >>= 14
OOOOOOOOOO00O0OOO -= 14
OO0OOO000000OOOOO += O0O00O00O00O0O0O0[(OO00O0OO00OOO000O % 91)] + O0O00O00O00O0O0O0[(OO00O0OO00OOO000O // 91)]

if OOOOOOOOOO00O0OOO:
OO0OOO000000OOOOO += O0O00O00O00O0O0O0[(OOOO00OOO00O000OO % 91)]
if OOOOOOOOOO00O0OOO > 7 or OOOO00OOO00O000OO > 90:
OO0OOO000000OOOOO += O0O00O00O00O0O0O0[(OOOO00OOO00O000OO // 91)]
return OO0OOO000000OOOOO


O0O00O00O00O0O0O0 = []
OO000O00O00O0O0O0 = []
O0O0O0O0000O0O00O = input('plz input O0O0O0O0000O0O00O:\n')
for i in range(0, 52):
O0O00O00O00O0O0O0 = O0O00O00O00O0O00O[i:] + O0O00O00O00O0O00O[0:i]
O0O0O0O0000O0O00O = encode(O0O0O0O0000O0O00O.encode('utf-8'))

dic = open('./00.txt', 'a')
dic.write(O0O0O0O0000O0O00O)
dic.close

看着上面的加密像base,除91,应该是base91https://github.com/aberaud/base91-python

51次base91,每一次的表都做了一次移位,直接抄脚本解

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding:utf-8 -*-

import struct

rawb91Maps = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',

'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',

'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$',

'%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=',

'>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"']

def decode(encoded_str):

''' Decode Base91 string to a bytearray '''

v = -1

b = 0

n = 0

out = b''

for strletter in encoded_str.decode():

if not strletter in b91Table:

continue

c = b91Table[strletter]

if v < 0:

v = c

else:

v += c * 91

b |= v << n

n += 13 if (v & 8191) > 88 else 14

while True:

out += struct.pack('B', b & 255)

b >>= 8

n -= 8

if not n > 7:

break

v = -1

if v + 1:

out += struct.pack('B', (b | v << n) & 255)

return out



b91MapArr = []

for i in range(0, 52):

b91MapArr.append(rawb91Maps[i:] + rawb91Maps[0:i])

b91MapArr.reverse()

f= open("/home/giantbranch/桌面/wasm/00.txt", "rb")

strs = f.read()



for i in range(0, 52):

b91Table = dict((v, k) for k, v in enumerate(b91MapArr[i]))

strs = decode(strs).decode()

print("round:", i)



print(strs)

3

马猴烧酒

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

strcpy(v12, "flag{this_is_fake_flag}");
*(_OWORD *)v18 = 0i64;
*(_OWORD *)Source = 0i64;
v20 = 0i64;
v21 = 0i64;
*(_OWORD *)Destination = 0i64;
*(__m128i *)v23 = _mm_load_si128((const __m128i *)&xmmword_140024A00);
v12[31] = 0;
v24 = _mm_load_si128((const __m128i *)&xmmword_1400249F0);
v15 = 0;
v22 = 0;
v17 = 0;
v9 = sub_14000B170(0i64);
v3 = sub_14000B170(&v9); // 获取时间戳
strcpy(Str2, "flag{");
v4 = v3;
*(_OWORD *)Str1 = 0i64;
sub_140001020("please input your flag\n");
sub_140001080("%s", Str1);
if ( strncmp(Str1, Str2, 5ui64) || Str1[15] != 125 )
goto LABEL_13;
sub_1400040D0("You find it?");
if ( IsDebuggerPresent() )
{
LABEL_15:
MessageBoxW(0i64, &Text, "-N璭", 0);
exit(1);
}
if ( v4 != 1633831810 )
goto LABEL_13;
strcpy((char *)&base64, "abcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZ");// base64
sub_14000C3F8(1633831810, (__int64)Source, 10);// 赋值
sub_1400010E0(Source); // 对时间戳base64加密
v5 = -1i64;
do
++v5;
while ( Source[v5] );
if ( v5 < 0x10 )
goto LABEL_13;
strncpy(Destination, Source, 0x10ui64); // source赋值给Destination
for ( i = 0i64; i < 16; ++i )
{
v12[i] ^= Destination[i]; // 异或得到key
if ( IsDebuggerPresent() )
goto LABEL_15;
}
v10 = 1;
sub_1400015F0((__int64)v11, (unsigned __int8 *)v12);
sub_140001910(v11, Str1, v18);
if ( !strncmp(v18, v23, 0x10ui64) )
v7 = "Good job!\nyou get the flag.\n";
else
LABEL_13:
v7 = "You entered the wrong flag,please try again.\n";
sub_140001020(v7);
sub_140005864("pause");
return 0;
}

先获取时间戳,如何base64变表,异或出key

魔改sm4

4

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

// sm4.h
static const unsigned char SboxTable[16][16] =
{
{0x48, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05},
{0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99},
{0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B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, 0xB0},
{0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84},
{0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0xD6}
};
static const unsigned long CK[32] =
{
0xF4BFE18F, 0xA8AA055C, 0x8B266D2B, 0xB3819D47, 0x0B1B3A85, 0xF7DB86B6, 0xC3279F82, 0x39D9C102,
0xBEA224C9, 0xE75D4DAC, 0xAC61726C, 0x6F98AA6F, 0xFA2ADA4E, 0x6A7CFF92, 0xA8066E7B, 0x7BE32F9F,
0x8CD0FED3, 0x4B98AF71, 0x790C2CBC, 0xBF880433, 0xAA46F582, 0x69C17A2C, 0x80BBD5E4, 0x24A02531,
0x293D87B3, 0x75F159AD, 0xB750AE9D, 0x9886928C, 0x05577A22, 0xB425E19F, 0x124D4F63, 0xE26F66D1
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <stdio.h>
#include "sm4.h"
int main()
{
sm4_context ctx;
unsigned char key[16] = {0x0B, 0x18, 0x18, 0x29, 0x16, 0x3A, 0x5E, 0x27, 0x1E, 0x2B, 0x5F, 0x3F, 0x32, 0x07, 0x5C, 0x56};
unsigned char enc[16] = {0xF7, 0xEB, 0x5E, 0x87, 0x17, 0x9C, 0x74, 0x94, 0x44, 0xB5, 0xF5, 0x12, 0xF9, 0x74, 0x15, 0x5F};
unsigned char dec[16];

sm4_setDecryptKey(&ctx, key);
sm4_CryptoEcb(&ctx, 0, 16, enc, dec);
for (int i = 0; i < 16; i++)
printf("%c", dec[i]);
printf("\n");
return 0;
}