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

Folder download functionality #700

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
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 @@ const log = (m: string): void => {
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 @@ const dirListingHTML = (p: string): string => {
`;

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 @@ export const start = (pwd: string, port: number = 9000): void => {
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)));
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 @@ export const stop = (): void => {
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
4 changes: 1 addition & 3 deletions agent/src/ios/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ const enumerateKeychain = (): IKeychainData[] => {
});

const keyChainData: IKeychainData[] = [];
keyChainData.concat(...itemClassResults).filter((n) => n !== undefined);

return keyChainData;
return keyChainData.concat(...itemClassResults).filter((n) => n !== undefined);
};

// print raw entries using some Frida magic
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
Loading
Loading