Skip to content

Commit

Permalink
Fix admin privileges with homebrew install
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurieWired committed Jan 13, 2025
1 parent e8bd4a8 commit b5877bf
Showing 1 changed file with 56 additions and 13 deletions.
69 changes: 56 additions & 13 deletions src/main/java/com/lauriewired/malimite/utils/GhidraSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ public class GhidraSetup {
private static final String JSON_JAR = "json-" + JSON_VERSION + ".jar";
private static final String DOWNLOAD_URL = "https://repo1.maven.org/maven2/org/json/json/" + JSON_VERSION + "/" + JSON_JAR;

private static boolean runAsAdmin(String[] command) {
try {
ProcessBuilder pb = new ProcessBuilder(command);
pb.inheritIO();
Process process = pb.start();
return process.waitFor() == 0;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public static void setupGhidraLibs(String ghidraPath) {
Path patchDir = Paths.get(ghidraPath, "Ghidra", "patch");
Path jsonJarPath = patchDir.resolve(JSON_JAR);

// Create patch directory if it doesn't exist
// First try normal file operations
try {
Files.createDirectories(patchDir);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"Failed to create patch directory: " + e.getMessage(),
"Setup Error",
JOptionPane.ERROR_MESSAGE);
return;
}

// Download and save the JSON library
try {
URL url = new URL(DOWNLOAD_URL);
try (InputStream in = url.openStream()) {
Files.copy(in, jsonJarPath, StandardCopyOption.REPLACE_EXISTING);
Expand All @@ -35,10 +37,51 @@ public static void setupGhidraLibs(String ghidraPath) {
"Required library has been successfully installed.",
"Setup Complete",
JOptionPane.INFORMATION_MESSAGE);
return;
} catch (IOException e) {
// If normal operation fails, try with admin privileges
int response = JOptionPane.showConfirmDialog(null,
"Failed to install library with normal permissions. Try with admin privileges?",
"Permission Error",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);

if (response == JOptionPane.YES_OPTION) {
// Download to temp file first
Path tempFile = null;
try {
tempFile = Files.createTempFile("ghidra_json_", ".jar");
URL url = new URL(DOWNLOAD_URL);
try (InputStream in = url.openStream()) {
Files.copy(in, tempFile, StandardCopyOption.REPLACE_EXISTING);
}

// Use sudo commands to create directory and copy file
String[] mkdirCommand = {"sudo", "mkdir", "-p", patchDir.toString()};
String[] copyCommand = {"sudo", "cp", tempFile.toString(), jsonJarPath.toString()};

if (runAsAdmin(mkdirCommand) && runAsAdmin(copyCommand)) {
JOptionPane.showMessageDialog(null,
"Required library has been successfully installed with admin privileges.",
"Setup Complete",
JOptionPane.INFORMATION_MESSAGE);
return;
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (tempFile != null) {
try {
Files.deleteIfExists(tempFile);
} catch (IOException ignored) {}
}
}
}

// If everything fails, show error message
JOptionPane.showMessageDialog(null,
"Failed to download library: " + e.getMessage(),
"Download Error",
"Failed to install library: " + e.getMessage(),
"Setup Error",
JOptionPane.ERROR_MESSAGE);
}
}
Expand Down

0 comments on commit b5877bf

Please sign in to comment.