我怎样才能反转这个递归函数?

逆向工程 Python 快手
2021-07-06 17:53:45

这是我写的一个简单的crackme的python代码,我已经颠倒了,但我无法理解这里的递归函数是做什么的。

def the_process(stored, inp, size):
    idx = 0
    全局阈值
    idx = stored.index(inp[0])
    如果 idx:
        the_process(存储[:idx],inp[1:],idx)
    如果大小 - 1 != idx:
        the_process(存储[1+idx:1+idx + 大小 - idx -1],inp[idx+1:], 大小 - idx - 1)
    thstr.append(inp[0])

如果 __name__ == "__main__":
    thstr = []
    存储=“MGNCHXWIZDJAOKPELYSFUTV”
    input1 = "TFSLPOJZCGMNHWXIDAKEYUV"
    the_process(存储,列表(输入1),23)
    打印“输出:”+“”.join(thstr)

input1是我可以为该函数找出的示例有效输入。要获得的crackme验证我需要thstrMNGHCWZIJDXOPKLESUVTFYA到底。我怎样才能得到输入来实现这一点?

2个回答

我试了一下。首先,我稍微简化了您的代码。

def the_process(stored, inp, size):
    global thstr
    idx = 0
    idx = stored.index(inp[0])
    if idx:
        the_process(stored[:idx], inp[1:], idx)
    if size - 1 != idx:
        the_process(stored[idx+1:],inp[idx+1:], size - idx - 1)
    thstr.append(inp[0])

现在它看起来像是stored在二进制文件中硬编码的命名我认为MGNCHXWIZDJAOKPELYSFUTV原样,目标输出为MNGHCWZIJDXOPKLESUVTFYA.

通过该函数,它看起来像是获取 的第一个字符inputstored在该字符的位置拆分,然后在生成的两个部分上递归调用自身。我做了一棵树来跟随它。原始输入

该图是这样的,从输入中取出一个字符TFSLPOJZCGMNHWXIDAKEYUV并适当地定位位于右/左的节点/字符(V 在图中的 T 的右侧,因为它在 中的 T 的右侧stored)。在这个图中,输出是图的后序遍历,输入是前序遍历。

可以从 构造类似的图target MNGHCWZIJDXOPKLESUVTFYA只有相反,才能给出后序并构造图。 目标输入

现在 pre order 遍历这个图,你就会有你的 input AXCGMNHDIWZJYEKOPLFSTUV

等效的hackish函数是this。随意让它变得更好。

def rev_process(stored, inp, size):
    if not size:
        return
    global thstr
    mp = {stored[i] : i for i in xrange(len(stored))}
    idx = mp[inp[-1]]
    thstr.append(inp[-1])
    if size == 1:
        return
    try:
        less_part = max(loc for loc, val in enumerate(inp) if mp[val] < idx) + 1
    except ValueError:
        less_part = 0
    rev_process(stored[:idx], inp[:less_part], less_part, fs+1)
    if less_part!= size-1:
        rev_process(stored[idx+1:], inp[less_part:-1], size-less_part-1, fs+1)

我还添加了反函数代码和资源在这里

你说你写的,你能理解你写的??

你有没有试过调试它或单步执行代码

C:\>python -m pdb foo.py
> c:\foo.py(1)<module>()
-> def the_process(stored, inp, size):
(Pdb) s
> c:\foo.py(11)<module>()
-> if __name__ == "__main__":
(Pdb) s
> c:\foo.py(12)<module>()
-> thstr = []
(Pdb) s
> c:\foo.py(13)<module>()
-> stored = "MGNCHXWIZDJAOKPELYSFUTV"
(Pdb) s
> c:\foo.py(14)<module>()
-> input1 = "TFSLPOJZCGMNHWXIDAKEYUV"
(Pdb) s
> c:\foo.py(15)<module>()
-> the_process(stored, list(input1), 23)
(Pdb) p strored
*** NameError: NameError("name 'strored' is not defined",)
(Pdb) p stored
'MGNCHXWIZDJAOKPELYSFUTV'
(Pdb) p list(input1)
['T', 'F', 'S', 'L', 'P', 'O', 'J', 'Z', 'C', 'G', 'M', 'N', 'H', 'W', 'X', 'I', 'D', 'A', 'K', 'E', 'Y', 'U', 'V']
(Pdb) p size
*** NameError: NameError("name 'size' is not defined",)
(Pdb) s
--Call--
> c:\foo.py(1)the_process()
-> def the_process(stored, inp, size):
(Pdb) p size
23
(Pdb) p inp
['T', 'F', 'S', 'L', 'P', 'O', 'J', 'Z', 'C', 'G', 'M', 'N', 'H', 'W', 'X', 'I', 'D', 'A', 'K', 'E', 'Y', 'U', 'V']
(Pdb) p stored
'MGNCHXWIZDJAOKPELYSFUTV'
(Pdb) p thstr
[]
(Pdb) s
> c:\foo.py(2)the_process()
-> idx = 0
(Pdb) s
> c:\foo.py(4)the_process()
-> idx = stored.index(inp[0])
(Pdb) s
> c:\foo.py(5)the_process()
-> if idx:
(Pdb) p idx
21
(Pdb) p inp
['T', 'F', 'S', 'L', 'P', 'O', 'J', 'Z', 'C', 'G', 'M', 'N', 'H', 'W', 'X', 'I', 'D', 'A', 'K', 'E', 'Y', 'U', 'V']
(Pdb) p inp[0]
'T'
(Pdb) ?

Documented commands (type help <topic>):
========================================
EOF    bt         cont      enable  jump  pp       run      unt
a      c          continue  exit    l     q        s        until
alias  cl         d         h       list  quit     step     up
args   clear      debug     help    n     r        tbreak   w
b      commands   disable   ignore  next  restart  u        whatis
break  condition  down      j       p     return   unalias  where

Miscellaneous help topics:
==========================
exec  pdb

Undocumented commands:
======================
retval  rv

(Pdb) rv
*** Not yet returned!
(Pdb) retval
*** Not yet returned!
(Pdb) bt
  c:\python27\lib\bdb.py(400)run()
-> exec cmd in globals, locals
  <string>(1)<module>()
  c:\foo.py(15)<module>()
-> the_process(stored, list(input1), 23)
> c:\foo.py(5)the_process()
-> if idx:
(Pdb) pp
*** SyntaxError: SyntaxError('unexpected EOF while parsing', ('<string>', 0, 0, ''))
(Pdb) a
stored = MGNCHXWIZDJAOKPELYSFUTV
inp = ['T', 'F', 'S', 'L', 'P', 'O', 'J', 'Z', 'C', 'G', 'M', 'N', 'H', 'W', 'X', 'I', 'D', 'A', 'K', 'E', 'Y', 'U', 'V'
]
size = 23
(Pdb) l
  1     def the_process(stored, inp, size):
  2         idx = 0
  3         global thstr
  4         idx = stored.index(inp[0])
  5  ->     if idx:
  6             the_process(stored[:idx], inp[1:], idx)
  7         if size - 1 != idx:
  8             the_process(stored[1+idx:1+idx + size - idx -1],inp[idx+1:], size - idx - 1)
  9         thstr.append(inp[0])
 10
 11     if __name__ == "__main__":
(Pdb)