-
Notifications
You must be signed in to change notification settings - Fork 7
/
Save_Bytes_From_Here.py
53 lines (39 loc) · 1.1 KB
/
Save_Bytes_From_Here.py
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
# vim: set fileencoding=utf-8 :
# «Save Bytes From Here» for Hopper 5
# Copyright (c) 2018, 2023, Daniel Roethlisberger <[email protected]>
# https://github.com/droe/hopper-scripts
#
# Save n bytes from current position to a file, optionally XOR decoded.
import hopper_api as api
import binascii
from itertools import cycle
def unhexlify(s):
if len(s) % 2 != 0:
s = '0' + s
return binascii.unhexlify(s)
def xorcrypt(buf, key):
if len(key) == 0:
return buf
k = cycle(key)
return bytes(b ^ next(k) for b in buf)
def main():
addr = api.selection().start
size = api.ask_int("Number of bytes")
if size == None:
return
blob = api.document.read(addr, size)
ans = api.ask_hex("XOR key in hex (optional)")
if ans == None:
return
if len(ans) == 0:
key = b'\x00'
else:
key = unhexlify(ans)
blob = xorcrypt(blob, key)
filename = api.ask_file("Save bytes to", None, True)
if filename == None:
return
with open(filename, 'wb') as f:
f.write(blob)
if __name__ == '__main__':
api.run(main)