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

scripts not working #6455

Open
dot166 opened this issue Apr 23, 2024 · 13 comments
Open

scripts not working #6455

dot166 opened this issue Apr 23, 2024 · 13 comments
Assignees
Labels
Feature: Scripting Status: Triage Information is being gathered

Comments

@dot166
Copy link

dot166 commented Apr 23, 2024

Describe the bug
A clear and concise description of the bug.

the script is not executing see log and additional info.

To Reproduce
Steps to reproduce the behavior:

  1. run a script using command in additional info
  2. See error

Expected behavior
it would execute

Screenshots
N/A

Attachments

INFO  REPORT: Analysis succeeded for file: file:///home/dot_______166/tm.exe (HeadlessAnalyzer)                        
ERROR Failed to find script in any script directory: /home/dot_______166/ghidra-headless-decompile/fix.java (GhidraScriptUtil)                                                                                                                  
ERROR REPORT SCRIPT ERROR: fix.java : The class could not be found. It must be the public class of the .java file: Failed to find source bundle containing script: /home/dot_______166/ghidra-headless-decompile/fix.java (HeadlessAnalyzer)
ghidra.app.script.GhidraScriptLoadException: The class could not be found. It must be the public class of the .java file: Failed to find source bundle containing script: /home/dot_______166/ghidra-headless-decompile/fix.java
at ghidra.app.script.JavaScriptProvider.getScriptInstance(JavaScriptProvider.java:105)
at ghidra.app.util.headless.HeadlessAnalyzer.runScriptsList(HeadlessAnalyzer.java:908)
at ghidra.app.util.headless.HeadlessAnalyzer.analyzeProgram(HeadlessAnalyzer.java:1070)
at ghidra.app.util.headless.HeadlessAnalyzer.processFileWithImport(HeadlessAnalyzer.java:1552)
at ghidra.app.util.headless.HeadlessAnalyzer.processWithLoader(HeadlessAnalyzer.java:1734)
at ghidra.app.util.headless.HeadlessAnalyzer.processWithImport(HeadlessAnalyzer.java:1675)
at ghidra.app.util.headless.HeadlessAnalyzer.processWithImport(HeadlessAnalyzer.java:1759)
at ghidra.app.util.headless.HeadlessAnalyzer.processLocal(HeadlessAnalyzer.java:460)
at ghidra.app.util.headless.AnalyzeHeadless.launch(AnalyzeHeadless.java:127)
at ghidra.GhidraLauncher.launch(GhidraLauncher.java:78)
at ghidra.Ghidra.main(Ghidra.java:54)
Caused by: java.lang.ClassNotFoundException: Failed to find source bundle containing script: /home/dot_______166/ghidra-headless-decompile/fix.java                                                                                                     
at ghidra.app.script.JavaScriptProvider.loadClass(JavaScriptProvider.java:146)
at ghidra.app.script.JavaScriptProvider.getScriptInstance(JavaScriptProvider.java:91)                                   ... 10 more                         

Environment (please complete the following information):

  • OS: Ubuntu 22.04.3 LTS on Windows 10 x86_64
  • Java Version: 17
  • Ghidra Version: latest
  • Ghidra Origin: official GitHub distro

Additional context

code:

//Decompile an entire program

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import ghidra.app.plugin.core.script.Ingredient;
import ghidra.app.plugin.core.script.IngredientDescription;
import ghidra.app.script.GatherParamPanel;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.Option;
import ghidra.app.util.exporter.CppExporter;

public class fix extends GhidraScript implements Ingredient {

	@Override
	public void run() throws Exception {
		IngredientDescription[] ingredients = getIngredientDescriptions();
		for (IngredientDescription ingredient : ingredients) {
			state.addParameter(ingredient.getID(), ingredient.getLabel(), ingredient.getType(),
				ingredient.getDefaultValue());
		}
		// if (!state.displayParameterGatherer("Script Options")) {
		// 	return;
		// }

		String[] args = getScriptArgs();


		System.out.println("ARGS: " + Arrays.toString(args));

		// File outputFile = (File) state.getEnvironmentVar("COutputFile");
		File outputFile = new File(args[0]);


		CppExporter cppExporter = new CppExporter();
		List<Option> options = new ArrayList<Option>();
		options.add(new Option(CppExporter.CREATE_HEADER_FILE, new Boolean(false)));
		cppExporter.setOptions(options);
		cppExporter.setExporterServiceProvider(state.getTool());
		cppExporter.export(outputFile, currentProgram, null, monitor);
	}

	@Override
	public IngredientDescription[] getIngredientDescriptions() {
		IngredientDescription[] retVal = new IngredientDescription[] {
			new IngredientDescription("COutputFile", "Output C File", GatherParamPanel.FILE, "") };
		return retVal;
	}

}

command executed:

time /home/dot_______166/ghidra/support/analyzeHeadless . tmp_ghidra_project -import tm.exe  -scriptPath /home/dot_______166/ghidra-headless-decompile/ -postscript fix.java tm.cpp 
@ryanmkurtz
Copy link
Collaborator

Are you sure you are using JDK 17? A similar looking error recently popped up when JDK 22 was being used.

@dot166
Copy link
Author

dot166 commented Apr 23, 2024

when i run java -version i get the following output:
dot_______166@JAKE-PC:~/ghidra-headless-decompile$ java -version openjdk version "17.0.10" 2024-01-16 OpenJDK Runtime Environment (build 17.0.10+7-Ubuntu-122.04.1) OpenJDK 64-Bit Server VM (build 17.0.10+7-Ubuntu-122.04.1, mixed mode, sharing)

@ryanmkurtz
Copy link
Collaborator

Would you mind just confirming what version is displayed on your Ghidra splash screen when it starts?

@dot166
Copy link
Author

dot166 commented Apr 23, 2024

11.0.3

@ryanmkurtz
Copy link
Collaborator

Hmmm, the latest version of Ghidra should not even be allowed to launch with JDK 11.

@dot166
Copy link
Author

dot166 commented Apr 23, 2024

Oh my mistake i put the ghidra version in that comment. My splash screen says
ghidra 11.0.3
Java 17.0.10

@ryanmkurtz
Copy link
Collaborator

Ok thanks, i'll think more about it.

@ryanmkurtz ryanmkurtz self-assigned this Apr 23, 2024
@ryanmkurtz ryanmkurtz added Feature: Scripting Status: Triage Information is being gathered labels Apr 23, 2024
@astrelsky
Copy link
Contributor

Ok thanks, i'll think more about it.

#6449 (comment)

I see this problem roughly 80% of the time I try to run a java script. I think the easiest way to reproduce it is to delete the osgi folder, launch the code browser, close it while it is taking forever to setup the osgi stuff, let it error and then it should happen the next time you try to run a java script.

@MarcusMWilliams
Copy link

MarcusMWilliams commented Apr 25, 2024 via email

@ProgSys
Copy link

ProgSys commented May 4, 2024

Describe the bug
I have the exactly same problem. Not even a simple hello world runs.

To Reproduce
Select any script in Script Manager like "HelloWorldScript.java" and try to run it.

Expected behavior
it would execute

Screenshots
N/A

Environment (please complete the following information):
OS: Windows 10 x86_64
Java Version: 17.0.10 or 22.0.1 (zip and set path variable)
Ghidra Version: latest
Ghidra Origin: official GitHub distro

Additional context:

> Unable to load script: HelloWorldScript.java
>   detail: The class could not be found. It must be the public class of the .java file: HelloWorldScript

@cyrozap
Copy link
Contributor

cyrozap commented May 11, 2024

I'm having the same issue--custom scripts written in Java fail to run, but scripts included with Ghidra run without any problems.

It doesn't matter what version of Java I use--the result is the same whether I use Java 17, 21, or 22.

I'm running Ghidra on Arch Linux, and I'm using the latest version of Ghidra from the Arch repositories (11.0.3).

Here's a minimal reproducer script (TestScript.java):

// Test script

import ghidra.app.script.GhidraScript;

public class TestScript extends GhidraScript {
	@Override
	public void run() {
	}
}

Placing that script in a custom script directory $USER_HOME/Documents/Ghidra/scripts results in the following console output:

> Unable to load script: TestScript.java
>   detail: The class could not be found. It must be the public class of the .java file: TestScript

The problem can be worked around by moving the script into /opt/ghidra/Ghidra/Features/Base/ghidra_scripts/. When I do that, TestScript.java executes successfully:

TestScript.java> Running...
TestScript.java> Finished!

@cyrozap
Copy link
Contributor

cyrozap commented May 12, 2024

In the process of trying to figure out what the problem was, I accidentally fixed it for myself. Here's approximately what I did--not sure what part of this fixed the issue:

  1. Have Ghidra already open with a CodeBrowser window open.
  2. $ mkdir ~/ghidra_scripts (it didn't exist before since I prefer to keep my scripts in other directories)
  3. $ mv TestScript.java ~/ghidra_scripts/
  4. "Display Script Manager" -> "Manage Script Directories" -> "Refesh state by cleaning and reactivating all enabled bundles"
  5. Close Ghidra.
  6. Open Ghidra.
  7. Open CodeBrowser window.
  8. "Display Script Manager" -> "Manage Script Directories" -> Enable $USER_HOME/ghidra_scripts (I might have refreshed again after this--I forget whether or not I did)
  9. Run TestScript.java : Results in successful script output in console.
  10. Run other custom scripts in other custom directories (not $USER_HOME/ghidra_scripts`): Results in successful script output in console.
  11. $ rm -r ~/ghidra_scripts
  12. "Display Script Manager" -> "Manage Script Directories" -> "Refesh state by cleaning and reactivating all enabled bundles"
  13. Close Ghidra.
  14. Open Ghidra.
  15. Open CodeBrowser window.
  16. Run other custom scripts in other custom directories (not $USER_HOME/ghidra_scripts`): Results in successful script output in console.
  17. "Display Script Manager" -> "Manage Script Directories" -> "Refesh state by cleaning and reactivating all enabled bundles"
  18. Run other custom scripts in other custom directories (not $USER_HOME/ghidra_scripts`): Results in successful script output in console.

So now custom scripts in custom directories works for me and I can't get it back into the broken state any more. If someone really wants me to I can try restoring my ~/.ghidra directory from yesterday's backup to see if things are broken again and maybe try to figure out the minimum number of steps required to fix the issue, but it would be kind of a pain for me to do all of that so I'd rather not unless it's deemed necessary for finding the root cause of the issue. Hopefully the information I've already posted will be enough for someone else to determine the root cause or find a simpler fix.

@astrelsky
Copy link
Contributor

In the process of trying to figure out what the problem was, I accidentally fixed it for myself. Here's approximately what I did--not sure what part of this fixed the issue:

  1. Have Ghidra already open with a CodeBrowser window open.
  2. $ mkdir ~/ghidra_scripts (it didn't exist before since I prefer to keep my scripts in other directories)
  3. $ mv TestScript.java ~/ghidra_scripts/
  4. "Display Script Manager" -> "Manage Script Directories" -> "Refesh state by cleaning and reactivating all enabled bundles"
  5. Close Ghidra.
  6. Open Ghidra.
  7. Open CodeBrowser window.
  8. "Display Script Manager" -> "Manage Script Directories" -> Enable $USER_HOME/ghidra_scripts (I might have refreshed again after this--I forget whether or not I did)
  9. Run TestScript.java : Results in successful script output in console.
  10. Run other custom scripts in other custom directories (not $USER_HOME/ghidra_scripts`): Results in successful script output in console.
  11. $ rm -r ~/ghidra_scripts
  12. "Display Script Manager" -> "Manage Script Directories" -> "Refesh state by cleaning and reactivating all enabled bundles"
  13. Close Ghidra.
  14. Open Ghidra.
  15. Open CodeBrowser window.
  16. Run other custom scripts in other custom directories (not $USER_HOME/ghidra_scripts`): Results in successful script output in console.
  17. "Display Script Manager" -> "Manage Script Directories" -> "Refesh state by cleaning and reactivating all enabled bundles"
  18. Run other custom scripts in other custom directories (not $USER_HOME/ghidra_scripts`): Results in successful script output in console.

So now custom scripts in custom directories works for me and I can't get it back into the broken state any more. If someone really wants me to I can try restoring my ~/.ghidra directory from yesterday's backup to see if things are broken again and maybe try to figure out the minimum number of steps required to fix the issue, but it would be kind of a pain for me to do all of that so I'd rather not unless it's deemed necessary for finding the root cause of the issue. Hopefully the information I've already posted will be enough for someone else to determine the root cause or find a simpler fix.

#6449 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Scripting Status: Triage Information is being gathered
Projects
None yet
Development

No branches or pull requests

6 participants