Skip to content

Commit a03619f

Browse files
author
Your Name
committed
Added sz command
1 parent d48c80f commit a03619f

File tree

23 files changed

+1107
-24
lines changed

23 files changed

+1107
-24
lines changed

.devcontainer/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ COPY assets/cshell/vm-arm32 /bin/vm-arm32
757757
COPY assets/cshell/vm-arm64 /bin/vm-arm64
758758
COPY assets/cshell/vm-x64 /bin/vm-x64
759759
COPY assets/cshell/vm-x86 /bin/vm-x86
760+
COPY assets/cshell/vm-pty /bin/vm-pty
760761
WORKDIR /root/
761762

762763
################################################################################
@@ -787,7 +788,9 @@ FROM cshell AS frida-cshell
787788
RUN apt-get update && \
788789
apt-get install -y \
789790
nano \
790-
psmisc
791+
psmisc \
792+
picocom \
793+
lrzsz
791794

792795
ARG VSCODE_COMMIT_ID
793796
ENV VSCODE_COMMIT_ID=$VSCODE_COMMIT_ID

.vscode/tasks.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,33 @@
287287
}
288288
}
289289
},
290+
{
291+
"type": "shell",
292+
"command": "vm-pty sh /home/ws/frida-cshell -f /bin/target32",
293+
"problemMatcher": [],
294+
"label": "VM pty",
295+
"group": {
296+
"kind": "build",
297+
"isDefault": true
298+
},
299+
"presentation": {
300+
"echo": true,
301+
"reveal": "always",
302+
"focus": true,
303+
"panel": "shared",
304+
"showReuseMessage": false,
305+
"clear": false,
306+
"close": false
307+
},
308+
"dependsOn": [
309+
"Build"
310+
],
311+
"options": {
312+
"env": {
313+
"FRIDA_INJECT": "frida-inject-32"
314+
}
315+
}
316+
},
290317
{
291318
"label": "Build Image",
292319
"type": "shell",

assets/cshell/vm-pty

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
cmd="$@"
3+
# see https://bugs.launchpad.net/qemu/+bug/1790975
4+
qemu-system-arm \
5+
-machine virtualization=true \
6+
-machine virt,highmem=off \
7+
-smp 4 \
8+
-kernel /root/zImage-arm32 \
9+
-initrd /root/initramfs-arm32.img \
10+
-append "earlyprintk=serial,ttyAMA0 console=ttyAMA0 coredump_filter=0x3f FRIDA_INJECT=$FRIDA_INJECT cmd=\"$cmd\"" \
11+
-m 4096M \
12+
-net nic,id=eth \
13+
-net user,id=mynet,net=192.168.76.0/24 \
14+
-virtfs local,path=/home/share/,mount_tag=host0,security_model=passthrough,id=host0 \
15+
-virtfs local,path=/home/ws/,mount_tag=host1,security_model=passthrough,id=host1 \
16+
-nographic \
17+
-no-reboot \
18+
-serial pty

package

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fi
7979
if [ \${debug} ]; then
8080
opt="{\"debug\":true}"
8181
else
82-
opt="{}"
82+
opt="{\"debug\":false}"
8383
fi
8484
8585
if [ -z \${file} ] && [ -z \${name} ] && [ -z \${pid} ]; then

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.7.9",
3+
"version": "1.8.0",
44
"description": "Frida's CShell",
55
"scripts": {
66
"prepare": "npm run version && npm run build && npm run package && npm run copy",

src/cmdlets/development/js.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import { ReplaceCmdLet } from '../breakpoints/replace.js';
8383
import { EchoCmdLet } from '../misc/echo.js';
8484
import { CorpseCmdLet } from '../misc/corpse/corpse.js';
8585
import { ErrnoCmdLet } from '../misc/errno.js';
86+
import { SzCmdLet } from '../files/sz.js';
8687

8788
export class JsCmdLet extends CmdLetBase {
8889
name = 'js';
@@ -167,6 +168,7 @@ js path - load commandlet JS script
167168
ShlCmdLet: ShlCmdLet,
168169
ShrCmdLet: ShrCmdLet,
169170
SrcCmdLet: JsCmdLet,
171+
SzCmdLet: SzCmdLet,
170172
SubCmdLet: SubCmdLet,
171173
SymCmdLet: SymCmdLet,
172174
ThreadCmdLet: ThreadCmdLet,

src/cmdlets/files/src.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CharCode } from '../../io/char.js';
33
import { Input } from '../../io/input.js';
44
import { Output } from '../../io/output.js';
55
import { Token } from '../../io/token.js';
6+
import { Format } from '../../misc/format.js';
67
import { History } from '../../terminal/history.js';
78
import { Var } from '../../vars/var.js';
89

@@ -64,7 +65,10 @@ src path - run commands from file
6465
if (line.length === 0) continue;
6566
if (line.charAt(0) === '#') continue;
6667
Output.write(line);
67-
await Input.read(`${line}${String.fromCharCode(CharCode.CR)}`);
68+
const bytes = Format.toByteArray(
69+
`${line}${String.fromCharCode(CharCode.CR)}`,
70+
);
71+
await Input.read(bytes);
6872
}
6973

7074
Output.clearLine();

src/cmdlets/files/sz.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { CmdLetBase } from '../../commands/cmdlet.js';
2+
import { Input } from '../../io/input.js';
3+
import { Output } from '../../io/output.js';
4+
import { Token } from '../../io/token.js';
5+
import { InputBuffer } from '../../io/zmodem/input.js';
6+
import { OutputBuffer } from '../../io/zmodem/output.js';
7+
import { Sz } from '../../io/zmodem/sz.js';
8+
import { Zmodem } from '../../io/zmodem/zmodem.js';
9+
import { Files } from '../../misc/files.js';
10+
import { Version } from '../../misc/version.js';
11+
import { Var } from '../../vars/var.js';
12+
13+
export class SzCmdLet extends CmdLetBase {
14+
name = 'sz';
15+
category = 'files';
16+
help = 'send a file using Z-Modem';
17+
18+
private static readonly USAGE: string = `Usage: sz
19+
20+
cat file - send file
21+
file the file to send`;
22+
23+
public override runSync(_tokens: Token[]): Var {
24+
throw new Error("can't run in synchronous mode");
25+
}
26+
27+
public override async run(tokens: Token[]): Promise<Var> {
28+
const vars = this.transform(tokens, [this.parseLiteral]);
29+
if (vars === null) return this.usage();
30+
31+
const [filePath] = vars as [string];
32+
Output.writeln(`Sending file: ${Output.green(filePath)}`);
33+
34+
const debugFileName = Files.getRandomFileName('debug');
35+
let debug = (_msg: string) => {};
36+
37+
if (Output.getDebugging()) {
38+
Output.debug(`writing debug to: ${debugFileName}`);
39+
const debugFile = new File(debugFileName, 'w');
40+
debug = (msg: string) => {
41+
debugFile.write(`${msg}\n`);
42+
debugFile.flush();
43+
};
44+
}
45+
46+
debug('Starting transmission');
47+
48+
Output.writeln(`Transmission will start in 2 seconds....`);
49+
Output.writeln();
50+
51+
const input = new InputBuffer(debug);
52+
const output = new OutputBuffer(debug);
53+
54+
Input.setInterceptRaw(input);
55+
try {
56+
await Sz.sleep(2000);
57+
const zmodem = new Zmodem(input, output, debug);
58+
await zmodem.send(filePath);
59+
} catch (error) {
60+
if (error instanceof Error) {
61+
debug(`Error: ${error.message}`);
62+
debug(`Stack: ${error.stack}`);
63+
} else {
64+
debug(`Error: Unknown error`);
65+
}
66+
} finally {
67+
Input.setInterceptRaw(null);
68+
this.checkDebugFile(debugFileName);
69+
}
70+
return Var.ZERO;
71+
}
72+
73+
private checkDebugFile(debugFileName: string) {
74+
Output.debug('ZModem output...');
75+
try {
76+
const debugFile = new File(debugFileName, 'r');
77+
for (
78+
let line = debugFile.readLine();
79+
line.length != 0;
80+
line = debugFile.readLine()
81+
) {
82+
Output.debug(`\t${Output.yellow(line.trimEnd())}`);
83+
}
84+
} finally {
85+
Output.debug('ZModem output complete');
86+
}
87+
}
88+
89+
public usage(): Var {
90+
Output.writeln(SzCmdLet.USAGE);
91+
return Var.ZERO;
92+
}
93+
94+
public override isSupported(): boolean {
95+
switch (Process.platform) {
96+
case 'linux':
97+
if (Version.VERSION >= Version.BINARY_MODE_MIN_VERSION) {
98+
return true;
99+
} else {
100+
return false;
101+
}
102+
case 'windows':
103+
case 'barebone':
104+
case 'darwin':
105+
case 'freebsd':
106+
case 'qnx':
107+
default:
108+
return false;
109+
}
110+
}
111+
}

src/cmdlets/misc/sh.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ sh - run a shell`;
8080
throw new Error('failed to find necessary native functions');
8181

8282
const shell = Memory.allocUtf8String('SHELL');
83-
const { value: shellVar, errno: getenvErrno } = this.fnGetEnv(
83+
const { value: shellVar, errno: _getenvErrno } = this.fnGetEnv(
8484
shell,
8585
) as UnixSystemFunctionResult<NativePointer>;
86-
if (shellVar.equals(ptr(0)))
87-
throw new Error(`failed to getenv("SHELL"), errno: ${getenvErrno}`);
8886

89-
const shellPath = shellVar.readUtf8String();
87+
const shellPath = shellVar.equals(ptr(0))
88+
? '/bin/sh'
89+
: shellVar.readUtf8String();
9090
Output.debug(`SHELL: ${shellPath}`);
9191

9292
if (shellPath === null) throw new Error('failed to read SHELL');
@@ -225,8 +225,8 @@ sh - run a shell`;
225225
});
226226

227227
const onRaw: InputInterceptRaw = {
228-
addRaw(raw: string) {
229-
output.write(Format.toByteArray(raw));
228+
addRaw(bytes: ArrayBuffer) {
229+
output.write(bytes);
230230
},
231231
abortRaw() {},
232232
};

0 commit comments

Comments
 (0)