Skip to content

Commit 3818bb2

Browse files
committed
Add V1 of ctypes_definition for TEB
1 parent 2b64a6d commit 3818bb2

6 files changed

Lines changed: 66 additions & 19 deletions

File tree

ctypes_generation/definitions/functions/process.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ HMODULE LoadLibraryExW(
6363

6464
BOOL FreeLibrary(
6565
HMODULE hLibModule
66-
);
66+
);
67+
68+
69+
/* Not documented by seems present since dawn of time (WRK)
70+
I Prefere PVOID as a return value to allow simple cast to PEB subclass in process.py*/
71+
72+
PVOID RtlGetCurrentPeb ();

ctypes_generation/definitions/structures/teb_peb.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,17 @@ typedef struct _EXCEPTION_REGISTRATION_RECORD {
198198
};
199199

200200
typedef struct _NT_TIB {
201-
_EXCEPTION_REGISTRATION_RECORD *ExceptionList;
202-
PVOID StackBase;
203-
PVOID StackLimit;
204-
PVOID SubSystemTib;
205-
PVOID FiberData;
206-
ULONG Version;
207-
PVOID ArbitraryUserPointer;
208-
_NT_TIB *Self;
209-
};
201+
struct _EXCEPTION_REGISTRATION_RECORD *ExceptionList;
202+
PVOID StackBase;
203+
PVOID StackLimit;
204+
PVOID SubSystemTib;
205+
union {
206+
PVOID FiberData;
207+
// ULONG Version; // Sub-union break remotectypes generation for now -> Ignore this field until fixed
208+
};
209+
PVOID ArbitraryUserPointer;
210+
struct _NT_TIB *Self;
211+
} NT_TIB;
210212

211213
typedef struct _TEB {
212214
_NT_TIB NtTib;

ctypes_generation/winstruct.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ def prepare_anon_struct(self):
123123

124124
def generate_selfref_ctypes_class(self):
125125
res = ["# Self referencing struct tricks"]
126-
res += ["""class {0}(Structure): pass""".format(self.name)]
127-
# res += [self.generate_anonymous_union()]
126+
res += ["""class {0}(Structure):""".format(self.name)]
127+
# We need some code in the def of anon is empty -> insert path
128+
res += [self.generate_anonymous_union() or " pass"]
128129
res += [self.generate_typedef_ctypes()]
129130

130131
if self.pack:

docs/source/winfuncs_generated.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ Functions
696696

697697
.. function:: FreeLibrary(hLibModule)
698698

699+
.. function:: RtlGetCurrentPeb()
700+
699701
.. function:: RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData)
700702

701703
.. function:: RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData)

docs/source/winstructs_generated.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11066,6 +11066,10 @@ _EXCEPTION_REGISTRATION_RECORD
1106611066

1106711067
_NT_TIB
1106811068
'''''''
11069+
.. class:: NT_TIB
11070+
11071+
Alias for :class:`_NT_TIB`
11072+
1106911073
.. class:: _NT_TIB
1107011074

1107111075
.. attribute:: ExceptionList
@@ -11088,14 +11092,9 @@ _NT_TIB
1108811092
:class:`PVOID`
1108911093

1109011094

11091-
.. attribute:: FiberData
11092-
11093-
:class:`PVOID`
11094-
11095-
11096-
.. attribute:: Version
11095+
.. attribute:: anon_01
1109711096

11098-
:class:`ULONG`
11097+
:class:`_ANON__NT_TIB_SUB_UNION_1`
1109911098

1110011099

1110111100
.. attribute:: ArbitraryUserPointer

tests/test_generated_def.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ def assert_struct_offset(struct, field, offset):
1212
if windows.current_process.bitness == 32:
1313
PEB32 = windows.generated_def.PEB
1414
PEB64 = rctypes.transform_type_to_remote64bits(windows.generated_def.PEB)
15+
16+
TEB32 = windows.generated_def.TEB
17+
TEB64 = rctypes.transform_type_to_remote64bits(windows.generated_def.TEB)
18+
19+
NT_TIB32 = windows.generated_def.NT_TIB
20+
NT_TIB64 = rctypes.transform_type_to_remote64bits(windows.generated_def.NT_TIB)
21+
1522
SYSTEM_PROCESS_INFORMATION32 = windows.generated_def.SYSTEM_PROCESS_INFORMATION
1623
SYSTEM_PROCESS_INFORMATION64 = rctypes.transform_type_to_remote64bits(windows.generated_def.SYSTEM_PROCESS_INFORMATION)
1724
else:
1825
PEB32 = rctypes.transform_type_to_remote32bits(windows.generated_def.PEB)
1926
PEB64 = windows.generated_def.PEB
27+
28+
TEB32 = rctypes.transform_type_to_remote32bits(windows.generated_def.TEB)
29+
TEB64 = windows.generated_def.TEB
30+
31+
NT_TIB32 = rctypes.transform_type_to_remote32bits(windows.generated_def.NT_TIB)
32+
NT_TIB64 = windows.generated_def.NT_TIB
33+
2034
SYSTEM_PROCESS_INFORMATION32 = rctypes.transform_type_to_remote32bits(windows.generated_def.SYSTEM_PROCESS_INFORMATION)
2135
SYSTEM_PROCESS_INFORMATION64 = windows.generated_def.SYSTEM_PROCESS_INFORMATION
2236

@@ -53,6 +67,29 @@ def test_peb64_fields():
5367
assert_peb_offset("CSDVersion", 0x02E8)
5468
assert_peb_offset("MinimumStackCommit", 0x0318)
5569

70+
# Important to the the current TEB via Self
71+
def test_nt_tib32_fields():
72+
assert_nt_tib_offset = lambda field, offset: assert_struct_offset(NT_TIB32, field, offset)
73+
assert_nt_tib_offset("ExceptionList", 0)
74+
assert_nt_tib_offset("StackBase", 4)
75+
assert_nt_tib_offset("StackLimit", 8)
76+
assert_nt_tib_offset("SubSystemTib", 0xc)
77+
assert_nt_tib_offset("FiberData", 0x10)
78+
# assert_nt_tib_offset("Version", 0x14)
79+
assert_nt_tib_offset("ArbitraryUserPointer", 0x14)
80+
assert_nt_tib_offset("Self", 0x18) # Important !
81+
82+
def test_nt_tib64_fields():
83+
assert_nt_tib_offset = lambda field, offset: assert_struct_offset(NT_TIB64, field, offset)
84+
assert_nt_tib_offset("ExceptionList", 0)
85+
assert_nt_tib_offset("StackBase", 8)
86+
assert_nt_tib_offset("StackLimit", 0x10)
87+
assert_nt_tib_offset("SubSystemTib", 0x18)
88+
assert_nt_tib_offset("FiberData", 0x20)
89+
# assert_nt_tib_offset("Version", 0x28)
90+
assert_nt_tib_offset("ArbitraryUserPointer", 0x28)
91+
assert_nt_tib_offset("Self", 0x30) # Important !
92+
5693
def test_system_process_information32_fields():
5794
assert_spi_offset = lambda field, offset: assert_struct_offset(SYSTEM_PROCESS_INFORMATION32, field, offset)
5895
# Mainly based on https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ex/sysinfo/process.htm

0 commit comments

Comments
 (0)