Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tired of installing ghidra manually? Use this.
import requests
import apt
import logging
import sys
import re
import argparse
import os
from zipfile import ZipFile
project_site = "https://www.ghidra-sre.org/"
jdk_name = "openjdk-11-jdk" # Hardcoded for now
dest_dir = "/opt/"
exec_dir = "/usr/bin"
class installer:
def ghidraInstall(self, dirname):
try:
log.info("Moving file to {}".format(dest_dir))
os.rename("./{}".format(dirname), dest_dir + dirname)
os.chdir(dest_dir)
log.info("Extracting zip file...")
os.system("unzip {}".format(dirname))
log.info("Extraced, Creating symlink")
dirlist = os.listdir()
print(dirlist)
reobj = re.findall("ghidra_\d*[.]\d*[.]\d*_PUBLIC", ",".join(dirlist))
print(reobj)
os.symlink(dest_dir + reobj[0]+"/ghidraRun", exec_dir + "/ghidraRun")
log.info("Symlink Placed, Cleaning up")
os.remove(dirname)
log.info("Zip file deleted")
except Exception as e:
log.error(e)
def ghidraScrape(self):
try:
page = requests.get(project_site)
regex = b"ghidra_\d*[.]\d*[.]\d*_PUBLIC_\d*.zip"
log.info("Finding Newest Version")
regobj = re.findall(regex, page.content)
tgtfile = str(regobj[0].decode("utf-8"))
log.info("Newest Version - {}".format(tgtfile))
log.info("Downloading...")
ghidraFile = requests.get(project_site + tgtfile)
with open(tgtfile, "wb") as f:
f.write(ghidraFile.content)
f.close()
return tgtfile
except Exception as e:
log.error(e)
def jdkInstall(self):
cache = apt.cache.Cache()
cache.update()
cache.open()
pkg = cache[jdk_name]
if pkg.is_installed:
log.info("JDK Already Installed...passing")
pass
else:
pkg.mark_install()
try:
cache.commit()
except Exception as e:
log.error(e)
sys.exit()
def run(self):
self.jdkInstall()
vername = self.ghidraScrape()
self.ghidraInstall(vername)
log.info("Done!")
sys.exit()
if __name__ == "__main__":
# Args
parser = argparse.ArgumentParser()
parser.add_argument("-v", dest="verbose", help="Verbose Mode", action='store_true')
args = parser.parse_args()
# Verbosity not yet implemented
# LOGGING
log = logging.getLogger('')
log.setLevel(logging.INFO)
if args.verbose:
log.setLevel(logging.DEBUG)
infofmt = logging.Formatter("[%(levelname)s] %(message)s")
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(infofmt)
log.addHandler(ch)
# Execution
if os.geteuid() != 0:
log.error("Must be root to run this")
sys.exit()
x = installer()
x.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.