hugsy / gef-extras

Extra goodies for GEF to (try to) make GDB suck even less
https://hugsy.github.io/gef-extras
MIT License
148 stars 50 forks source link

persistent libc.txt.gz for glibc_function_args #106

Closed aliencaocao closed 2 months ago

aliencaocao commented 3 months ago

Is your feature request related to a problem? Please describe. Im always frustrated when I have to download https://www.gnu.org/software/libc/manual/text/libc.txt.gz everytime I work on a new project/switch to a different dir. I hope there can be a way to read the extracted json from a fixed path in env var.

Describe the solution you'd like Use the path defined in context.libc_args_path in gef config.

file libc.txt.gz cannot be found. download it from https://www.gnu.org/software/libc/manual/text/libc.txt.gz
Traceback (most recent call last):
  File "/root/.config/gef-extras/scripts/libc_function_args/tables/generate_glibc_args_json.py", line 56, in <module>
    fh = gzip.open(file_name, "r")
  File "/usr/lib/python3.10/gzip.py", line 58, in open
    binary_file = GzipFile(filename, gz_mode, compresslevel)
  File "/usr/lib/python3.10/gzip.py", line 174, in __init__
    fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'libc.txt.gz'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/.config/gef-extras/scripts/libc_function_args/tables/generate_glibc_args_json.py", line 63, in <module>
    sys.exit(-1)
  File "/root/.gef-2024.01.py", line 446, in FakeExit
    raise RuntimeWarning
RuntimeWarning
therealdreg commented 2 months ago

the same bug: #107

@hugsy Is aware of the (bug) cause and is working on it

aliencaocao commented 2 months ago

Oh so this is supposed to be persistent but it is a bug that it is not?

therealdreg commented 2 months ago

The current code is not behaving as it should, so it is a bug.

image

As you can see, the libc and JSON files are already included, but it fails to find the path....

therealdreg commented 2 months ago

@aliencaocao can you test a simple crappy fix?

1

cd ~/.config/gef-extras/scripts/libc_function_args/tables
wget https://www.gnu.org/software/libc/manual/text/libc.txt.gz

2

Edit:

~/.config/gef-extras/scripts/libc_function_args/tables/generate_glibc_args_json.py

Replace:

file_name = "libc.txt.gz"

with this:

file_name = pathlib.Path(inspect.getfile(inspect.currentframe())).parent.resolve() / "libc.txt.gz"

Replace:

with open(outfile_name, "w") as outfile:

with this:

with open(pathlib.Path(inspect.getfile(inspect.currentframe())).parent.resolve() / outfile_name, "w") as outfile:

3

Edit:

~/.config/gef-extras/scripts/libc_function_args/__init__.py

Replace:

GLIBC_FUNCTION_ARGS_CURRENT_FILE = ...
GLIBC_FUNCTION_ARGS_CURRENT_DIRECTORY = ....

with this:

GLIBC_FUNCTION_ARGS_CURRENT_FILE = pathlib.Path(inspect.getfile(inspect.currentframe())).parent.resolve()
GLIBC_FUNCTION_ARGS_CURRENT_DIRECTORY = pathlib.Path(inspect.getfile(inspect.currentframe())).parent.resolve()

This works now as expected?

aliencaocao commented 2 months ago

I did the mods, launched gef once and saw the json written messages, then deleted the txt.gz, still same error come out. I should be expecting it to just read the existing json instead of trying to extract a new one right? The patches given does not seem to fix this for me as it still point to the txt.gz and does not check for any existing json.

aliencaocao commented 2 months ago

If I switch to the different dir without deleting the txt.gz, it works fine but it seem to be extracting it every time i run gef which is not very necessary. Also, when I run gef in tables, I get file x86_64.json exists, overwrite? [y/N] for both json.

therealdreg commented 2 months ago

Let's tackle this step by step.

First, I want to address the path issue, and then we'll move on to the other matters.

If we keep the files in the tables/ directory (json + tgz), everything works as you would expect, right?

(After fixing this, I'll look into ensuring that the JSON files aren't generated every time, etc.)

DimSumCodes commented 2 months ago

I was having a similar issue with the libc.txt.gz not being read from tables. I had no idea the cause, but using step 1 and 2 it fixed the issue and now properly recognizes the lic.txt.gz file in the tables directory. Also to note it does generate the x86_32 and x86_64 files in my directory every time I run gdb but it's not a big deal to me I just overwrite them every time and everything runs smooth. I did the other steps to see if it would resolve the overwrite json issue but unfortunately the json is another issue anyway.

aliencaocao commented 2 months ago

Yes it does work if i keep the txt.gz, but that was not the reason I opened the issue as I thought the json itself should be cached and reused. Seems like this is a feature request instead of a bug then

therealdreg commented 2 months ago

I have to download https://www.gnu.org/software/libc/manual/text/libc.txt.gz everytime I work on a new project/switch to a different dir

This should not happen, and this is my fix :D. So yes, this part is a bug!

therealdreg commented 2 months ago

I want to make sure everything works well.

Can you test the latest dev versions of gef-extras and gef and let me know if everything now works as you expect, please? @aliencaocao @DimSumCodes

Related to: #109 #111 https://github.com/hugsy/gef/pull/1093

Can I close this issue?

DimSumCodes commented 2 months ago

Just checked it, works perfectly doesn't generate a new file in my active directory and doesn't have any issues referencing the files.

aliencaocao commented 2 months ago

Same for me.

hugsy commented 2 months ago

Great work on fixing this @therealdreg 🎉