angr和claripy:定义非连续约束

huangapple go评论76阅读模式
英文:

angr and claripy: defining non-contiguos constraints

问题

Sure, here's the translated code snippet you provided:

  1. 我正在使用 angr 并尝试将输入限制为可打印字符我使用了如下众所周知的方式
  2. ```python
  3. import angr
  4. import claripy
  5. base_addr = 0x800000
  6. proj = angr.Project("binary", main_opts={'base_addr': base_addr})
  7. flag_chars = [claripy.BVS('%d' % i, 8) for i in range(16)]
  8. flag = claripy.Concat( *flag_chars + [claripy.BVV(b'\n')])
  9. state = proj.factory.full_init_state(
  10. args=['binary'],
  11. add_options=angr.options.unicorn,
  12. stdin=flag,
  13. )
  14. for k in flag_chars:
  15. init_state.solver.add(k <= 0x7f)
  16. init_state.solver.add(k >= 0x20)
  17. simgr = proj.factory.simulation_manager(state)
  18. find_addr = 0x807bf5
  19. avoid_addr = 0x807c55
  20. simgr.explore(find=find_addr, avoid=avoid_addr)
  21. if (len(simgr.found) > 0):
  22. for found in simgr.found:
  23. print(found.posix.dumps(0))

至此,一切都正常。但现在我需要做一些不同的事情:我的输入应该仅限于字母和数字。这显然不会起作用,因为在这个区间内还有其他字符(斜杠、反斜杠、点等)。

我在尝试让这个工作方面感到困难。我尝试了类似以下的东西:

  1. init_state.solver.add(Or(And(k >= ord('A'), k <= ord('Z')), And(k >= ord('a'), k <= ord('z'))))

以及其他几种变化,但没有成功。对此有什么建议吗?

谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m playing with angr and I&#39;m trying to constraint my input to printable chars. I did it in the &quot;well-known&quot; way like this:
  4. ```python
  5. import angr
  6. import claripy
  7. base_addr = 0x800000
  8. proj = angr.Project(&quot;binary&quot;, main_opts={&#39;base_addr&#39;: base_addr})
  9. flag_chars = [claripy.BVS(&#39;%d&#39; % i, 8) for i in range(16)]
  10. flag = claripy.Concat( *flag_chars + [claripy.BVV(b&#39;\n&#39;)])
  11. state = proj.factory.full_init_state(
  12. args=[&#39;binary&#39;],
  13. add_options=angr.options.unicorn,
  14. stdin=flag,
  15. )
  16. for k in flag_chars:
  17. init_state.solver.add(k &lt;= 0x7f)
  18. init_state.solver.add(k &gt;= 0x20)
  19. simgr = proj.factory.simulation_manager(state)
  20. find_addr = 0x807bf5
  21. avoid_addr = 0x807c55
  22. simgr.explore(find=find_addr, avoid=avoid_addr)
  23. if (len(simgr.found) &gt; 0):
  24. for found in simgr.found:
  25. print(found.posix.dumps(0))

So far, so good, this is working. But now I need to do something different: my input should be constrained only to letters and numbers. This is not going to work obviously, because in this interval we have also other chars (slash, backslash, dot, etc).

I'm struggling in getting this working. I tried something like

  1. init_state.solver.add(Or(And(k &gt;= ord(&#39;A&#39;),k &lt;= ord(&#39;Z&#39;)),And(k &gt;= ord(&#39;a&#39;),k &lt;= ord(&#39;z&#39;))))

and several other variations, but no luck. Any suggestion on this?

Thanks!

答案1

得分: 0

I found the solution:

  1. for byte in flag.chop(8):
  2. initial_state.solver.add(And(byte >= ord('A'), byte <= ord('Z')) | \
  3. And(byte >= ord('a'), byte <= ord('z')) )

A small change to the script is required, this:

  1. flag_chars = [claripy.BVS('%d' % i, 8) for i in range(16)]
  2. flag = claripy.Concat( *flag_chars + [claripy.BVV(b'\n')])

must be replaced with:

  1. flag = claripy.BVS("flag", 16 * 8)
英文:

OK, I found the solution:

  1. for byte in flag.chop(8):
  2. initial_state.solver.add(And(byte &gt;= ord(&#39;A&#39;), byte &lt;= ord(&#39;Z&#39;)) | \
  3. And(byte &gt;= ord(&#39;a&#39;), byte &lt;= ord(&#39;z&#39;)) )

A small change to the script is required, this

  1. flag_chars = [claripy.BVS(&#39;%d&#39; % i, 8) for i in range(16)]
  2. flag = claripy.Concat( *flag_chars + [claripy.BVV(b&#39;\n&#39;)])

must be replaced with

  1. flag = claripy.BVS(&quot;flag&quot;, 16 * 8)

huangapple
  • 本文由 发表于 2023年4月20日 00:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定