NtQuerySection
Récupère des métadonnées de base ou spécifiques à l'image d'un section object.
Prototype
NTSTATUS NtQuerySection( HANDLE SectionHandle, SECTION_INFORMATION_CLASS SectionInformationClass, PVOID SectionInformation, SIZE_T SectionInformationLength, PSIZE_T ResultLength );
Arguments
| Name | Type | Dir | Description |
|---|---|---|---|
| SectionHandle | HANDLE | in | Handle vers le section object, requérant l'accès SECTION_QUERY. |
| SectionInformationClass | SECTION_INFORMATION_CLASS | in | Enum de classe d'info : SectionBasicInformation = 0 (BaseAddress, AllocationAttributes, MaximumSize), SectionImageInformation = 1 (IMAGE_INFORMATION complet : EntryPoint, StackReserve/Commit, ImageType, Machine, CheckSum, ...), SectionRelocationInformation = 2, SectionOriginalBaseInformation = 3. |
| SectionInformation | PVOID | out | Tampon fourni par l'appelant recevant la structure SECTION_BASIC_INFORMATION ou SECTION_IMAGE_INFORMATION. |
| SectionInformationLength | SIZE_T | in | Taille en octets du tampon SectionInformation. |
| ResultLength | PSIZE_T | out | Optionnel ; reçoit la taille réellement écrite ou requise. |
IDs de syscalls par version de Windows
| Version de Windows | ID de syscall | Build |
|---|---|---|
| Win10 1507 | 0x51 | win10-1507 |
| Win10 1607 | 0x51 | win10-1607 |
| Win10 1703 | 0x51 | win10-1703 |
| Win10 1709 | 0x51 | win10-1709 |
| Win10 1803 | 0x51 | win10-1803 |
| Win10 1809 | 0x51 | win10-1809 |
| Win10 1903 | 0x51 | win10-1903 |
| Win10 1909 | 0x51 | win10-1909 |
| Win10 2004 | 0x51 | win10-2004 |
| Win10 20H2 | 0x51 | win10-20h2 |
| Win10 21H1 | 0x51 | win10-21h1 |
| Win10 21H2 | 0x51 | win10-21h2 |
| Win10 22H2 | 0x51 | win10-22h2 |
| Win11 21H2 | 0x51 | win11-21h2 |
| Win11 22H2 | 0x51 | win11-22h2 |
| Win11 23H2 | 0x51 | win11-23h2 |
| Win11 24H2 | 0x51 | win11-24h2 |
| Server 2016 | 0x51 | winserver-2016 |
| Server 2019 | 0x51 | winserver-2019 |
| Server 2022 | 0x51 | winserver-2022 |
| Server 2025 | 0x51 | winserver-2025 |
Module noyau
APIs liées
Stub du syscall
4C 8B D1 mov r10, rcx B8 51 00 00 00 mov eax, 0x51 F6 04 25 08 03 FE 7F 01 test byte ptr [0x7FFE0308], 1 75 03 jne short +3 0F 05 syscall C3 ret CD 2E int 2Eh C3 ret
Notes non documentées
SSN `0x51` sur tous les builds Windows 10 / 11 / Server — parfaitement stable, ce qui en fait une cible SSN-codée-en-dur sûre. Il n'y a pas de wrapper Win32 public ; les consommateurs passent par le nom Nt ou par le `ZwQuerySection` non documenté. `SectionBasicInformation` retourne un `SECTION_BASIC_INFORMATION { PVOID BaseAddress; ULONG AllocationAttributes; LARGE_INTEGER MaximumSize; }` — les AllocationAttributes portent les flags SEC_IMAGE / SEC_COMMIT / SEC_RESERVE / SEC_NOCACHE / SEC_LARGE_PAGES. `SectionImageInformation` retourne un `SECTION_IMAGE_INFORMATION` bien plus grand reflétant le PE OPTIONAL_HEADER (entry point, stack/heap reserve/commit, sous-système, machine, checksum, caractéristiques image) — équivalent à ce que `LdrQueryImageFileExecutionOptions` apprend du disque plus les décisions de résolution du loader.
Usage courant par les malwares
La primitive d'empreinte dans les loaders d'**unhooking**. Un flux typique : ouvrir le `ntdll.dll` sur disque (`NtCreateFile`), l'envelopper comme section SEC_IMAGE (`NtCreateSection`), appeler `NtQuerySection(SectionImageInformation)` pour apprendre les `BaseOfCode` / `SizeOfCode` / `EntryPoint` propres de l'image disque, mapper la section read-only (`NtMapViewOfSection`), puis comparer octet à octet le `.text` propre contre le `.text` du ntdll hooké en mémoire pour identifier les hooks inline installés par l'EDR, et enfin écraser les octets hookés depuis la copie propre. Des variantes font la même chose contre `kernel32.dll`, `kernelbase.dll` et `win32u.dll`. Les outils utilisant ce motif incluent Perun's Fart, RefleXXion, Hell's Hall, le plugin unhooker Adwind/jRAT, plusieurs UDRL Cobalt Strike, et les PoC publics fournis avec la plupart des cours red-team. Usage secondaire : évasion forensique — mettre à zéro les champs SECTION_IMAGE_INFORMATION.GpValue / CheckSum d'un PE injecté avant un mappage ultérieur pour défaire les détections d'anomalie PE naïves.
Opportunités de détection
Pris isolément, NtQuerySection est ordinaire — le loader Windows et le runtime CLR l'appellent constamment pendant le chargement de DLL. La détection doit être contextuelle. La signature d'unhooker : un processus qui vient d'ouvrir `\Device\HarddiskVolumeN\Windows\System32\ntdll.dll`, l'a mappé en SEC_IMAGE, a interrogé SectionImageInformation, puis émis `NtProtectVirtualMemory` suivi de `NtWriteVirtualMemory` contre le `.text` de son propre ntdll chargé — c'est la chaîne d'unhook indubitable. Les EDR kernel-mode (WdFilter de Defender, CSAgent de CrowdStrike, le sensor noyau MsSense de MDE) détectent l'étape *NtProtect/NtWrite-vers-DLL-système* plutôt que NtQuerySection lui-même, ce qui explique que la plupart des auteurs d'unhooker s'attendent pleinement à ce que l'appel soit invisible et comptent sur le reste de la chaîne pour rester sous le radar. ETW Threat Intelligence fait remonter l'écriture `.text` sur les systèmes PG-protégés.
Exemples de syscalls directs
asmx64 direct stub
; Direct syscall stub for NtQuerySection (SSN 0x51, all builds)
NtQuerySection PROC
mov r10, rcx ; syscall convention
mov eax, 51h ; SSN
syscall
ret
NtQuerySection ENDPcUnhooker: get clean ntdll .text from disk
// Open disk ntdll, map as SEC_IMAGE, query its image info, then
// memcmp against the in-memory ntdll to spot EDR inline hooks.
#include <windows.h>
typedef struct _SECTION_IMAGE_INFORMATION {
PVOID TransferAddress;
ULONG ZeroBits;
SIZE_T MaximumStackSize;
SIZE_T CommittedStackSize;
ULONG SubSystemType;
USHORT SubSystemMinorVersion;
USHORT SubSystemMajorVersion;
ULONG GpValue;
USHORT ImageCharacteristics;
USHORT DllCharacteristics;
USHORT Machine;
BOOLEAN ImageContainsCode;
UCHAR ImageFlags;
ULONG LoaderFlags;
ULONG ImageFileSize;
ULONG CheckSum;
} SECTION_IMAGE_INFORMATION;
typedef NTSTATUS (NTAPI *pNtQuerySection)(HANDLE, ULONG, PVOID, SIZE_T, PSIZE_T);
NTSTATUS GetCleanImageInfo(HANDLE h_section, SECTION_IMAGE_INFORMATION *out) {
pNtQuerySection NtQuerySection = (pNtQuerySection)GetProcAddress(
GetModuleHandleA("ntdll.dll"), "NtQuerySection");
SIZE_T ret = 0;
return NtQuerySection(h_section, /* SectionImageInformation */ 1,
out, sizeof *out, &ret);
}cBasic info: section size and attributes
// Cheap sanity check on a section we don't own: how big, what flags?
#include <windows.h>
typedef struct _SECTION_BASIC_INFORMATION {
PVOID BaseAddress;
ULONG AllocationAttributes; // SEC_IMAGE / SEC_COMMIT / ...
LARGE_INTEGER MaximumSize;
} SECTION_BASIC_INFORMATION;
typedef NTSTATUS (NTAPI *pNtQuerySection)(HANDLE, ULONG, PVOID, SIZE_T, PSIZE_T);
LONGLONG SectionSize(HANDLE h_section) {
pNtQuerySection NtQuerySection = (pNtQuerySection)GetProcAddress(
GetModuleHandleA("ntdll.dll"), "NtQuerySection");
SECTION_BASIC_INFORMATION bi = {0};
SIZE_T ret = 0;
if (NtQuerySection(h_section, /* SectionBasicInformation */ 0,
&bi, sizeof bi, &ret) != 0) return -1;
return bi.MaximumSize.QuadPart;
}Mappings MITRE ATT&CK
Last verified: 2026-05-20