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

Uncontrolled Search Path Element when executing CMD. #107

Closed
dalvarezperez opened this issue Mar 7, 2019 · 25 comments
Closed

Uncontrolled Search Path Element when executing CMD. #107

dalvarezperez opened this issue Mar 7, 2019 · 25 comments
Assignees
Labels
Feature: Python Status: Internal This is being tracked internally by the Ghidra team Type: Security
Milestone

Comments

@dalvarezperez
Copy link

Describe the bug
When executing Ghidra from a given path the Java process working directory is set to this path. Then, when launching Python interpreter located in "Ghidra Codebrowser" -> "Window" -> "Python"
Ghidra will try to execute an arbitrary file "cmd.exe" located at the attacker choosen working directory.

To Reproduce
Steps to reproduce the behavior:

  1. Copy an arbitrary Portable Executable file named "cmd.exe" into a folder, for instance: c:\test.
  2. Open a command line and go to the choosen folder. Following the same example: cd c:\test\
  3. Execute Ghidra from the command line: [route_to_ghidra]\ghidraRun.bat
  4. Create a new Ghidra proyect.
  5. Add a Portable Executable into the proyect.
  6. Open the added file in "GhidraCodebrowser".
  7. Go to "Window", and then to "Python".
  8. Wait until your arbitrary file "cmd.exe" is executed.

Expected behavior
Ghidra would resolve the right "cmd.exe" located into the Windows system directory.

Screenshots
bug_ghidra

Environment (please complete the following information):

  • OS: Microsoft Windows 7 x64
  • Version 6.1.7601
@dalvarezperez dalvarezperez added the Type: Bug Something isn't working label Mar 7, 2019
@ryanmkurtz ryanmkurtz self-assigned this Mar 8, 2019
@ryanmkurtz
Copy link
Collaborator

I could not reproduce this. The first line of output in your Process Monitor is referencing bin\jython.exe. That is not part of the Ghidra release. Did you add that there? Where you doing anything with Eclipse projects in that jython directory?

@dalvarezperez
Copy link
Author

I could not reproduce this. The first line of output in your Process Monitor is referencing bin\jython.exe. That is not part of the Ghidra release. Did you add that there? Where you doing anything with Eclipse projects in that jython directory?

The CreateFile operation result of "bin\jython.exe" is: PATH NOT FOUND because the file, as you known, is not included with Ghidra, in fact, file does not exist but this is not relevant in this case.
Important things here are to execute Ghidra from a choosen working directory where arbitrary "cmd.exe" do exist. You can verify that with Process Explorer. Then, when you launches the python interpreter this file will be unexpectedly executed. Maybe this screeshot helps:
screenshot

@ryanmkurtz
Copy link
Collaborator

Thanks for the additional info. Does it execute your cmd.exe binary every time you reset the python interpreter (ctrl+d or exit() from the interpreter)?

@dalvarezperez
Copy link
Author

Thanks for the additional info. Does it execute your cmd.exe binary every time you reset the python interpreter (ctrl+d or exit() from the interpreter)?

Yes, It happens in both cases: CTRL+D and exit()

@ryanmkurtz
Copy link
Collaborator

Ok, I'll have to look into why Jython is doing this...it's definitely not necessary for our built-in python interpreter to function. Thanks for finding/reporting this and all of the detailed information!

@ryanmkurtz
Copy link
Collaborator

Would you mind sharing the command line arguments that get passed to cmd.exe? Looking at the jython source, it seems to be launching cmd.exe /c ver to get the Windows version (and uname for linux/mac). I want to confirm that is what is happening for you.

@dalvarezperez
Copy link
Author

dalvarezperez commented Mar 8, 2019

Would you mind sharing the command line arguments that get passed to cmd.exe? Looking at the jython source, it seems to be launching cmd.exe /c ver to get the Windows version (and uname for linux/mac). I want to confirm that is what is happening for you.

Yes, it is launching cmd.exe /c ver

@dalvarezperez
Copy link
Author

I got infected with Gandcrab v5.2 while analyzing it named as: "cmd.exe"
But this behaviour will happen with any other threat.
screenshot

@dalvarezperez
Copy link
Author

This security breach was silently fixed. It is not in release notes: https://ghidra-sre.org/releaseNotes.html#9_0_1

@ryanmkurtz
Copy link
Collaborator

Unfortunately this issue was not able to be fixed for 9.0.1. Are you sure you are testing it in exactly the same manner as when you reported it?

@dalvarezperez
Copy link
Author

You are right. It is not fixed yet.

@ryanmkurtz
Copy link
Collaborator

cmd.exe is also launched every time you call getwindowsversion() from the interpreter.

@dalvarezperez
Copy link
Author

It is possible to set the JAVA working directory avoiding most of these situations instead of fixing one by one. I suggest to add the following code into "support\launch.bat" :
cd /d %~dp0/../
But well, an analogous command will be necessary for Linux/Mac into the file "support\launch.sh"

@ryanmkurtz ryanmkurtz added Feature: Python Type: Security and removed Type: Bug Something isn't working labels Sep 20, 2019
@0x6d696368
Copy link

I would say this is an issue in Jython. More specifically this line: https://github.com/jythontools/jython/blob/master/src/org/python/core/PySystemState.java#L1786

I would open an issue there and have it fixed upstream.


However, Ghidra has the same issue when opening manuals, see:

I would use %COMSPEC% which is the Windows env var for the comandline interpreter binary.

Linux should be OK as there binaries that are not in the PATH env var can only be executed by prefixing them with ./. So uname will not run a local uname. To run a local uname bin in the CWD you'd have to run ./uname... as Unix has identified this problem of local binaries shadowing global ones early on and recognized the threat this poses.

@ryanmkurtz ryanmkurtz removed their assignment Jan 28, 2020
@NicoleG25
Copy link

@ryanmkurtz was this issue ever addressed ?
If so could you point me to the fixing commit ?
Please note that this issue was assigned CVE-2019-17664.
Thanks in advance !

@ryanmkurtz
Copy link
Collaborator

No, it is not fixed. It's more of an issue with Jython...it should be fixed in that library. As far as I know, Jython is unaware of the issue.

@NicoleG25
Copy link

@jeff5 Could you take a look/have someone take a look at this ?
Thanks in advance :)

@ryanmkurtz was this issue ever addressed ?
If so could you point me to the fixing commit ?
Please note that this issue was assigned CVE-2019-17664.
Thanks in advance !

@jeff5
Copy link

jeff5 commented Apr 22, 2020

Happy to take a look You just missed v2.7.2, sorry. I raised https://bugs.jython.org/issue2882 .

Almost certainly, the right answer is to use COMSPEC as @0x6d696368 suggests.

The standard library subprocess.py in CPython falls back to cmd.exe if COMSPEC is not defined, but Jython's subprocess.py on;y uses a value ultimately defined in enum OS, cmd.exe . Given the experience here, the fallback may be undesirable.

@NicoleG25
Copy link

Thank you Jeff !

@dev747368 dev747368 self-assigned this Aug 5, 2020
@gsuberland
Copy link

Was this resolved? I don't see any responses to the Jython ticket.

@ryanmkurtz
Copy link
Collaborator

No. I am hoping it gets put into Jython 2.7.3. Ghidra will upgrade its jar when 2.7.3 is released.

@xiaoyinl
Copy link
Contributor

I opened a PR on Jython repo, which will hopefully fix this bug.

@ryanmkurtz ryanmkurtz added the Status: Internal This is being tracked internally by the Ghidra team label Jul 14, 2022
@ryanmkurtz ryanmkurtz assigned ryanmkurtz and unassigned dev747368 Jul 14, 2022
@ryanmkurtz
Copy link
Collaborator

No. I am hoping it gets put into Jython 2.7.3. Ghidra will upgrade its jar when 2.7.3 is released.

@jeff5 Do you have an idea of when 2.7.3-final will be coming out? I'm hoping to include it in the upcoming Ghidra 10.2 release.

@jeff5
Copy link

jeff5 commented Aug 29, 2022

I published the rc1 today (just waiting for Sonatype to index it properly to announce).

So 2 weeks, all being well. I could squeeze that a little if you're committed to a date.

@ryanmkurtz
Copy link
Collaborator

@jeff5 2 weeks sounds good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Python Status: Internal This is being tracked internally by the Ghidra team Type: Security
Projects
None yet
Development

No branches or pull requests

8 participants