Skip to content

Instantly share code, notes, and snippets.

@SciresM
Created June 3, 2018 02:29
Show Gist options
  • Save SciresM/f8280134395cb585145a9c171f8a80a5 to your computer and use it in GitHub Desktop.
Save SciresM/f8280134395cb585145a9c171f8a80a5 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdio.h>
#include <switch.h>
static Handle g_port;
static uint64_t g_procID;
#define MODULE_HBL 111
void threadFunc(void* ctx)
{
Handle session;
Result rc;
rc = svcWaitSynchronizationSingle(g_port, -1);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 22));
rc = svcAcceptSession(&session, g_port);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 14));
s32 idx = 0;
rc = svcReplyAndReceive(&idx, &session, 1, 0, -1);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 15));
IpcParsedCommand ipc;
rc = ipcParse(&ipc);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 16));
if (!ipc.HasPid)
fatalSimple(MAKERESULT(MODULE_HBL, 17));
g_procID = ipc.Pid;
svcCloseHandle(session);
}
void getOwnProcessID(uint32_t fake_pid)
{
static Thread t;
Result rc;
rc = threadCreate(&t, &threadFunc, NULL, 0x1000, 0x20, 0);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 10));
rc = smUnregisterService("haxx:hax");
rc = smRegisterService(&g_port, "haxx:hax", false, 1);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 12));
rc = threadStart(&t);
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 13));
Service srv;
rc = smGetService(&srv, "haxx:hax");
if (R_FAILED(rc))
fatalSimple(MAKERESULT(MODULE_HBL, 23));
IpcCommand ipc;
ipcInitialize(&ipc);
ipcSendPid(&ipc);
struct {
int x, y;
}* raw;
raw = ipcPrepareHeader(&ipc, sizeof(*raw));
raw->x = raw->y = 0;
u32 *tls = (u32 *)armGetTls();
if (fake_pid) {
tls[3] = fake_pid;
tls[4] = 0xFFFE0000;
}
rc = serviceIpcDispatch(&srv);
threadWaitForExit(&t);
threadClose(&t);
serviceClose(&srv);
svcCloseHandle(g_port);
}
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(NULL);
getOwnProcessID(0);
printf("Testing Real PID send:\n");
printf("PID: %016lx\n", g_procID);
printf("---\n");
printf("Testing sending fake pid:\n");
getOwnProcessID(0xCAFEBABE);
printf("PID: %016lx\n", g_procID);
// Main loop
while(appletMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
}
gfxExit();
return 0;
}
@type0dev
Copy link

type0dev commented Jun 3, 2018

You are on fire

@whamer100
Copy link

whamer100 commented Jun 5, 2018

oh

shoot

thats nice

nice work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment