Hello World

Hello World

第 N 次开坑, 希望这次持久一些。 测试一下代码高亮。
预计数分期中后更新 lug-hackergame-2020 的 writeup。

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
#!/usr/bin/env python3

from Crypto.Cipher import AES
import os
from hashlib import sha256
from utils import *


def talk_to_Alice():
name = bytes.fromhex(input("What's your name? "))
extra = bytes.fromhex(input("What else do you want to say? "))
msg = b"Thanks " + name + b" for taking my flag: " + flag + extra
plaintext = msg + sha256(msg).digest()
iv = os.urandom(AES.block_size)
aes = AES.new(AES_key, AES.MODE_CBC, iv)
print("This is my encrypted message, please take it to Bob:")
print((iv + aes.encrypt(pad(plaintext))).hex())


def talk_to_Bob():
try:
ciphertext = bytes.fromhex(input("Show me your message from Alice: "))
iv = ciphertext[: AES.block_size]
aes = AES.new(AES_key, AES.MODE_CBC, iv)
plaintext = unpad(aes.decrypt(ciphertext[AES.block_size :]))
assert sha256(plaintext[:-32]).digest() == plaintext[-32:]
print("Thanks")
except:
print("What's your problem???")


if __name__ == "__main__":
flag = open("flag1").read().encode()
AES_key = os.urandom(16)
while True:
choice = input("Whom do you want to talk to? ")
if choice == "Alice":
talk_to_Alice()
elif choice == "Bob":
talk_to_Bob()

… 似乎对 C 的支持不那么好

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <limits.h>
#include <assert.h>

#define EMPTY 0
#define BLACK 1
#define WHITE 2
// Send payload to server to get flag plz
#define FLAG "\x76\x43\x49\x4C\x09\x5A\x4A\x55" \
"\x41\x41\x4E\x54\x11\x46\x5C\x14" \
"\x46\x53\x45\x4E\x5C\x48\x1B\x48" \
"\x52\x1E\x58\x25\x35\x62\x25\x28" \
"\x24\x21\x67\x38\x25\x30"

#define COMPUTER BLACK
#define HUMAN WHITE

#define MIN_SCORE -100

char flag[] = FLAG;
int board[3][3] = {};

void flag_decode(void) {
for (int i = 0; flag[i] != '\0'; i++) {
flag[i] ^= (i + 23333);
}
}

int check(void) {
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != EMPTY) {
return board[i][0];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != EMPTY) {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != EMPTY) {
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != EMPTY) {
return board[0][2];
}
return false;
}

bool full(void) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}

void print(void) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
char out = '_';
if (board[i][j] == WHITE) {
out = 'X';
} else if (board[i][j] == BLACK) {
out = 'O';
} else if (board[i][j] != EMPTY) {
out = '?'; // well if you see this something may be wrong.
}
printf("%c", out);
}
puts("");
}
}

int opp(int p) {
if (p == BLACK)
return WHITE;
else if (p == WHITE)
return BLACK;
assert(0);
}

int minimax(int player) {
int winColor = check();
if (winColor != EMPTY) {
return winColor == player ? 1 : -1;
}

int movex = -1, movey = -1;
int max_score = MIN_SCORE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = player;
int score = -minimax(opp(player));
if (score > max_score) {
max_score = score;
movex = i; movey = j;
}
board[i][j] = EMPTY;
}
}
}
if (movex == -1) {
return 0;
}
return max_score;
}

void ai(int *x, int *y) {
// Make sure that human cannot win
// I heard that there's an algorithm named "Minimax"
int movex = -1, movey = -1;
int max_score = MIN_SCORE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = COMPUTER;
int score = -minimax(HUMAN);
if (score > max_score) {
max_score = score;
movex = i; movey = j;
}
board[i][j] = EMPTY;
}
}
}
*x = movex;
*y = movey;
}

int main(void) {
setvbuf(stdin, 0, 2, 0);
setvbuf(stdout, 0, 2, 0);

bool success = false; // human wins?
char input[128] = {}; // input is large and it will be ok.

puts("Welcome to Tic Tac Toe! Computer first!");
puts("You're 'X' and I'm 'O'!");
while (!success) {
// computer: BLACK
int x, y;
puts("Now computer goes!");
ai(&x, &y);
board[x][y] = BLACK;
print();
if (check()) {
success = false;
break;
}
if (full()) {
break;
}
puts("Now human goes!");
// human: WHITE

// example input: (0,1)
while (true) {
printf("Your turn. Input like (x,y), such as (0,1): ");
gets(input);
x = input[1] - '0';
y = input[3] - '0';
printf("You wanna put X on (%d,%d)...\n", x, y);
if (!(x >= 0 && x < 3 && y >= 0 && y < 3)) {
puts("Wrong input! Please try again!");
continue;
}
if (board[x][y] != EMPTY) {
puts("This pos has already been occupied!");
continue;
}
break;
}
board[x][y] = WHITE;
print();
if (check()) {
success = true;
break;
}
if (full()) {
break;
}
}
if (success) {
puts("What? You win! Here is your flag:");
flag_decode();
puts(flag);
} else {
puts("You failed! See you next time~");
}
return 0;
}
作者

hang333

发布于

18-11-2020

更新于

21-11-2020

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×