Skip to content

Instantly share code, notes, and snippets.

@shellcromancer
Created February 23, 2023 05:29
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 shellcromancer/c7dc77e469721de754880569f6bfd8bf to your computer and use it in GitHub Desktop.
Save shellcromancer/c7dc77e469721de754880569f6bfd8bf to your computer and use it in GitHub Desktop.
Add Go folders in the new Binary Ninja similar to the AlphaGolang IDA script
# Copy of https://github.com/SentineLabs/AlphaGolang/blob/main/3.categorize_go_folders.py
# for Binja with new folders view
folders = {}
def create_folders():
for function in bv.functions:
folderName = ""
name = function.name
if name.startswith("."):
folders.setdefault("uncategorized",[]).append(name)
continue
for i,c in enumerate(name):
if i > 2 and (c == "." or c == "_"):
folderName = name[:i]
break
if folderName == "":
folderName = "uncategorized"
if folderName == "sub":
folderName = "unnamed"
folders.setdefault(folderName,[]).append(name)
existed = []
created = []
for folderName in folders:
if bv.get_component_by_path(folderName):
existed.append(folderName)
continue
else:
try:
bv.create_component(folderName)
created.append(folderName)
except:
print("Failed to create folder: ", folderName)
continue
for folderName in existed:
if (folderName != "github"):
folders.pop(folderName)
for folder in created:
if len(folders[folder]) <= 3:
for func in folders[folder]:
folders.setdefault("uncategorized",[]).append(func)
folders.pop(folder)
bv.remove_component(folder)
def populate_folders():
for folderName in folders:
comp = bv.get_component_by_path(folderName)
if not comp:
log_info(f"Error: {folderName} folder didn't exist, recreating...")
try:
comp = bv.create_component(folderName)
except:
log_info(f"Couldn't create folder: {folderName}")
list_of_funcs = folders[folderName]
for funcName in list_of_funcs:
try:
funcs = bv.get_functions_by_name(funcName)
for f in funcs:
comp.add_function(f)
except:
log_info(f"Failed to move function: {funcName}")
def nest_standard_packages():
common_packages = ["_archive", "_bufio", "_builtin", "_bytes", "_compress", "_container", "_context", "_crypto", "_database", "_debug", "_embed", "_encoding", "_errors", "_flag", "_fmt", "_go", "_hash", "_html", "_image", "_index", "_io", "_log", "_math", "_mime", "_net", "_os", "_path", "_plugin", "_regexp", "_sort", "_strconv", "_strings", "_sync", "_syscall", "_testing", "_text", "_time", "_unicode", "_unsafe", "_internal", "_reflect", "_vendor", "_golang", "_runtime", "_type", "_setg", "_pthread", "_walk", "_gosave", "_x", "_cgo"]
folderName = "StandardGoPackages"
try:
comp = bv.create_component(folderName)
for package in common_packages:
try:
comp.rename(package, folderName+"/"+package)
except:
log_info("Failed to move standard package folder: {package}")
except:
log_info("Failed to create folder: {folderName}" )
def github_sort():
if "_github" in folders.keys():
github_repos = {}
for package in folders["_github"]:
repo = ""
for i,c in enumerate(package[11:]):
if i > 2 and (c == "." or c == "_"):
i = i+11
repo = package[11:i]
github_repos.setdefault(repo,[]).append(package)
break
for repo in github_repos:
sub_folder = "github/" + repo
try:
bv.create_component(sub_folder)
except:
log_info(f"Failed to create folder: {sub_folder}")
for funcName in github_repos[repo]:
try:
funcs = bv.get_functions_by_name(funcName)
for f in funcs:
comp.add_function(f)
except:
log_info(f"Failed to move github repo: {funcName}")
create_folders()
populate_folders()
# these are pretty broken
# github_sort()
# nest_standard_packages()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment