From eaae28f5b395b5e4a9295ad127944127936d8da2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 15 Nov 2024 13:49:11 +0000 Subject: [PATCH] Added errno cmdlet --- package-lock.json | 4 +- package.json | 2 +- src/cmdlets/development/js.ts | 2 + src/cmdlets/misc/corpse/core_pattern.ts | 4 +- src/cmdlets/misc/errno.ts | 88 +++++++++++++++++++++++++ src/commands/cmdlets.ts | 2 + 6 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/cmdlets/misc/errno.ts diff --git a/package-lock.json b/package-lock.json index 554ba4b..abeb999 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "frida-cshell", - "version": "1.7.4", + "version": "1.7.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "frida-cshell", - "version": "1.7.4", + "version": "1.7.5", "devDependencies": { "@eslint/js": "^9.10.0", "@types/frida-gum": "^18.7", diff --git a/package.json b/package.json index 70d977f..7f23557 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "frida-cshell", - "version": "1.7.4", + "version": "1.7.5", "description": "Frida's CShell", "scripts": { "prepare": "npm run build && npm run version && npm run package && npm run copy", diff --git a/src/cmdlets/development/js.ts b/src/cmdlets/development/js.ts index f634bae..b80b260 100644 --- a/src/cmdlets/development/js.ts +++ b/src/cmdlets/development/js.ts @@ -82,6 +82,7 @@ import { MacroCmdLet } from '../misc/macro.js'; import { ReplaceCmdLet } from '../breakpoints/replace.js'; import { EchoCmdLet } from '../misc/echo.js'; import { CorpseCmdLet } from '../misc/corpse/corpse.js'; +import { ErrnoCmdLet } from '../misc/errno.js'; export class JsCmdLet extends CmdLetBase { name = 'js'; @@ -126,6 +127,7 @@ js path - load commandlet JS script DumpCmdLet: DumpCmdLet, EchoCmdLet: EchoCmdLet, EqCmdLet: EqCmdLet, + ErrnoCmdLet: ErrnoCmdLet, ExitCmdLet: ExitCmdLet, FalseCmdLet: FalseCmdLet, FdCmdLet: FdCmdLet, diff --git a/src/cmdlets/misc/corpse/core_pattern.ts b/src/cmdlets/misc/corpse/core_pattern.ts index 9208c6b..89b93f7 100644 --- a/src/cmdlets/misc/corpse/core_pattern.ts +++ b/src/cmdlets/misc/corpse/core_pattern.ts @@ -9,7 +9,9 @@ export class CorePattern { public static get(): string { const value = File.readAllText(CorePattern.CORE_PATTERN).trimEnd(); if (value.startsWith('|')) - throw new Error(`core pattern must not start with '|' - value: '${value}'`); + throw new Error( + `core pattern must not start with '|' - value: '${value}'`, + ); if (value.indexOf('%') !== -1) throw new Error(`core pattern must not contain '%' - value: '${value}'`); diff --git a/src/cmdlets/misc/errno.ts b/src/cmdlets/misc/errno.ts new file mode 100644 index 0000000..cebd2c6 --- /dev/null +++ b/src/cmdlets/misc/errno.ts @@ -0,0 +1,88 @@ +import { CmdLetBase } from '../../commands/cmdlet.js'; +import { Output } from '../../io/output.js'; +import { Token } from '../../io/token.js'; +import { Var } from '../../vars/var.js'; + +export class ErrnoCmdLet extends CmdLetBase { + name = 'errno'; + category = 'misc'; + help = 'displays the errno value'; + private static readonly USAGE: string = `Usage: errno + +errno - display the errno value`; + + private fnErrnoLocation: SystemFunction | null = null; + + public runSync(tokens: Token[]): Var { + const retGetErrno = this.getErrno(tokens); + if (retGetErrno !== null) return retGetErrno; + + const retSetErrno = this.setErrno(tokens); + if (retSetErrno !== null) return retSetErrno; + + return this.usage(); + } + + private setErrno(tokens: Token[]): Var | null { + const vars = this.transform(tokens, [this.parseVar]); + if (vars === null) return null; + const [value] = vars as [Var]; + + const errno = value.toU64().toNumber(); + const location = this.getErrnoLocation(); + location.writeInt(errno); + return value; + } + + private getErrno(tokens: Token[]): Var | null { + if (tokens.length !== 0) return null; + const location = this.getErrnoLocation(); + + const errno = location.readInt(); + Output.writeln(`errno: ${errno}`); + + return new Var(uint64(errno), 'errno'); + } + + private getErrnoLocation(): NativePointer { + const fnErrnoLocation = this.fnErrnoLocation as SystemFunction< + NativePointer, + [] + >; + const location = + fnErrnoLocation() as UnixSystemFunctionResult; + if (location.value.equals(ptr(0))) + throw new Error('failed to get __errno_location()'); + return location.value; + } + + public usage(): Var { + Output.writeln(ErrnoCmdLet.USAGE); + return Var.ZERO; + } + + public override isSupported(): boolean { + switch (Process.platform) { + case 'linux': { + const pErrnoLocation = Module.findExportByName( + null, + '__errno_location', + ); + if (pErrnoLocation === null) return false; + this.fnErrnoLocation = new SystemFunction( + pErrnoLocation, + 'pointer', + [], + ); + return true; + } + case 'darwin': + case 'freebsd': + case 'qnx': + case 'windows': + case 'barebone': + default: + return false; + } + } +} diff --git a/src/commands/cmdlets.ts b/src/commands/cmdlets.ts index 41fad16..d925293 100644 --- a/src/commands/cmdlets.ts +++ b/src/commands/cmdlets.ts @@ -69,6 +69,7 @@ import { MacroCmdLet } from '../cmdlets/misc/macro.js'; import { ReplaceCmdLet } from '../cmdlets/breakpoints/replace.js'; import { EchoCmdLet } from '../cmdlets/misc/echo.js'; import { CorpseCmdLet } from '../cmdlets/misc/corpse/corpse.js'; +import { ErrnoCmdLet } from '../cmdlets/misc/errno.js'; export class CmdLets { private static byName: Map = new Map(); @@ -91,6 +92,7 @@ export class CmdLets { this.registerCmdletType(EchoCmdLet); this.registerCmdletType(EndianCmdLet); this.registerCmdletType(EqCmdLet); + this.registerCmdletType(ErrnoCmdLet); this.registerCmdletType(ExitCmdLet); this.registerCmdletType(FalseCmdLet); this.registerCmdletType(FdCmdLet);