-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base64Decode.py
39 lines (32 loc) · 1.43 KB
/
Base64Decode.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
#Base64 decodes the currently selected string and adds the decoded string as a repeatable comment
#@author clover v.
#@category Strings
#@toolbar B.gif
import base64
from ghidra.program.model.listing import CodeUnit
def main():
# If something is highlighted, use its minimum address, otherwise use whatever the current address is.
if currentSelection:
b64_string_address = currentSelection.getMinAddress()
else:
b64_string_address = currentAddress
# A Listing is needed to get the defined data and set the comment.
listing = currentProgram.getListing()
# listing.getDefinedDataAt() returns the defined data at the current address, or None if there isn't any defined data there.
# Get its default value representation to turn it into a Python string.
try:
b64_string = listing.getDefinedDataAt(b64_string_address).getDefaultValueRepresentation()
except AttributeError:
print("There's no defined data at {}".format(b64_string_address))
return
# If it can't be decoded, it's probably not Base64-encoded text.
try:
decoded_string = base64.b64decode(b64_string)
except TypeError:
print("Couldn't Base64 decode {}.".format(b64_string))
return
# Print out the original string as well as the decoded string, and write the decoded string to a repeatable comment.
print('{} -> "{}"'.format(b64_string, decoded_string))
listing.setComment(b64_string_address, CodeUnit.REPEATABLE_COMMENT, decoded_string)
if __name__ == "__main__":
main()