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

Bugfixes and minor http server feature. #698

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 5 additions & 2 deletions agent/src/android/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from "fs";
import * as fs from "frida-fs";
import { Buffer } from "buffer";
import { hexStringToBytes } from "../lib/helpers.js";
import { IAndroidFilesystem } from "./lib/interfaces.js";
import {
Expand Down Expand Up @@ -78,7 +79,9 @@ export const pwd = (): Promise<string> => {
};

// heavy lifting is done in frida-fs here.
export const readFile = (path: string): Buffer => {
export const readFile = (path: string): string | Buffer => {
if (fs.statSync(path).size == 0)
return Buffer.alloc(0);
return fs.readFileSync(path);
};

Expand Down
59 changes: 45 additions & 14 deletions agent/src/generic/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from "fs";
import * as fs from "frida-fs";
import * as httpLib from "http";
import * as url from "url";
import { colors as c } from "../lib/color.js";
Expand All @@ -11,7 +11,7 @@
c.log(`[http server] ${m}`);
};

const dirListingHTML = (p: string): string => {
const dirListingHTML = (pwd: string, path: string): string => {
let h = `
<html>
<body>
Expand All @@ -22,8 +22,15 @@
`;

h = h.replace(`{file_listing}`, () => {
return fs.readdirSync(p).map((f) => {
return `<a href="${f}">${f}</a>`;
return fs.list(pwd + path).map((f) => {
// Add a slash at the end if it is a directory.
var fname = f.name + (f.type == 4 ? '/' : '');

if (path !== '/') {
return `<a href="${path + fname}">${fname}</a>`;
} else {
return `<a href="${fname}">${fname}</a>`;
}
}).join("<br>");
});

Expand All @@ -49,16 +56,40 @@
log(`${c.redBright('Missing URL or request method.')}`);
return;
}

const parsedUrl = new URL(req.url);

if (parsedUrl.pathname === "/") {
res.end(dirListingHTML(pwd));
return;
}
try {
const parsedUrl = url.parse(req.url);
const fileLocation = pwd + decodeURIComponent(parsedUrl.path);

if (fs.statSync(fileLocation).isDirectory()) {
res.end(dirListingHTML(pwd, decodeURIComponent(parsedUrl.path)));
Fixed Show fixed Hide fixed
return;
}

res.setHeader("Content-type", "application/octet-stream");
res.end(fs.readFileSync(pwd + parsedUrl.pathname));
res.setHeader("Content-type", "application/octet-stream");

// Check that we are not reading an empty file
if (fs.statSync(fileLocation).size !== 0) {
const file = fs.readFileSync(fileLocation);
res.write(file, 'utf-8')
}
res.end();

} catch (error) {
if (error instanceof Error && error.message == "No such file or directory") {
res.statusCode = 404;
res.end("File not found")
} else {
if (error instanceof Error) {
log(c.redBright(`${error.stack}`));
} else {
log(c.redBright(`${error}`));
}

res.statusCode = 500;
res.end("Internal Server Error")
}
}
});

httpServer.listen(port);
Expand All @@ -75,12 +106,12 @@
httpServer.close()
.once("close", () => {
log(c.blackBright(`Server closed.`));
// httpServer = undefined;
httpServer = undefined;
});
};

export const status = (): void => {
if (httpServer.listening) {
if (httpServer && httpServer.listening) {
log(`Server is running on port ` +
`${c.greenBright(listenPort.toString())} serving ${c.greenBright(servePath)}`);
return;
Expand Down
3 changes: 3 additions & 0 deletions agent/src/ios/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export const info = (): IBinaryModuleDictionary => {

const imports: Set<string> = new Set(a.enumerateImports().map((i) => i.name));
const fb = iosfilesystem.readFile(a.path);
if (typeof(fb) == 'string') {
return;
}

try {
const exe = macho.parse(fb);
Expand Down
7 changes: 5 additions & 2 deletions agent/src/ios/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from "fs";
import * as fs from "frida-fs";
import { Buffer } from "buffer";
import { hexStringToBytes } from "../lib/helpers.js";
import { getNSFileManager } from "./lib/helpers.js";
import {
Expand Down Expand Up @@ -87,7 +88,9 @@ export const pwd = (): string => {
};

// heavy lifting is done in frida-fs here.
export const readFile = (path: string): Buffer => {
export const readFile = (path: string): string | Buffer => {
if (fs.statSync(path).size == 0)
return Buffer.alloc(0);
return fs.readFileSync(path);
};

Expand Down
2 changes: 1 addition & 1 deletion agent/src/rpc/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const ios = {
// ios filesystem
iosFileCwd: (): string => iosfilesystem.pwd(),
iosFileDelete: (path: string): boolean => iosfilesystem.deleteFile(path),
iosFileDownload: (path: string): Buffer => iosfilesystem.readFile(path),
iosFileDownload: (path: string): string | Buffer => iosfilesystem.readFile(path),
iosFileExists: (path: string): boolean => iosfilesystem.exists(path),
iosFileLs: (path: string): IIosFileSystem => iosfilesystem.ls(path),
iosFilePathIsFile: (path: string): boolean => iosfilesystem.pathIsFile(path),
Expand Down
26 changes: 22 additions & 4 deletions objection/commands/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def cd(args: list) -> None:
return

# moving one directory back
if path == '..':
device_path_separator = device_state.platform.path_separator

if path == '..' or path == '..'+device_path_separator:

split_path = os.path.split(current_dir)

Expand All @@ -66,6 +68,10 @@ def cd(args: list) -> None:
# assume the path does not exist by default
does_exist = False

# normalise path to remove '../'
if '..'+device_path_separator in path:
path = os.path.normpath(path).replace('\\', device_path_separator)

# check for existence based on the runtime
if device_state.platform == Ios:
does_exist = _path_exists_ios(path)
Expand All @@ -89,7 +95,13 @@ def cd(args: list) -> None:
# see if its legit.
else:

proposed_path = device_state.platform.path_separator.join([current_dir, path])
proposed_path = device_path_separator.join([current_dir, path])

# normalise path to remove '../'
if '..'+device_path_separator in proposed_path:
proposed_path = os.path.normpath(proposed_path).replace('\\', device_path_separator)
if proposed_path == '//':
return

# assume the proposed_path does not exist by default
does_exist = False
Expand Down Expand Up @@ -785,7 +797,10 @@ def list_folders_in_current_fm_directory() -> dict:
file_name, file_type = entry

if file_type == 'directory':
resp[file_name] = file_name
if ' ' in file_name:
resp[f"'{file_name}'"] = file_name
else:
resp[file_name] = file_name

return resp

Expand Down Expand Up @@ -816,6 +831,9 @@ def list_files_in_current_fm_directory() -> dict:
file_name, file_type = entry

if file_type == 'file':
resp[file_name] = file_name
if ' ' in file_name:
resp[f"'{file_name}'"] = file_name
else:
resp[file_name] = file_name

return resp
5 changes: 4 additions & 1 deletion objection/utils/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ def set_target_pid(self):

elif self.config.spawn:
if self.config.uid is not None:
if self.device.query_system_parameters()['os']['id'] != 'android':
raise Exception('--uid flag can only be used on Android.')
self.pid = self.device.spawn(self.config.name, uid=int(self.config.uid))
else:
self.pid = self.device.spawn(self.config.name)
Expand Down Expand Up @@ -245,9 +247,10 @@ def attach(self):
raise Exception('A PID needs to be set before attach()')

if self.config.uid is None:
debug_print(f'Attaching to PID: {self.pid}')
self.session = self.device.attach(self.pid)
else:
self.session = self.device.attach(self.pid, uid=self.config.uid)
self.session = self.device.attach(self.pid)

self.session.on('detached', self.handlers.session_on_detached)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ requests
Flask>=3.0.0
Pygments>=2.0.0
litecli>=1.3.0
setuptools>=70.0.0
Loading