@cheatron/native-mock 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +6 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/kernel32.d.ts +31 -0
- package/dist/src/kernel32.d.ts.map +1 -0
- package/dist/src/kernel32.js +232 -0
- package/dist/src/kernel32.js.map +1 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/dist/{logger.js → src/logger.js} +1 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/msvcrt.d.ts +15 -0
- package/dist/src/msvcrt.d.ts.map +1 -0
- package/dist/src/msvcrt.js +43 -0
- package/dist/src/msvcrt.js.map +1 -0
- package/dist/src/os/handles.d.ts +17 -0
- package/dist/src/os/handles.d.ts.map +1 -0
- package/dist/{os → src/os}/handles.js +2 -0
- package/dist/src/os/handles.js.map +1 -0
- package/dist/src/os/kernel.d.ts +21 -0
- package/dist/src/os/kernel.d.ts.map +1 -0
- package/dist/{os → src/os}/kernel.js +36 -1
- package/dist/src/os/kernel.js.map +1 -0
- package/dist/{os → src/os}/memory.d.ts +1 -5
- package/dist/src/os/memory.d.ts.map +1 -0
- package/dist/{os → src/os}/memory.js +3 -8
- package/dist/src/os/memory.js.map +1 -0
- package/dist/src/os/module.d.ts +11 -0
- package/dist/src/os/module.d.ts.map +1 -0
- package/dist/src/os/module.js +20 -0
- package/dist/src/os/module.js.map +1 -0
- package/dist/{os → src/os}/process.d.ts +4 -0
- package/dist/src/os/process.d.ts.map +1 -0
- package/dist/{os → src/os}/process.js +12 -0
- package/dist/src/os/process.js.map +1 -0
- package/dist/{os → src/os}/thread.d.ts +4 -4
- package/dist/src/os/thread.d.ts.map +1 -0
- package/dist/{os → src/os}/thread.js +1 -0
- package/dist/src/os/thread.js.map +1 -0
- package/dist/utils/src/handle.d.ts +26 -0
- package/dist/utils/src/handle.d.ts.map +1 -0
- package/dist/utils/src/handle.js +56 -0
- package/dist/utils/src/handle.js.map +1 -0
- package/dist/utils/src/index.d.ts +4 -0
- package/dist/utils/src/index.d.ts.map +1 -0
- package/dist/utils/src/index.js +4 -0
- package/dist/utils/src/index.js.map +1 -0
- package/dist/utils/src/module.d.ts +22 -0
- package/dist/utils/src/module.d.ts.map +1 -0
- package/dist/utils/src/module.js +54 -0
- package/dist/utils/src/module.js.map +1 -0
- package/dist/utils/src/process.d.ts +30 -0
- package/dist/utils/src/process.d.ts.map +1 -0
- package/dist/utils/src/process.js +103 -0
- package/dist/utils/src/process.js.map +1 -0
- package/dist/utils/src/thread.d.ts +38 -0
- package/dist/utils/src/thread.d.ts.map +1 -0
- package/dist/utils/src/thread.js +136 -0
- package/dist/utils/src/thread.js.map +1 -0
- package/package.json +7 -7
- package/dist/constants.d.ts +0 -175
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -173
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -4
- package/dist/kernel32.d.ts +0 -22
- package/dist/kernel32.d.ts.map +0 -1
- package/dist/kernel32.js +0 -170
- package/dist/logger.d.ts.map +0 -1
- package/dist/os/handles.d.ts +0 -17
- package/dist/os/handles.d.ts.map +0 -1
- package/dist/os/kernel.d.ts +0 -18
- package/dist/os/kernel.d.ts.map +0 -1
- package/dist/os/memory.d.ts.map +0 -1
- package/dist/os/process.d.ts.map +0 -1
- package/dist/os/thread.d.ts.map +0 -1
- package/dist/process.d.ts +0 -30
- package/dist/process.d.ts.map +0 -1
- package/dist/process.js +0 -124
- package/dist/thread.d.ts +0 -28
- package/dist/thread.d.ts.map +0 -1
- package/dist/thread.js +0 -113
- /package/dist/{logger.d.ts → src/logger.d.ts} +0 -0
package/dist/os/thread.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../../src/os/thread.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACrB,WAAW,IAAA;IACX,KAAK,IAAA;IACL,OAAO,IAAA;IACP,OAAO,IAAA;IACP,UAAU,IAAA;CACX;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,eAAe;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,WAAW,CAAC;IACnB,YAAY,EAAE,MAAM,CAAK;IACzB,OAAO,EAAE,aAAa,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;gBAElB,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IA4BzC,OAAO,IAAI,MAAM;IAQjB,MAAM,IAAI,MAAM;IAUhB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,aAAa;IAKzC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG,IAAI;CAG9C"}
|
package/dist/process.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import * as D from 'win32-def';
|
|
2
|
-
import { type MemoryBasicInformation } from './constants';
|
|
3
|
-
/**
|
|
4
|
-
* Represents a remote process
|
|
5
|
-
*/
|
|
6
|
-
export declare class Process {
|
|
7
|
-
protected _handle: D.HANDLE | null;
|
|
8
|
-
protected _pid: number;
|
|
9
|
-
constructor(handle: D.HANDLE | null, autoClose?: boolean, pid?: number);
|
|
10
|
-
static open(pid: number, access?: number): Process;
|
|
11
|
-
static current(): CurrentProcess;
|
|
12
|
-
get handle(): D.HANDLE | null;
|
|
13
|
-
get pid(): number;
|
|
14
|
-
isValid(): boolean;
|
|
15
|
-
close(): void;
|
|
16
|
-
read(address: number | bigint, size: number): Buffer;
|
|
17
|
-
write(address: number | bigint, buffer: Buffer): void;
|
|
18
|
-
query(address: number | bigint): MemoryBasicInformation;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Represents the current process (singleton)
|
|
22
|
-
*/
|
|
23
|
-
export declare class CurrentProcess extends Process {
|
|
24
|
-
constructor();
|
|
25
|
-
close(): void;
|
|
26
|
-
query(address: number | bigint): MemoryBasicInformation;
|
|
27
|
-
}
|
|
28
|
-
export declare const currentProcess: CurrentProcess;
|
|
29
|
-
export declare const currentProcessId: number;
|
|
30
|
-
//# sourceMappingURL=process.d.ts.map
|
package/dist/process.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"process.d.ts","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,WAAW,CAAC;AAC/B,OAAO,EAIL,KAAK,sBAAsB,EAC5B,MAAM,aAAa,CAAC;AAerB;;GAEG;AACH,qBAAa,OAAO;IAClB,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC;IACnC,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC;gBAGrB,MAAM,EAAE,CAAC,CAAC,MAAM,GAAG,IAAI,EACvB,SAAS,GAAE,OAAc,EACzB,GAAG,GAAE,MAAU;IASjB,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,MAAiC,GAAG,OAAO;IAU5E,MAAM,CAAC,OAAO,IAAI,cAAc;IAIhC,IAAI,MAAM,oBAET;IACD,IAAI,GAAG,WAEN;IAED,OAAO,IAAI,OAAO;IAIlB,KAAK;IAUL,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;IAyBpD,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAsBrD,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;CAkBxD;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,OAAO;;IAMhC,KAAK;IAIL,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;CAQjE;AAGD,eAAO,MAAM,cAAc,gBAAuB,CAAC;AACnD,eAAO,MAAM,gBAAgB,QAAqB,CAAC"}
|
package/dist/process.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { Kernel32Impl as Kernel32 } from './kernel32';
|
|
2
|
-
import { ProcessAccess, MEMORY_BASIC_INFORMATION, MBI_SIZE, } from './constants';
|
|
3
|
-
import koffi from 'koffi';
|
|
4
|
-
import { log } from './logger';
|
|
5
|
-
/**
|
|
6
|
-
* Handle management registry for automatic cleanup
|
|
7
|
-
*/
|
|
8
|
-
const registry = new FinalizationRegistry((handle) => {
|
|
9
|
-
if (handle) {
|
|
10
|
-
log.trace('Process', 'Closing orphaned handle via GC');
|
|
11
|
-
Kernel32.CloseHandle(handle);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
/**
|
|
15
|
-
* Represents a remote process
|
|
16
|
-
*/
|
|
17
|
-
export class Process {
|
|
18
|
-
_handle;
|
|
19
|
-
_pid;
|
|
20
|
-
constructor(handle, autoClose = true, pid = 0) {
|
|
21
|
-
this._handle = handle;
|
|
22
|
-
this._pid = pid;
|
|
23
|
-
if (autoClose && handle) {
|
|
24
|
-
registry.register(this, handle, this);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
static open(pid, access = ProcessAccess.ALL_ACCESS) {
|
|
28
|
-
log.debug('Process', `Opening process ${pid}`, { access });
|
|
29
|
-
const handle = Kernel32.OpenProcess(access, 0, pid);
|
|
30
|
-
if (!handle) {
|
|
31
|
-
log.error('Process', `Failed to open process ${pid}`);
|
|
32
|
-
throw new Error(`Failed to open process ${pid}`);
|
|
33
|
-
}
|
|
34
|
-
return new Process(handle, true, pid);
|
|
35
|
-
}
|
|
36
|
-
static current() {
|
|
37
|
-
return currentProcess;
|
|
38
|
-
}
|
|
39
|
-
get handle() {
|
|
40
|
-
return this._handle;
|
|
41
|
-
}
|
|
42
|
-
get pid() {
|
|
43
|
-
return this._pid;
|
|
44
|
-
}
|
|
45
|
-
isValid() {
|
|
46
|
-
return this._handle !== null && this._handle !== undefined;
|
|
47
|
-
}
|
|
48
|
-
close() {
|
|
49
|
-
if (this.isValid()) {
|
|
50
|
-
log.debug('Process', `Closing process handle ${this._pid}`);
|
|
51
|
-
const h = this._handle;
|
|
52
|
-
this._handle = null;
|
|
53
|
-
registry.unregister(this);
|
|
54
|
-
Kernel32.CloseHandle(h);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
read(address, size) {
|
|
58
|
-
if (!this.isValid())
|
|
59
|
-
throw new Error('Process handle is closed');
|
|
60
|
-
const buffer = Buffer.alloc(size);
|
|
61
|
-
const success = Kernel32.ReadProcessMemory(this._handle, address, buffer, size, null);
|
|
62
|
-
if (!success) {
|
|
63
|
-
const errCode = Kernel32.GetLastError
|
|
64
|
-
? Kernel32.GetLastError()
|
|
65
|
-
: 'unknown';
|
|
66
|
-
log.error('Process', `ReadProcessMemory failed at ${address}`, {
|
|
67
|
-
size,
|
|
68
|
-
errCode,
|
|
69
|
-
});
|
|
70
|
-
throw new Error('ReadProcessMemory failed');
|
|
71
|
-
}
|
|
72
|
-
return buffer;
|
|
73
|
-
}
|
|
74
|
-
write(address, buffer) {
|
|
75
|
-
if (!this.isValid())
|
|
76
|
-
throw new Error('Process handle is closed');
|
|
77
|
-
const success = Kernel32.WriteProcessMemory(this._handle, address, buffer, buffer.length, null);
|
|
78
|
-
if (!success) {
|
|
79
|
-
const errCode = Kernel32.GetLastError
|
|
80
|
-
? Kernel32.GetLastError()
|
|
81
|
-
: 'unknown';
|
|
82
|
-
log.error('Process', `WriteProcessMemory failed at ${address}`, {
|
|
83
|
-
size: buffer.length,
|
|
84
|
-
errCode,
|
|
85
|
-
});
|
|
86
|
-
throw new Error('WriteProcessMemory failed');
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
query(address) {
|
|
90
|
-
if (!this.isValid())
|
|
91
|
-
throw new Error('Process handle is closed');
|
|
92
|
-
const buffer = Buffer.alloc(MBI_SIZE);
|
|
93
|
-
const result = Kernel32.VirtualQueryEx(this._handle, address, buffer, MBI_SIZE);
|
|
94
|
-
if (!result) {
|
|
95
|
-
log.error('Process', `VirtualQueryEx failed at ${address}`);
|
|
96
|
-
throw new Error('VirtualQueryEx failed');
|
|
97
|
-
}
|
|
98
|
-
const info = koffi.decode(buffer, MEMORY_BASIC_INFORMATION);
|
|
99
|
-
return info;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Represents the current process (singleton)
|
|
104
|
-
*/
|
|
105
|
-
export class CurrentProcess extends Process {
|
|
106
|
-
constructor() {
|
|
107
|
-
// Current process uses a pseudo-handle that doesn't need closing
|
|
108
|
-
super(Kernel32.GetCurrentProcess(), false, Kernel32.GetCurrentProcessId());
|
|
109
|
-
}
|
|
110
|
-
close() {
|
|
111
|
-
this._handle = null;
|
|
112
|
-
}
|
|
113
|
-
query(address) {
|
|
114
|
-
const buffer = Buffer.alloc(MBI_SIZE);
|
|
115
|
-
const result = Kernel32.VirtualQuery(address, buffer, MBI_SIZE);
|
|
116
|
-
if (!result)
|
|
117
|
-
throw new Error('VirtualQuery failed');
|
|
118
|
-
const info = koffi.decode(buffer, MEMORY_BASIC_INFORMATION);
|
|
119
|
-
return info;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
// Export a pre-initialized instance of the current process
|
|
123
|
-
export const currentProcess = new CurrentProcess();
|
|
124
|
-
export const currentProcessId = currentProcess.pid;
|
package/dist/thread.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import * as D from 'win32-def';
|
|
2
|
-
import { type ThreadContext } from './constants';
|
|
3
|
-
/**
|
|
4
|
-
* Represents a thread handle
|
|
5
|
-
*/
|
|
6
|
-
export declare class Thread {
|
|
7
|
-
protected _handle: D.HANDLE | null;
|
|
8
|
-
constructor(handle: D.HANDLE | null, autoClose?: boolean);
|
|
9
|
-
static open(threadId: number, access?: number): Thread;
|
|
10
|
-
static current(): CurrentThread;
|
|
11
|
-
static currentId(): number;
|
|
12
|
-
isValid(): boolean;
|
|
13
|
-
close(): void;
|
|
14
|
-
suspend(): number;
|
|
15
|
-
resume(): number;
|
|
16
|
-
getContext(flags?: number): ThreadContext;
|
|
17
|
-
setContext(ctx: ThreadContext): void;
|
|
18
|
-
get handle(): D.HANDLE | null;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Represents the current thread (singleton)
|
|
22
|
-
*/
|
|
23
|
-
export declare class CurrentThread extends Thread {
|
|
24
|
-
constructor();
|
|
25
|
-
close(): void;
|
|
26
|
-
}
|
|
27
|
-
export declare const currentThread: CurrentThread;
|
|
28
|
-
//# sourceMappingURL=thread.d.ts.map
|
package/dist/thread.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../src/thread.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,WAAW,CAAC;AAC/B,OAAO,EAKL,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;AAerB;;GAEG;AACH,qBAAa,MAAM;IACjB,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC;gBAEvB,MAAM,EAAE,CAAC,CAAC,MAAM,GAAG,IAAI,EAAE,SAAS,GAAE,OAAc;IAO9D,MAAM,CAAC,IAAI,CACT,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,MAAgC,GACvC,MAAM;IAUT,MAAM,CAAC,OAAO,IAAI,aAAa;IAI/B,MAAM,CAAC,SAAS,IAAI,MAAM;IAI1B,OAAO,IAAI,OAAO;IAIlB,KAAK;IAUL,OAAO,IAAI,MAAM;IAUjB,MAAM,IAAI,MAAM;IAUhB,UAAU,CAAC,KAAK,GAAE,MAA0B,GAAG,aAAa;IAgB5D,UAAU,CAAC,GAAG,EAAE,aAAa,GAAG,IAAI;IAapC,IAAI,MAAM,oBAET;CACF;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,MAAM;;IAM9B,KAAK;CAGf;AAGD,eAAO,MAAM,aAAa,eAAsB,CAAC"}
|
package/dist/thread.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Kernel32Impl as Kernel32 } from './kernel32';
|
|
2
|
-
import { ThreadAccess, ContextFlags, CONTEXT, CONTEXT_SIZE, } from './constants';
|
|
3
|
-
import koffi from 'koffi';
|
|
4
|
-
import { log } from './logger';
|
|
5
|
-
/**
|
|
6
|
-
* Handle management registry for automatic cleanup
|
|
7
|
-
*/
|
|
8
|
-
const registry = new FinalizationRegistry((handle) => {
|
|
9
|
-
if (handle) {
|
|
10
|
-
log.trace('Thread', 'Closing orphaned handle via GC');
|
|
11
|
-
Kernel32.CloseHandle(handle);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
/**
|
|
15
|
-
* Represents a thread handle
|
|
16
|
-
*/
|
|
17
|
-
export class Thread {
|
|
18
|
-
_handle;
|
|
19
|
-
constructor(handle, autoClose = true) {
|
|
20
|
-
this._handle = handle;
|
|
21
|
-
if (autoClose && handle) {
|
|
22
|
-
registry.register(this, handle, this);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
static open(threadId, access = ThreadAccess.ALL_ACCESS) {
|
|
26
|
-
log.debug('Thread', `Opening thread ${threadId}`, { access });
|
|
27
|
-
const handle = Kernel32.OpenThread(access, 0, threadId);
|
|
28
|
-
if (!handle) {
|
|
29
|
-
log.error('Thread', `Failed to open thread ${threadId}`);
|
|
30
|
-
throw new Error(`Failed to open thread ${threadId}`);
|
|
31
|
-
}
|
|
32
|
-
return new Thread(handle);
|
|
33
|
-
}
|
|
34
|
-
static current() {
|
|
35
|
-
return currentThread;
|
|
36
|
-
}
|
|
37
|
-
static currentId() {
|
|
38
|
-
return Kernel32.GetCurrentThreadId();
|
|
39
|
-
}
|
|
40
|
-
isValid() {
|
|
41
|
-
return this._handle !== null && this._handle !== undefined;
|
|
42
|
-
}
|
|
43
|
-
close() {
|
|
44
|
-
if (this.isValid()) {
|
|
45
|
-
log.debug('Thread', 'Closing thread handle');
|
|
46
|
-
const h = this._handle;
|
|
47
|
-
this._handle = null;
|
|
48
|
-
registry.unregister(this);
|
|
49
|
-
Kernel32.CloseHandle(h);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
suspend() {
|
|
53
|
-
if (!this.isValid())
|
|
54
|
-
throw new Error('Thread handle is closed');
|
|
55
|
-
const count = Kernel32.SuspendThread(this._handle);
|
|
56
|
-
if (count === 0xffffffff) {
|
|
57
|
-
log.error('Thread', 'SuspendThread failed');
|
|
58
|
-
throw new Error('SuspendThread failed');
|
|
59
|
-
}
|
|
60
|
-
return count;
|
|
61
|
-
}
|
|
62
|
-
resume() {
|
|
63
|
-
if (!this.isValid())
|
|
64
|
-
throw new Error('Thread handle is closed');
|
|
65
|
-
const count = Kernel32.ResumeThread(this._handle);
|
|
66
|
-
if (count === 0xffffffff) {
|
|
67
|
-
log.error('Thread', 'ResumeThread failed');
|
|
68
|
-
throw new Error('ResumeThread failed');
|
|
69
|
-
}
|
|
70
|
-
return count;
|
|
71
|
-
}
|
|
72
|
-
getContext(flags = ContextFlags.FULL) {
|
|
73
|
-
if (!this.isValid())
|
|
74
|
-
throw new Error('Thread handle is closed');
|
|
75
|
-
const buf = Buffer.alloc(CONTEXT_SIZE);
|
|
76
|
-
// ContextFlags is at offset 0x30 (after 6 × uint64 P*Home registers)
|
|
77
|
-
buf.writeUInt32LE(flags, 0x30);
|
|
78
|
-
const success = Kernel32.GetThreadContext(this._handle, buf);
|
|
79
|
-
if (!success) {
|
|
80
|
-
log.error('Thread', 'GetThreadContext failed');
|
|
81
|
-
throw new Error('GetThreadContext failed');
|
|
82
|
-
}
|
|
83
|
-
return koffi.decode(buf, CONTEXT);
|
|
84
|
-
}
|
|
85
|
-
setContext(ctx) {
|
|
86
|
-
if (!this.isValid())
|
|
87
|
-
throw new Error('Thread handle is closed');
|
|
88
|
-
const buf = Buffer.alloc(CONTEXT_SIZE);
|
|
89
|
-
koffi.encode(buf, CONTEXT, ctx);
|
|
90
|
-
const success = Kernel32.SetThreadContext(this._handle, buf);
|
|
91
|
-
if (!success) {
|
|
92
|
-
log.error('Thread', 'SetThreadContext failed');
|
|
93
|
-
throw new Error('SetThreadContext failed');
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
get handle() {
|
|
97
|
-
return this._handle;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Represents the current thread (singleton)
|
|
102
|
-
*/
|
|
103
|
-
export class CurrentThread extends Thread {
|
|
104
|
-
constructor() {
|
|
105
|
-
// Current thread uses a pseudo-handle that doesn't need closing
|
|
106
|
-
super(Kernel32.GetCurrentThread(), false);
|
|
107
|
-
}
|
|
108
|
-
close() {
|
|
109
|
-
this._handle = null;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
// Export a pre-initialized instance of the current thread
|
|
113
|
-
export const currentThread = new CurrentThread();
|
|
File without changes
|