Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hexdump to print non-ascii characters #173

Closed
Ekultek opened this issue Feb 14, 2022 · 5 comments · Fixed by #177
Closed

Add hexdump to print non-ascii characters #173

Ekultek opened this issue Feb 14, 2022 · 5 comments · Fixed by #177

Comments

@Ekultek
Copy link

Ekultek commented Feb 14, 2022

(venv) me@DESKTOP-123456:~$ ROPgadget --binary '/bin/ls' --string '.+\w+(.)?\\.+'
Strings information
============================================================
0x000000000001c7bf : ��G��BI�\��
0x000000000001c7f7 : ��G��BI�\��
0x000000000001ca32 : ��A��BN�\
Traceback (most recent call last):
  File "/home/me/erop/venv/bin/ROPgadget", line 12, in <module>
    ropgadget.main()
  File "/home/me/erop/venv/lib/python3.8/site-packages/ropgadget/__init__.py", line 30, in main
    sys.exit(0 if Core(args.getArgs()).analyze() else 1)
  File "/home/me/erop/venv/lib/python3.8/site-packages/ropgadget/core.py", line 246, in analyze
    return self.__lookingForAString(self.__options.string)
  File "/home/me/erop/venv/lib/python3.8/site-packages/ropgadget/core.py", line 176, in __lookingForAString
    print("0x{{0:0{}x}} : {{1}}".format(8 if arch == CS_MODE_32 else 16).format(vaddr, match.decode()))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 12: invalid start byte
(venv) me@DESKTOP-123456:~$

Create a hexdump for it so that it can decode the string properly, for example (in core.py):

class Core(cmd.Cmd):
    ....
    
    def __hexdump(self, s):
        acceptable = string.printable[0:-6] # everything except \x00 and shit like that
        results = []
        for c in list(s):
            if c in acceptable:
                results.append(c)
            else:
                results.append(".")
        return "".join(results)
        
    ...

        def __lookingForAString(self, string):
        ....
                try:
                    match = section["opcodes"][ref:ref + len(string)]
                    print("0x{{0:0{}x}} : {{1}}".format(8 if arch == CS_MODE_32 else 16).format(vaddr, match.decode()))
                except UnicodeDecodeError:
                    match = self.__hexdump(section["opcodes"][ref:ref + len(string)].decode())
                    print("0x{{0:0{}x}} : {{1}}".format(8 if arch == CS_MODE_32 else 16).format(vaddr, match))
        return True

This way if anything comes up thats not printable you can still see it without crashing the program

@SweetVishnya
Copy link
Collaborator

Can you make a PR with this fix?

@Ekultek
Copy link
Author

Ekultek commented Feb 14, 2022

@SweetVishnya yes I can, I don't have time right now though that's why I put it in an issue.

@Ekultek Ekultek changed the title Add hexdump to print non-ascci characters Add hexdump to print non-ascii characters Feb 14, 2022
@Ekultek
Copy link
Author

Ekultek commented Mar 2, 2022

Hey, I'm most likely not going to have time in the near future, so the code above should work, if you want to test it.

@SweetVishnya
Copy link
Collaborator

Ok, I'll try to find time this week to apply this patch.

@SweetVishnya
Copy link
Collaborator

I merged a fix to master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants