Skip to content

Commit 71f7b2f

Browse files
author
Your Name
committed
Improve symbol lookup and support dumping strings
1 parent 079cd29 commit 71f7b2f

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frida-cshell",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Frida's CShell",
55
"scripts": {
66
"prepare": "npm run build && npm run version && npm run package && npm run copy",

src/cmdlets/string.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { CmdLet } from '../commands/cmdlet.js';
2+
import { Output } from '../io/output.js';
3+
import { Format } from '../misc/format.js';
4+
import { Token } from '../io/token.js';
5+
import { Var } from '../vars/var.js';
6+
import { Mem } from '../memory/mem.js';
7+
8+
const MAX_STRING_LENGTH: number = 1024;
9+
const USAGE: string = `Usage: ds
10+
11+
d address <bytes> - show string
12+
adress the address/symbol to read from
13+
`;
14+
15+
export class DumpStringCmdLet extends CmdLet {
16+
name = 'ds';
17+
category = 'data';
18+
help = 'dump string';
19+
20+
public runSync(tokens: Token[]): Var {
21+
if (tokens.length !== 1) return this.usage();
22+
23+
const t0 = tokens[0] as Token;
24+
const v0 = t0.toVar();
25+
if (v0 === null) return this.usage();
26+
27+
const address = v0.toPointer();
28+
if (address === undefined) return this.usage();
29+
30+
const name = t0.getLiteral();
31+
this.dump(name, address);
32+
return v0;
33+
}
34+
35+
private dump(name: string, address: NativePointer) {
36+
const length = MAX_STRING_LENGTH;
37+
let bytes: Uint8Array = new Uint8Array(0);
38+
let lastError: Error | null = null;
39+
while (length > 0) {
40+
try {
41+
bytes = Mem.readBytes(address, length);
42+
break;
43+
} catch (error) {
44+
if (error instanceof Error) {
45+
lastError = error;
46+
}
47+
continue;
48+
}
49+
}
50+
51+
if (length === 0) {
52+
throw new Error(
53+
`failed to read string from ${Format.toHexString(address)}, ${lastError}`,
54+
);
55+
}
56+
57+
const cp = Memory.alloc(length + 1);
58+
cp.writeByteArray(bytes.buffer as ArrayBuffer);
59+
60+
const value = cp.readUtf8String();
61+
if (value === null || value.length === 0) {
62+
Output.writeln(
63+
`No string found at ${Output.green(name)}: ${Output.yellow(Format.toHexString(address))}`,
64+
);
65+
} else {
66+
Output.write(Output.green(name));
67+
Output.write(' = ');
68+
Output.write(Output.blue("'"));
69+
Output.write(Output.yellow(value));
70+
Output.write(Output.blue("'"));
71+
Output.write(', length: ');
72+
Output.write(Output.blue(value.length.toString()));
73+
Output.write(' (');
74+
Output.write(Output.blue(`0x${value.length.toString(16)}`));
75+
Output.write(')');
76+
Output.writeln();
77+
}
78+
}
79+
80+
public usage(): Var {
81+
Output.write(USAGE);
82+
return Var.ZERO;
83+
}
84+
}

src/commands/cmdlets.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CmdLet } from './cmdlet.js';
22
import { DumpCmdLet } from '../cmdlets/dump.js';
3+
import { DumpStringCmdLet } from '../cmdlets/string.js';
34
import { ExitCmdLet } from '../cmdlets/exit.js';
45
import { SymCmdLet } from '../cmdlets/sym.js';
56
import { ReadCmdLet } from '../cmdlets/read.js';
@@ -60,6 +61,7 @@ export class CmdLets {
6061
this.registerCmdletType(CoverageBpCmdLet);
6162
this.registerCmdletType(DivCmdLet);
6263
this.registerCmdletType(DumpCmdLet);
64+
this.registerCmdletType(DumpStringCmdLet);
6365
this.registerCmdletType(EndianCmdLet);
6466
this.registerCmdletType(ExitCmdLet);
6567
this.registerCmdletType(FdCmdLet);

src/misc/regex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Regex {
77
}
88

99
public static globToRegex(input: string): RegExp | null {
10-
if (!Regex.isGlob(input)) return null;
10+
if (!Regex.isGlob(input)) return new RegExp(`^${input}$`);
1111

1212
const escaped = input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1313
const regex = escaped

0 commit comments

Comments
 (0)