Skip to content

Instantly share code, notes, and snippets.

@alexander-hanel
Created August 9, 2022 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-hanel/0af9cbef4c86bdae728ca5aa833e5a21 to your computer and use it in GitHub Desktop.
Save alexander-hanel/0af9cbef4c86bdae728ca5aa833e5a21 to your computer and use it in GitHub Desktop.
Add Function Comments to Exportable Functions in Go
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()
@alexander-hanel
Copy link
Author

Output

Function Comment Added: runtime.Caller 0x10082e0
Function Comment Added: runtime.SetFinalizer 0x1015020
Function Comment Added: runtime.Gosched 0x1031a80
Function Comment Added: runtime.FuncForPC 0x1049700
Function Comment Added: strconv.FormatInt 0x10625a0
Function Comment Added: strconv.CanBackquote 0x10639a0
Function Comment Added: strconv.IsPrint 0x1063aa0
Function Comment Added: reflect.Kind.String 0x106c3a0
Function Comment Added: reflect.ChanDir.String 0x106c980
Function Comment Added: reflect.Value.Bool 0x106da00
Function Comment Added: reflect.Value.Bytes 0x106dae0
Function Comment Added: reflect.Value.Elem 0x106ee00
Function Comment Added: reflect.Value.Field 0x106efe0
Function Comment Added: reflect.Value.Index 0x106f120
Function Comment Added: reflect.Value.Kind 0x106f440
Function Comment Added: reflect.Value.Len 0x106f460
Function Comment Added: reflect.Value.MapRange 0x106f860
Function Comment Added: reflect.Value.NumMethod 0x106fa60
Function Comment Added: reflect.Value.NumField 0x106fb20
Function Comment Added: reflect.Value.Pointer 0x106fc00
Function Comment Added: reflect.Value.Slice 0x106fdc0
Function Comment Added: reflect.Value.String 0x1070060
Function Comment Added: reflect.Value.Type 0x1070140
Function Comment Added: sort.Stable 0x1070da0
Function Comment Added: syscall.Getenv 0x1073280
Function Comment Added: syscall.SetNonblock 0x1073540
Function Comment Added: syscall.Getwd 0x10735e0
Function Comment Added: syscall.Errno.Error 0x1073760
Function Comment Added: syscall.Close 0x10738e0
Function Comment Added: syscall.Open 0x1073a60
Function Comment Added: syscall.Seek 0x1073d00
Function Comment Added: time.Time.String 0x1075cc0
Function Comment Added: time.Time.GoString 0x1075ea0
Function Comment Added: time.Time.Format 0x1076800
Function Comment Added: time.Time.AppendFormat 0x1076960
Function Comment Added: time.LoadLocationFromTZData 0x107bbe0
Function Comment Added: os.Getenv 0x1080700
Function Comment Added: os.NewFile 0x1080a80
Function Comment Added: os.Getwd 0x1080fc0
Function Comment Added: fmt.Fprintln 0x10844e0

Example Comment

Screen Shot 2022-08-09 at 3 51 55 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment