Skip to content

Commit 16242d3

Browse files
authored
Merge pull request #75 from hakril/new_pebldrdata_def
Improve tests stability + new PEB_LDR_DATA definition
2 parents 217da0d + f99dc61 commit 16242d3

File tree

4 files changed

+71
-28
lines changed

4 files changed

+71
-28
lines changed

ctypes_generation/definitions/structures/winstruct.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ typedef struct _LIST_ENTRY {
44
} LIST_ENTRY, *PLIST_ENTRY, *RESTRICTED_POINTER PRLIST_ENTRY;
55

66

7+
/* Definition of WinXP : Still same base in win11 with some extra field */
78
typedef struct _PEB_LDR_DATA {
8-
BYTE Reserved1[8];
9-
PVOID Reserved2[3];
10-
LIST_ENTRY InMemoryOrderModuleList;
11-
} PEB_LDR_DATA, *PPEB_LDR_DATA;
9+
ULONG Length;
10+
BYTE Initialized;
11+
PVOID SsHandle;
12+
_LIST_ENTRY InLoadOrderModuleList;
13+
_LIST_ENTRY InMemoryOrderModuleList;
14+
_LIST_ENTRY InInitializationOrderModuleList;
15+
PVOID EntryInProgress;
16+
// BYTE ShutdownInProgress; // New field
17+
// PVOID ShutdownThreadId; // New field
18+
}PEB_LDR_DATA, *PPEB_LDR_DATA;
1219

1320

1421
typedef struct _LSA_UNICODE_STRING {

docs/source/winstructs_generated.rst

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10521,19 +10521,39 @@ _PEB_LDR_DATA
1052110521

1052210522
.. class:: _PEB_LDR_DATA
1052310523

10524-
.. attribute:: Reserved1
10524+
.. attribute:: Length
1052510525

10526-
:class:`BYTE` ``[8]``
10526+
:class:`ULONG`
1052710527

1052810528

10529-
.. attribute:: Reserved2
10529+
.. attribute:: Initialized
1053010530

10531-
:class:`PVOID` ``[3]``
10531+
:class:`BYTE`
10532+
10533+
10534+
.. attribute:: SsHandle
10535+
10536+
:class:`PVOID`
10537+
10538+
10539+
.. attribute:: InLoadOrderModuleList
10540+
10541+
:class:`_LIST_ENTRY`
1053210542

1053310543

1053410544
.. attribute:: InMemoryOrderModuleList
1053510545

10536-
:class:`LIST_ENTRY`
10546+
:class:`_LIST_ENTRY`
10547+
10548+
10549+
.. attribute:: InInitializationOrderModuleList
10550+
10551+
:class:`_LIST_ENTRY`
10552+
10553+
10554+
.. attribute:: EntryInProgress
10555+
10556+
:class:`PVOID`
1053710557

1053810558
_LSA_UNICODE_STRING
1053910559
'''''''''''''''''''

tests/test_debugger.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -376,22 +376,28 @@ def trigger(self, dbg, exc):
376376
@pytest.mark.parametrize("bptype", [windows.debug.FunctionParamDumpHXBP, windows.debug.FunctionParamDumpBP])
377377
def test_standard_breakpoint_self_remove(proc32_64_debug, bptype):
378378
data = set()
379-
379+
thread_exception = []
380380
def do_check():
381381
time.sleep(1)
382-
print("[==================] LOADING PYTHON")
383-
proc32_64_debug.execute_python_unsafe("1").wait()
384-
print("[==================] OPEN SELF_FILENAME1")
385-
proc32_64_debug.execute_python_unsafe("open(u'SELF_FILENAME1')").wait()
386-
time.sleep(0.1)
387-
print("[==================] OPEN SELF_FILENAME2")
388-
proc32_64_debug.execute_python_unsafe("open(u'SELF_FILENAME2')").wait()
389-
time.sleep(0.1)
390-
print("[==================] OPEN SELF_FILENAME3")
391-
proc32_64_debug.execute_python_unsafe("open(u'SELF_FILENAME3')").wait()
392-
time.sleep(0.1)
393-
print("[==================] KILLING TARGET")
394-
proc32_64_debug.exit()
382+
try:
383+
assert proc32_64_debug.peb.Ldr.contents.Initialized, "peb.Ldr not yet Initialized"
384+
print("[==================] LOADING PYTHON")
385+
proc32_64_debug.execute_python_unsafe("1").wait()
386+
print("[==================] OPEN SELF_FILENAME1")
387+
proc32_64_debug.execute_python_unsafe("open(u'SELF_FILENAME1')").wait()
388+
time.sleep(0.1)
389+
print("[==================] OPEN SELF_FILENAME2")
390+
proc32_64_debug.execute_python_unsafe("open(u'SELF_FILENAME2')").wait()
391+
time.sleep(0.1)
392+
print("[==================] OPEN SELF_FILENAME3")
393+
proc32_64_debug.execute_python_unsafe("open(u'SELF_FILENAME3')").wait()
394+
time.sleep(0.1)
395+
print("[==================] KILLING TARGET")
396+
except Exception as e:
397+
traceback.print_exc()
398+
thread_exception.append(e)
399+
finally:
400+
proc32_64_debug.exit()
395401

396402
class TSTBP(bptype):
397403
TARGET = windows.winproxy.CreateFileW
@@ -407,8 +413,13 @@ def trigger(self, dbg, exc):
407413

408414
d = windows.debug.Debugger(proc32_64_debug)
409415
d.add_bp(TSTBP("kernelbase!CreateFileW"))
410-
threading.Thread(target=do_check).start()
416+
t = threading.Thread(target=do_check)
417+
t.start()
411418
d.loop()
419+
assert not t.is_alive()
420+
if thread_exception:
421+
raise thread_exception[0]
422+
412423
assert data >= set([u"SELF_FILENAME1", u"SELF_FILENAME2"])
413424
assert u"SELF_FILENAME3" not in data
414425

@@ -429,8 +440,9 @@ def test_standard_breakpoint_remove(proc32_64_debug, bptype):
429440
data = set()
430441
thread_exception = []
431442
def do_check():
432-
time.sleep(1)
443+
time.sleep(2)
433444
try:
445+
assert proc32_64_debug.peb.Ldr.contents.Initialized, "peb.Ldr not yet Initialized"
434446
print("[==================] LOADING PYTHON")
435447
assert list(d.breakpoints.values())[0]
436448
proc32_64_debug.execute_python_unsafe("1").wait()

windows/generated_def/winstructs.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5438,9 +5438,13 @@ class _LIST_ENTRY(Structure): pass
54385438

54395439
class _PEB_LDR_DATA(Structure):
54405440
_fields_ = [
5441-
("Reserved1", BYTE * (8)),
5442-
("Reserved2", PVOID * (3)),
5443-
("InMemoryOrderModuleList", LIST_ENTRY),
5441+
("Length", ULONG),
5442+
("Initialized", BYTE),
5443+
("SsHandle", PVOID),
5444+
("InLoadOrderModuleList", _LIST_ENTRY),
5445+
("InMemoryOrderModuleList", _LIST_ENTRY),
5446+
("InInitializationOrderModuleList", _LIST_ENTRY),
5447+
("EntryInProgress", PVOID),
54445448
]
54455449
PEB_LDR_DATA = _PEB_LDR_DATA
54465450
PPEB_LDR_DATA = POINTER(_PEB_LDR_DATA)

0 commit comments

Comments
 (0)