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

Hooks for fopen and canOpenURL for Cydia. #390

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 128 additions & 2 deletions agent/src/ios/jailbreak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { jobs } from "../lib/jobs";
// an OS upgrade, some filesystem artifacts may still exist, causing some
// of the typical checks to incorrectly detect the jailbreak status!

// Hook NSFileManager calls and check if it is to a common path.
// TODO: Hook fopen too.
// Hook NSFileManager and fopen calls and check if it is to a common path.
// Hook canOpenURL for Cydia deep link.

const jailbreakPaths = [
"/Applications/Cydia.app",
"/Applications/FakeCarrier.app",
Expand Down Expand Up @@ -109,6 +110,127 @@ export namespace iosjailbreak {
);
};


// toggles replies to fopen: for the paths in jailbreakPaths
const fopen = (success: boolean, ident: string): InvocationListener => {

return Interceptor.attach(
Module.findExportByName(null, "fopen"), {
onEnter(args) {

this.is_common_path = false;

// Extract the path
this.path = Memory.readCString(ptr(args[0]));

// check if the looked up path is in the list of common_paths
if (jailbreakPaths.indexOf(this.path) >= 0) {

// Mark this path as one that should have its response
// modified if needed.
this.is_common_path = true;
}
},
onLeave(retval) {

// stop if we dont care about the path
if (!this.is_common_path) {
return;
}

// depending on the desired state, we flip retval
switch (success) {
case (true):
// ignore successful lookups
if (!retval.isNull()) {
return;
}
send(
c.blackBright(`[${ident}] `) + `fopen: check for ` +
c.green(this.path) + ` failed with: ` +
c.red(retval.toString()) + `, marking it as successful.`,
);

retval.replace(new NativePointer(0x01));
break;

case (false):
// ignore failed lookups
if (retval.isNull()) {
return;
}
send(
c.blackBright(`[${ident}] `) + `fopen: check for ` +
c.green(this.path) + ` was successful with: ` +
c.red(retval.toString()) + `, marking it as failed.`,
);

retval.replace(new NativePointer(0x00));
break;
}
},
},
);
};

// toggles replies to canOpenURL for Cydia
const canOpenURL = (success: boolean, ident: string): InvocationListener => {

return Interceptor.attach(
ObjC.classes.UIApplication["- canOpenURL:"].implementation, {
onEnter(args) {

this.is_flagged = false;

// Extract the path
this.path = ObjC.Object(args[2]).toString();

if (this.path.startsWith('cydia') || path.startsWith('Cydia')) {
this.is_flagged = true;
}
},
onLeave(retval) {

if (!this.is_flagged) {
return;
}

// depending on the desired state, we flip retval
switch (success) {
case (true):
// ignore successful lookups
if (!retval.isNull()) {
return;
}
send(
c.blackBright(`[${ident}] `) + `canOpenURL: check for ` +
c.green(this.path) + ` failed with: ` +
c.red(retval.toString()) + `, marking it as successful.`,
);

retval.replace(new NativePointer(0x01));
break;

case (false):
// ignore failed
if (retval.isNull()) {
return;
}
send(
c.blackBright(`[${ident}] `) + `canOpenURL: check for ` +
c.green(this.path) + ` was successful with: ` +
c.red(retval.toString()) + `, marking it as failed.`,
);

retval.replace(new NativePointer(0x00));
break;
}
},
},
);
};


const libSystemBFork = (success: boolean, ident: string): InvocationListener => {
// Hook fork() in libSystem.B.dylib and return 0
// TODO: Hook vfork
Expand Down Expand Up @@ -165,6 +287,8 @@ export namespace iosjailbreak {

job.invocations.push(fileExistsAtPath(false, job.identifier));
job.invocations.push(libSystemBFork(false, job.identifier));
job.invocations.push(fopen(false, job.identifier));
job.invocations.push(canOpenURL(false, job.identifier));

jobs.add(job);
};
Expand All @@ -178,6 +302,8 @@ export namespace iosjailbreak {

job.invocations.push(fileExistsAtPath(true, job.identifier));
job.invocations.push(libSystemBFork(true, job.identifier));
job.invocations.push(fopen(true, job.identifier));
job.invocations.push(canOpenURL(true, job.identifier));

jobs.add(job);
};
Expand Down