2.12(reverse)---pyc文件反编译

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 一. python相关知识
    • 1. 解释型语言与编译型语言
  • 二. pyc文件逆向
    • 1. pyc 文件简介
    • 2. pyc文件结构
    • 3. pyc 文件的生成
    • 4. pyc文件反编译
    • 5. python字节码解读
  • 三. attachment.pyc
  • 四. VNCTF2022 BabyMaze WP


一. python相关知识

1. 解释型语言与编译型语言

  • 把高级语言翻译成机器语言,计算机才能运行高级语言所编写的程序。翻译的方式有两种,一个是编译,一个是解释
  • 编译型语言写的程序执行之前,需要一个专门的编译过程
  • 解释型语言没有严格编译汇编过程,由解释器将代码块按需要变运行边翻译给机器执行
  • .pyc是可以由虚拟机直接执行的,是python将目标源码编译成字节码以后在磁盘上的文件形式。

二. pyc文件逆向

1. pyc 文件简介

  • pyc是一种二进制文件,是由Python文件经过编译后所生成的文件,它是一种字节码文件,Python文件变成pyc文件后,加载的速度有所提高,最终经过python解释器转换成机器码运行
  • Python是解释型语言,没有严格意义上的编译和汇编过程。但是一般编写好的.py源文件,由python解释器翻译成以.pyc为结尾的字节码文件,该文件可由python虚拟机直接运行
  • 根据python源码中提供的opcode,可以根据pyc文件反编译出py文件源码

2. pyc文件结构

  • Python代码的编译结果就是PyCodeObject对象,pyc文件就是PyCodeObject对象在硬盘上的保存形式
  • pyc文件结构主要包括两部分:pyc文件头部表示和PyCodeObject对象部分。
  • 下面就是完整的Pyc文件结构
    2.12(reverse)---pyc文件反编译_第1张图片
    2.12(reverse)---pyc文件反编译_第2张图片

3. pyc 文件的生成

  • 方法一:命令行
python3.6 -m py_compile test.py
  • 方法二:脚本:
>>> import py_compile  
>>> py_compile.compile('test.py')

4. pyc文件反编译

  • 在命令行中,uncompyle6加上需要反编译的文件参数即可,注意使用-o 选项输出到文件中
uncompyle6 -o test.py test.pyc
  • 这会将test.pyc文件反编译,并输出到test.py文件中
    在这里插入图片描述
  • 可以将这个长路径加入环境变量,下次用直接输uncompyle6即可

5. python字节码解读

  • 博客园搜python逆向

    https://www.cnblogs.com

三. attachment.pyc

  • 按照之前的步骤进行反编译,得到.py文件
  • 看一下他的代码
print 'Welcome to Re World!'
print 'Your input1 is your flag~'
l = len(input1)
for i in range(l):
    num = ((input1[i] + i) % 128 + 128) % 128
    code += num

for i in range(l - 1):
    code[i] = code[i] ^ code[i + 1]

print code
code = ['\x1f', '\x12', '\x1d', '(', '0', '4', '\x01', '\x06', '\x14', '4', ',', 
 '\x1b', 'U', '?', 'o', '6', '*', ':', '\x01', 'D', ';', '%', '\x13']
  • 那么我们只需编写python脚本就能解密
code = ['\x1f', '\x12', '\x1d', '(', '0', '4', '\x01', '\x06', '\x14', '4', ',', '\x1b', 'U', '?', 'o','6', '*', ':', '\x01', 'D', ';', '%', '\x13']
flag = ""
for i in range(len(code) - 2, -1, -1):
    code[i] = chr(ord(code[i]) ^ ord(code[i + 1]))
for j in range(len(code)):
    flag += chr(((ord(code[j]) - j)+128) % 128)
print(flag)
  • 解得flag{Just_Re_1s_Ha66y!}

四. VNCTF2022 BabyMaze WP

  • 拿到题,第一反应是用uncompyle6来直接逆,不成功
    2.12(reverse)---pyc文件反编译_第3张图片
  • 发现有花指令
  • 用010编辑器打开.pyc文件
    2.12(reverse)---pyc文件反编译_第4张图片
  • 得出JUMP_ABSOLUTE的机器码为113即0x71,直接删除他
  • 下面就是字节码对应机器码的数字,即opcode.h
#ifndef Py_OPCODE_H
#define Py_OPCODE_H
#ifdef __cplusplus
extern "C" {
#endif


/* Instruction opcodes for compiled code */

#define STOP_CODE   0
#define POP_TOP     1
#define ROT_TWO     2
#define ROT_THREE   3
#define DUP_TOP     4
#define ROT_FOUR    5
#define NOP     9

#define UNARY_POSITIVE  10
#define UNARY_NEGATIVE  11
#define UNARY_NOT   12
#define UNARY_CONVERT   13

#define UNARY_INVERT    15

#define BINARY_POWER    19

#define BINARY_MULTIPLY 20
#define BINARY_DIVIDE   21
#define BINARY_MODULO   22
#define BINARY_ADD  23
#define BINARY_SUBTRACT 24
#define BINARY_SUBSCR   25
#define BINARY_FLOOR_DIVIDE 26
#define BINARY_TRUE_DIVIDE 27
#define INPLACE_FLOOR_DIVIDE 28
#define INPLACE_TRUE_DIVIDE 29

#define SLICE       30
/* Also uses 31-33 */

#define STORE_SLICE 40
/* Also uses 41-43 */

#define DELETE_SLICE    50
/* Also uses 51-53 */

#define STORE_MAP   54
#define INPLACE_ADD 55
#define INPLACE_SUBTRACT    56
#define INPLACE_MULTIPLY    57
#define INPLACE_DIVIDE  58
#define INPLACE_MODULO  59
#define STORE_SUBSCR    60
#define DELETE_SUBSCR   61

#define BINARY_LSHIFT   62
#define BINARY_RSHIFT   63
#define BINARY_AND  64
#define BINARY_XOR  65
#define BINARY_OR   66
#define INPLACE_POWER   67
#define GET_ITER    68

#define PRINT_EXPR  70
#define PRINT_ITEM  71
#define PRINT_NEWLINE   72
#define PRINT_ITEM_TO   73
#define PRINT_NEWLINE_TO 74
#define INPLACE_LSHIFT  75
#define INPLACE_RSHIFT  76
#define INPLACE_AND 77
#define INPLACE_XOR 78
#define INPLACE_OR  79
#define BREAK_LOOP  80
#define WITH_CLEANUP    81
#define LOAD_LOCALS 82
#define RETURN_VALUE    83
#define IMPORT_STAR 84
#define EXEC_STMT   85
#define YIELD_VALUE 86
#define POP_BLOCK   87
#define END_FINALLY 88
#define BUILD_CLASS 89

#define HAVE_ARGUMENT   90  /* Opcodes from here have an argument: */

#define STORE_NAME  90  /* Index in name list */
#define DELETE_NAME 91  /* "" */
#define UNPACK_SEQUENCE 92  /* Number of sequence items */
#define FOR_ITER    93
#define LIST_APPEND 94

#define STORE_ATTR  95  /* Index in name list */
#define DELETE_ATTR 96  /* "" */
#define STORE_GLOBAL    97  /* "" */
#define DELETE_GLOBAL   98  /* "" */
#define DUP_TOPX    99  /* number of items to duplicate */
#define LOAD_CONST  100 /* Index in const list */
#define LOAD_NAME   101 /* Index in name list */
#define BUILD_TUPLE 102 /* Number of tuple items */
#define BUILD_LIST  103 /* Number of list items */
#define BUILD_SET   104     /* Number of set items */
#define BUILD_MAP   105 /* Always zero for now */
#define LOAD_ATTR   106 /* Index in name list */
#define COMPARE_OP  107 /* Comparison operator */
#define IMPORT_NAME 108 /* Index in name list */
#define IMPORT_FROM 109 /* Index in name list */
#define JUMP_FORWARD    110 /* Number of bytes to skip */

#define JUMP_IF_FALSE_OR_POP 111 /* Target byte offset from beginning
                                    of code */
#define JUMP_IF_TRUE_OR_POP 112 /* "" */
#define JUMP_ABSOLUTE   113 /* "" */
#define POP_JUMP_IF_FALSE 114   /* "" */
#define POP_JUMP_IF_TRUE 115    /* "" */

#define LOAD_GLOBAL 116 /* Index in name list */

#define CONTINUE_LOOP   119 /* Start of loop (absolute) */
#define SETUP_LOOP  120 /* Target address (relative) */
#define SETUP_EXCEPT    121 /* "" */
#define SETUP_FINALLY   122 /* "" */

#define LOAD_FAST   124 /* Local variable number */
#define STORE_FAST  125 /* Local variable number */
#define DELETE_FAST 126 /* Local variable number */

#define RAISE_VARARGS   130 /* Number of raise arguments (1, 2 or 3) */
/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
#define CALL_FUNCTION   131 /* #args + (#kwargs<<8) */
#define MAKE_FUNCTION   132 /* #defaults */
#define BUILD_SLICE     133 /* Number of items */

#define MAKE_CLOSURE    134     /* #free vars */
#define LOAD_CLOSURE    135     /* Load free variable from closure */
#define LOAD_DEREF      136     /* Load and dereference from closure cell */ 
#define STORE_DEREF     137     /* Store into cell */ 

/* The next 3 opcodes must be contiguous and satisfy
   (CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1  */
#define CALL_FUNCTION_VAR          140  /* #args + (#kwargs<<8) */
#define CALL_FUNCTION_KW           141  /* #args + (#kwargs<<8) */
#define CALL_FUNCTION_VAR_KW       142  /* #args + (#kwargs<<8) */

#define SETUP_WITH 143

/* Support for opargs more than 16 bits long */
#define EXTENDED_ARG  145

#define SET_ADD         146
#define MAP_ADD         147


enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE, PyCmp_GT=Py_GT, PyCmp_GE=Py_GE,
         PyCmp_IN, PyCmp_NOT_IN, PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};

#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)

#ifdef __cplusplus
}
#endif
#endif /* !Py_OPCODE_H */

  • 当然,去掉后要改python文件头中记录代码长度的数据
    在这里插入图片描述
  • 找到0x73,表明该字段为字符串格式,后面的四个字节即为字节码指令长度,将其数值改为8E
  • 反编译即得到python代码
_map = [
 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
 [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1], 
 [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1], 
 [1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1], 
 [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], 
 [1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1], 
 [1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0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0, 1, 1, 1, 1, 1, 0, 1], 
 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 7, 1], 
 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

def maze():
    x = 1
    y = 1
    step = input()
    for i in range(len(step)):
        if step[i] == 'w':
            x -= 1
        else:
            if step[i] == 's':
                x += 1
            else:
                if step[i] == 'a':
                    y -= 1
                else:
                    if step[i] == 'd':
                        y += 1
                    else:
                        return False
        if _map[x][y] == 1:
            return False
        if x == 29 and y == 29:
            return True

  • 一道用坐标进行移动的题,即我们要找出走出迷宫的路径
  • 考虑深度优先搜索,是对一个连通图进行遍历的算法。它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节点,然后从另一条路开始走到底,这种尽量往深处走的概念即是深度优先的概念。
map1 = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1], [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1,
1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 0, 0, 1, 0, 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 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1]]
flag = ""
map2 =  [[0 for i in range(len(map1))] for i in range(len(map1)) ]
def DFS(x,y):
    global flag
    if x == len(map1) - 2 and y == len(map1) - 2: #判断边界
        print(flag)
    if map1[x+1][y] == 0 and map2[x+1][y] == 0:
        map2[x][y] = 1
        flag += 's'
        DFS(x+1,y)
        flag = flag[:-1]
        map2[x][y] = 0
    if map1[x-1][y] == 0 and map2[x-1][y] == 0:
        map2[x][y] = 1
        flag += 'w'
        DFS(x-1,y)
        flag = flag[:-1]
        map2[x][y] = 0
    if map1[x][y+1] == 0 and map2[x][y+1] == 0:
        map2[x][y] = 1
        flag += 'd'
        DFS(x,y+1)
        flag = flag[:-1]
        map2[x][y] = 0
    if map1[x][y-1] == 0 and map2[x][y-1] == 0:
        map2[x][y] = 1
        flag += 'a'
        DFS(x,y-1)
        flag = flag[:-1]
        map2[x][y] = 0
y=1
x=1
DFS(x,y)

结果是ssssddssaassddddwwwwddwwddddddwwddddddssddwwddddddddssssaawwaassaassaassddssaassaawwwwwwaaaaaaaassaassddddwwddssddssssaassddssssaaaaaawwddwwaawwwwaassssssssssssddddssddssddddddddwwaaaaaawwwwddssddwwwwwwwwddssddssssssssddddss

  • 没出flag,上网搜一下,发现还有最后一步,MD5加密
  • flag{801f190737434100e7d2790bd5b0732e}

你可能感兴趣的:(学习)