Skip to content

Commit

Permalink
Support for logging and cat command
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 18, 2024
1 parent 5170625 commit a633687
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frida-cshell",
"version": "1.4.2",
"version": "1.4.3",
"description": "Frida's CShell",
"scripts": {
"prepare": "npm run build && npm run version && npm run package && npm run copy",
Expand Down
38 changes: 38 additions & 0 deletions src/cmdlets/cat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CmdLet } from '../commands/cmdlet.js';
import { Output } from '../io/output.js';
import { Token } from '../io/token.js';
import { Var } from '../vars/var.js';

const USAGE: string = `Usage: cat
cat file - dump file
file the file to dump`;

export class CatCmdLet extends CmdLet {
name = 'cat';
category = 'misc';
help = 'dump a file';

public runSync(tokens: Token[]): Var {
const vars = this.transform(tokens, [this.parseLiteral]);
if (vars === null) return this.usage();

const [file] = vars as [string];
Output.writeln(`Dumping file: ${Output.green(file)}`);

try {
const text = File.readAllText(file);
const lines = text.split('\n');
lines.forEach(l => Output.writeln(Output.yellow(l), true));
} catch {
Output.writeln(`failed to read file: ${Output.green(file)}`);
}

return Var.ZERO;
}

public usage(): Var {
Output.writeln(USAGE);
return Var.ZERO;
}
}
4 changes: 4 additions & 0 deletions src/cmdlets/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import { VarCmdLet } from './var.js';
import { VmCmdLet } from './vm.js';
import { WriteCmdLet } from './write.js';
import { GrepCmdLet } from './grep.js';
import { CatCmdLet } from './cat.js';
import { LogCmdLet } from './log.js';

const USAGE: string = `Usage: js
Expand Down Expand Up @@ -86,6 +88,7 @@ export class JsCmdLet extends CmdLet {
Bp: Bp,
Bps: Bps,
BtCmdLet: BtCmdLet,
CatCmdLet: CatCmdLet,
CharCode: CharCode,
CmdLets: CmdLets,
Command: Command,
Expand All @@ -105,6 +108,7 @@ export class JsCmdLet extends CmdLet {
InsnBpCmdLet: InsnBpCmdLet,
LdCmdLet: LdCmdLet,
Line: Line,
LogCmdLet: LogCmdLet,
Mem: Mem,
MemoryBps: MemoryBps,
ModCmdLet: ModCmdLet,
Expand Down
47 changes: 47 additions & 0 deletions src/cmdlets/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { CmdLet } from '../commands/cmdlet.js';
import { Output } from '../io/output.js';
import { Token } from '../io/token.js';
import { Var } from '../vars/var.js';

const USAGE: string = `Usage: log
log - clear log file
log file - set log file
file the file to log to`;

export class LogCmdLet extends CmdLet {
name = 'log';
category = 'misc';
help = 'set log file';

public runSync(tokens: Token[]): Var {
const vars = this.transformOptional(tokens, [], [this.parseLiteral]);
if (vars === null) return this.usage();
const [_, [file]] = vars as [[], [string | null]];
if (file === null) {
Output.clearLog();
Output.writeln('log file cleared');
} else {
try {
Output.writeln(
[
'log file set to ',
Output.blue("'"),
Output.green(file),
Output.blue("'"),
].join(''),
);
Output.setLog(file);
} catch {
Output.writeln(`invalid log file: ${file}`);
}
}
return Var.ZERO;
}

public usage(): Var {
Output.writeln(USAGE);
return Var.ZERO;
}
}
4 changes: 4 additions & 0 deletions src/commands/cmdlets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import { ShCmdLet } from '../cmdlets/sh.js';
import { SrcCmdLet } from '../cmdlets/src.js';
import { VerboseCmdLet } from '../cmdlets/verbose.js';
import { GrepCmdLet } from '../cmdlets/grep.js';
import { CatCmdLet } from '../cmdlets/cat.js';
import { LogCmdLet } from '../cmdlets/log.js';

export class CmdLets {
private static byName: Map<string, CmdLet> = new Map<string, CmdLet>();
Expand All @@ -59,6 +61,7 @@ export class CmdLets {
this.registerCmdletType(BlockTraceBpCmdLet);
this.registerCmdletType(BtCmdLet);
this.registerCmdletType(CallTraceBpCmdLet);
this.registerCmdletType(CatCmdLet);
this.registerCmdletType(CopyCmdLet);
this.registerCmdletType(CoverageBpCmdLet);
this.registerCmdletType(DivCmdLet);
Expand All @@ -75,6 +78,7 @@ export class CmdLets {
this.registerCmdletType(InsnBpCmdLet);
this.registerCmdletType(JsCmdLet);
this.registerCmdletType(LdCmdLet);
this.registerCmdletType(LogCmdLet);
this.registerCmdletType(OrCmdLet);
this.registerCmdletType(ReadCmdLet);
this.registerCmdletType(ModCmdLet);
Expand Down
31 changes: 25 additions & 6 deletions src/io/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Output {
private static verbose: boolean = false;
private static indent: boolean = false;
private static filter: RegExp | null = null;
private static log: File | null = null;

public static banner() {
this.shell
Expand Down Expand Up @@ -72,34 +73,38 @@ export class Output {
filter: boolean,
) {
if (verbose && !this.verbose) return;

if (buffer === null) return;

const filterExpression = (l: string) =>
filter === false ||
l.length === 0 ||
l.trim().length === 0 ||
Output.filter === null ||
Output.filter.test(l);

let text = '';
if (this.indent) {
if (buffer.endsWith('\n')) {
const lines = buffer.slice(0, buffer.length - 1).split('\n');
const fixed = lines
.filter(filterExpression)
.join(`\r\n${Output.yellow('| ')}`);
send(['frida:stderr', `${Output.yellow('| ')}${fixed}\r\n`]);
text = `${Output.yellow('| ')}${fixed}\r\n`;
} else {
const lines = buffer.split('\n');
const fixed = lines
.filter(filterExpression)
.join(`\r\n${Output.yellow('| ')}`);
send(['frida:stderr', `${Output.yellow('| ')}${fixed}`]);
text = `${Output.yellow('| ')}${fixed}`;
}
} else {
const lines = buffer.split('\n');
const fixed = lines.filter(filterExpression).join(`\r\n`);
send(['frida:stderr', fixed]);
text = lines.filter(filterExpression).join(`\r\n`);
}

if (this.log !== null) {
this.log.write(text);
}
send(['frida:stderr', text]);
}

public static clearLine() {
Expand Down Expand Up @@ -155,4 +160,18 @@ export class Output {
public static isFiltered() {
return this.filter !== null;
}

public static setLog(log: string) {
this.log = new File(log, 'w');
}

public static clearLog() {
if (this.log !== null) {
const pos = this.log.tell();
Output.writeln(`Wrote ${Output.blue(pos.toString())} bytes to log.`);
this.log.flush();
this.log.close();
this.log = null;
}
}
}

0 comments on commit a633687

Please sign in to comment.