0%

LitCTF 2023]snake

2024-04-17 03:23By
yuanshitianzun
PYCPythonXORpython

Problem: [LitCTF 2023]snake

[[toc]]

[LitCTF 2023]snake

下载过后是一个.pyc文件,所以用pycdc进行反汇编

由于我之前没有下载过pycdc所以我们先下载pycdc,在linux下载比较方便。

首先我们要安装cmake

apt install cmake

下载pycdc

git clone https://github.com/zrax/pycdc

切换到pycdc目录

cd pycdc

进行cmake

cmake CMakeLists.txt make

用法,可以查看

./pycdc -h

反汇编

./pycdc xx.pyc > xx.txt

回到这一题,我们运行之后,发现出现了Bad MAGIC!的错误

NSSIMAGE

额,搞不懂,不了解,看了看这位师傅(https://blog.csdn.net/fivesheeptree/article/details/131653707)的exp搞懂了

NSSIMAGE

NSSIMAGE

我们要补上42 0D 0D 0A

再次在pycdc执行,这次没有出问题

# Source Generated with Decompyle++ # File: game.cpython-37.pyc (Python 3.7) '''贪吃蛇''' import random import sys import time import pygame from pygame.locals import * from collections import deque SCREEN_WIDTH = 600 SCREEN_HEIGHT = 480 SIZE = 20 LINE_WIDTH = 1 SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1) SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1) FOOD_STYLE_LIST = [ (10, (255, 100, 100)), (20, (100, 255, 100)), (30, (100, 100, 255))] LIGHT = (100, 100, 100) DARK = (200, 200, 200) BLACK = (0, 0, 0) RED = (200, 30, 30) BGCOLOR = (40, 40, 60) def print_text(screen, font, x, y, text, fcolor = ((255, 255, 255),)): imgText = font.render(text, True, fcolor) screen.blit(imgText, (x, y)) def init_snake(): snake = deque() snake.append((2, SCOPE_Y[0])) snake.append((1, SCOPE_Y[0])) snake.append((0, SCOPE_Y[0])) return snake def create_food(snake): food_x = random.randint(SCOPE_X[0], SCOPE_X[1]) food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1]) while (food_x, food_y) in snake: food_x = random.randint(SCOPE_X[0], SCOPE_X[1]) food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1]) return (food_x, food_y) def get_food_style(): return FOOD_STYLE_LIST[random.randint(0, 2)] def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('贪吃蛇') font1 = pygame.font.SysFont('SimHei', 24) font2 = pygame.font.Font(None, 72) (fwidth, fheight) = font2.size('GAME OVER') b = True snake = init_snake() food = create_food(snake) food_style = get_food_style() pos = (1, 0) game_over = True start = False score = 0 orispeed = 0.5 speed = orispeed last_move_time = None pause = False while None: for event in pygame.event.get(): if event.type == QUIT: sys.exit() continue if event.type == KEYDOWN or event.key == K_RETURN or game_over: start = True game_over = False b = True snake = init_snake() food = create_food(snake) food_style = get_food_style() pos = (1, 0) score = 0 last_move_time = time.time() continue if not event.key == K_SPACE or game_over: pause = not pause continue if not (event.key in (K_w, K_UP) or b) and pos[1]: pos = (0, -1) b = False continue if not (event.key in (K_s, K_DOWN) or b) and pos[1]: pos = (0, 1) b = False continue if not (event.key in (K_a, K_LEFT) or b) and pos[0]: pos = (-1, 0) b = False continue if not event.key in (K_d, K_RIGHT) and b and pos[0]: pos = (1, 0) b = False screen.fill(BGCOLOR) for x in range(SIZE, SCREEN_WIDTH, SIZE): pygame.draw.line(screen, BLACK, (x, SCOPE_Y[0] * SIZE), (x, SCREEN_HEIGHT), LINE_WIDTH) for y in range(SCOPE_Y[0] * SIZE, SCREEN_HEIGHT, SIZE): pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), LINE_WIDTH) if not game_over: curTime = time.time() if not curTime - last_move_time > speed and pause: b = True last_move_time = curTime next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1]) if next_s == food: snake.appendleft(next_s) score += food_style[0] speed = orispeed - 0.03 * (score // 100) food = create_food(snake) food_style = get_food_style() elif next_s[0] <= next_s[0] or next_s[0] <= SCOPE_X[1]: pass else: SCOPE_X[0] elif next_s[1] <= next_s[1] or next_s[1] <= SCOPE_Y[1]: pass else: SCOPE_Y[0] elif next_s not in snake: snake.appendleft(next_s) snake.pop() else: game_over = True if not game_over: pygame.draw.rect(screen, food_style[1], (food[0] * SIZE, food[1] * SIZE, SIZE, SIZE), 0) for s in snake: pygame.draw.rect(screen, DARK, (s[0] * SIZE + LINE_WIDTH, s[1] * SIZE + LINE_WIDTH, SIZE - LINE_WIDTH * 2, SIZE - LINE_WIDTH * 2), 0) print_text(screen, font1, 450, 7, f'''得分: {score}''') if score > 1000: flag = [ 30, 196, 52, 252, 49, 220, 7, 243, 3, 241, 24, 224, 40, 230, 25, 251, 28, 233, 40, 237, 4, 225, 4, 215, 40, 231, 22, 237, 14, 251, 10, 169] for i in range(0, len(flag), 2): flag[i] = flag[i + 1] ^ 136 flag[i + 1] = flag[i] ^ 119 print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, bytes(flag).decode(), RED) pygame.display.update() if game_over and start: print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, 'GAME OVER', RED) pygame.display.update() if __name__ == '__main__': main()

里面是这样的代码

我们看主要代码

if score > 1000: flag = [ 30, 196, 52, 252, 49, 220, 7, 243, 3, 241, 24, 224, 40, 230, 25, 251, 28, 233, 40, 237, 4, 225, 4, 215, 40, 231, 22, 237, 14, 251, 10, 169] for i in range(0, len(flag), 2): flag[i] = flag[i + 1] ^ 136 flag[i + 1] = flag[i] ^ 119

这段代码对flag列表中的元素进行了异或(XOR)操作。具体而言,它遍历列表中每一对相邻元素,对每个偶数索引的元素执行flag[i] ^= 136,对每个奇数索引的元素执行flag[i + 1] ^= 119

这就好办了,我们把这些转变为ascall就行了,

exp

flag = [ 30, 196, 52, 252, 49, 220, 7, 243, 3, 241, 24, 224, 40, 230, 25, 251, 28, 233, 40, 237, 4, 225, 4, 215, 40, 231, 22, 237, 14, 251, 10, 169] for i in range(0, len(flag), 2): flag[i], flag[i + 1] = flag[i + 1] ^ 136, flag[i] ^ 119 for i in flag: print(chr(i),end='')

得到

LitCTF{python_snake_is_so_easy!}
还没有人赞赏,快来当第一个赞赏的人吧!
  
© 著作权归作者所有
加载失败
广告
×
评论区
添加新评论