Created
August 9, 2022 21:51
-
-
Save alexander-hanel/0af9cbef4c86bdae728ca5aa833e5a21 to your computer and use it in GitHub Desktop.
Add Function Comments to Exportable Functions in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import idautils | |
import subprocess | |
import os | |
GOBIN = r"C:\Program Files\Go\bin" | |
def extract_name(func_name): | |
sp = func_name.split(".") | |
# if the start of a function is not upper case it is not exportable | |
# unexportable functions don't have docs | |
if sp[-1][0].islower() or sp[-1][0].isdigit(): | |
return None | |
for s in sp: | |
if s.startswith("_") or s == "": | |
return None | |
return sp | |
def main(): | |
if not os.path.exists(GOBIN): | |
print("Error: Go bin dir could not be found") | |
return | |
# save current working dir | |
cur_dir = os.getcwd() | |
# change working dir to Go bin diretory | |
os.chdir(GOBIN) | |
if idc.get_name_ea_simple("go.buildid") == idc.BADADDR: | |
print("go_buildid function is not present. Possibly not a Go binary...") | |
for func in idautils.Functions(): | |
name = idc.get_name(func) | |
ss = extract_name(name) | |
if not ss: | |
continue | |
try: | |
out = subprocess.check_output(['go', 'doc', name], shell=True) | |
print("Function Comment Added: %s 0x%x" % (name, func)) | |
idc.set_func_cmt(func, out.decode("utf-8"), 0) | |
except Exception as e: | |
continue | |
os.chdir(cur_dir) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
Example Comment