Skip to content

Instantly share code, notes, and snippets.

@ricardojoserf
Created January 15, 2025 08:25
Show Gist options
  • Select an option

  • Save ricardojoserf/8dc5d44f210fd541b28e50b4f53fecf7 to your computer and use it in GitHub Desktop.

Select an option

Save ricardojoserf/8dc5d44f210fd541b28e50b4f53fecf7 to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <winternl.h>
#include <stdio.h>
// Declaración de ZwOpenFile
typedef NTSTATUS(NTAPI* ZwOpenFile_t)(
PHANDLE FileHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes,
PIO_STATUS_BLOCK IoStatusBlock,
ULONG ShareAccess,
ULONG OpenOptions
);
typedef NTSTATUS(NTAPI* NtReadFile_t)(
HANDLE FileHandle,
HANDLE Event,
PIO_APC_ROUTINE ApcRoutine,
PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
PVOID Buffer,
ULONG Length,
PLARGE_INTEGER ByteOffset,
PULONG Key
);
int main() {
// Cargar ZwOpenFile desde ntdll.dll
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
if (!ntdll) {
printf("Error al cargar ntdll.dll\n");
return 1;
}
ZwOpenFile_t ZwOpenFile = (ZwOpenFile_t)GetProcAddress(ntdll, "ZwOpenFile");
if (!ZwOpenFile) {
printf("Error al obtener la función ZwOpenFile\n");
return 1;
}
NtReadFile_t NtReadFile = (NtReadFile_t)GetProcAddress(ntdll, "NtReadFile");
if (!NtReadFile) {
printf("Error al obtener la función NtReadFile\n");
return 1;
}
// Ruta del archivo (UNICODE_STRING)
UNICODE_STRING filePath;
WCHAR filePathBuffer[] = L"\\??\\C:\\windows\\system32\\wdigest.dll";
filePath.Length = (USHORT)(wcslen(filePathBuffer) * sizeof(WCHAR));
filePath.MaximumLength = sizeof(filePathBuffer);
filePath.Buffer = filePathBuffer;
// Inicializar OBJECT_ATTRIBUTES
OBJECT_ATTRIBUTES objectAttributes;
InitializeObjectAttributes(&objectAttributes, &filePath, OBJ_CASE_INSENSITIVE, NULL, NULL);
// Estructura para recibir el estado de E/S
IO_STATUS_BLOCK ioStatusBlock;
// Abrir el archivo
HANDLE fileHandle;
NTSTATUS status = ZwOpenFile(
&fileHandle,
GENERIC_READ,
&objectAttributes,
&ioStatusBlock,
FILE_SHARE_READ,
FILE_NON_DIRECTORY_FILE
);
if (!NT_SUCCESS(status)) {
printf("Error al abrir el archivo con ZwOpenFile (0x%08X)\n", status);
return 1;
}
// Leer bytes del archivo con NtReadFile
char buffer[256];
ZeroMemory(buffer, sizeof(buffer));
LARGE_INTEGER byteOffset;
byteOffset.QuadPart = 0; // Comenzar desde el inicio del archivo
status = NtReadFile(
fileHandle,
NULL,
NULL,
NULL,
&ioStatusBlock,
buffer,
sizeof(buffer),
&byteOffset, // Offset explícito
NULL
);
if (!NT_SUCCESS(status)) {
printf("Error al leer el archivo con NtReadFile (0x%08X)\n", status);
CloseHandle(fileHandle);
return 1;
}
// Mostrar los bytes leídos
printf("Bytes leídos:\n");
for (ULONG i = 0; i < ioStatusBlock.Information; i++) {
printf("%02X ", (unsigned char)buffer[i]);
}
printf("\n");
// Cerrar el archivo
CloseHandle(fileHandle);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment