Skip to content

Commit

Permalink
changed the update function to expose the rev in the filename
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Jan 23, 2021
1 parent ca124cf commit aac82ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
22 changes: 9 additions & 13 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,18 @@ def http_get(url):
def update_gef(argv):
"""Try to update `gef` to the latest version pushed on GitHub master branch.
Return 0 on success, 1 on failure. """
gef_local = os.path.realpath(argv[0])
hash_gef_local = hashlib.sha512(open(gef_local, "rb").read()).digest()
gef_remote = "https://raw.githubusercontent.com/hugsy/gef/master/gef.py"
gef_remote_data = http_get(gef_remote)
if gef_remote_data is None:
ver = "dev" if "--dev" in argv[2:] else "master"
latest_gef_data = http_get("https://raw.githubusercontent.com/hugsy/gef/{}/scripts/gef.sh".format(ver,))
if latest_gef_data is None:
print("[-] Failed to get remote gef")
return 1

hash_gef_remote = hashlib.sha512(gef_remote_data).digest()
if hash_gef_local == hash_gef_remote:
print("[-] No update")
else:
with open(gef_local, "wb") as f:
f.write(gef_remote_data)
print("[+] Updated")
return 0
fd, fname = tempfile.mkstemp(suffix=".sh")
os.write(fd, latest_gef_data)
os.close(fd)
retcode = subprocess.run(["bash", fname, ver], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
os.unlink(fname)
return retcode


try:
Expand Down
22 changes: 18 additions & 4 deletions scripts/gef.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#!/bin/bash

wget -q -O "$HOME/.gdbinit-gef.py" https://github.com/hugsy/gef/raw/master/gef.py
test -f "$HOME/.gdbinit" && mv "$HOME/.gdbinit" "$HOME/.gdbinit.old"
echo "source $HOME/.gdbinit-gef.py" > "$HOME/.gdbinit"
set -e

exit 0
branch="master"
test "$1" == "dev" && branch="dev"

# Backup gdbinit if any
test -f "${HOME}/.gdbinit" && mv "${HOME}/.gdbinit" "${HOME}/.gdbinit.old"

# Get the hash of the commit
ref=$(curl --silent https://api.github.com/repos/hugsy/gef/git/ref/heads/${branch} | grep '"sha"' | tr -s ' ' | cut -d ' ' -f 3 | tr -d "," | tr -d '"')

# Download the file
curl --silent --output "${HOME}/.gef-${ref}.py" "https://github.com/hugsy/gef/raw/${branch}/gef.py"

# Create the new gdbinit
echo "source ~/.gef-${ref}.py" > ~/.gdbinit

exit 0
10 changes: 0 additions & 10 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,6 @@ def test_func_stack(self):
self.assertRegex(res, r"\+0x0*20: *0x0000000000000000\n")
return

class TestGefMisc(GefUnitTestGeneric):
"""Tests external functionality."""

def test_update(self):
tempdir = tempfile.mkdtemp()
update_gef = os.path.join(tempdir, "gef.py")
subprocess.call(["cp", "/tmp/gef.py", update_gef])
status = subprocess.call(["python3", update_gef, "--update"])
self.assertEqual(status, 0)


def run_tests(name=None):

Expand Down

0 comments on commit aac82ac

Please sign in to comment.