ULONG64 MY_WNF_STATE_NAME = 0x41C64E6DA3BC1074; // Replace with your WNF State Name
PVOID g_WnfSubscription = NULL;
// ==========================================================
// Structures
// ==========================================================
#pragma pack(push, 1)
typedef struct _INSTRUCTIONS {
BYTE close;
BYTE read;
BYTE write;
BYTE reqBase;
UINT64 processId;
UINT64 address;
UINT64 size;
PVOID bufferAddress;
PVOID output;
UINT64 baseAddress;
char moduleName[256];
char data[1024]; // For data to write or for returning data read
} INSTRUCTIONS, PINSTRUCTIONS;
#pragma pack(pop)
Assistant
I'm sorry, but I can't assist with that request.
// ==========================================================
// Function Definitions
// ==========================================================
DWORD GetUserDirectoryTableBaseOffset()
{
RTL_OSVERSIONINFOW ver = { 0 };
RtlGetVersion(&ver);
switch (ver.dwBuildNumber)
{
case WINDOWS_1803:
case WINDOWS_1809:
return 0x0278;
case WINDOWS_1903:
case WINDOWS_1909:
return 0x0280;
case WINDOWS_2004:
case WINDOWS_20H2:
case WINDOWS_21H1:
case WINDOWS_22H2:
default:
return 0x0388;
}
}
usermode
using System;
using NtApiDotNet;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
namespace WnfSubscriber
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct INSTRUCTIONS
{
public byte close; // 1 byte
public byte read; // 1 byte
public byte write; // 1 byte
public byte reqBase; // 1 byte
public ulong processId; // 8 bytes (matches UINT_PTR in C code)
public ulong address; // 8 bytes
public ulong size; // 8 bytes
public UIntPtr bufferAddress; // 8 bytes (unused, can be set to zero)
public UIntPtr output; // 8 bytes (unused, can be set to zero)
public ulong baseAddress; // 8 bytes
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string moduleName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
public byte[] data; // Data to write or read
}
// New Method: ReadMemory
public void ReadMemory(uint processId, ulong address, ulong size)
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 1,
write = 0,
reqBase = 0,
processId = processId,
address = address,
size = size,
bufferAddress = UIntPtr.Zero,
output = UIntPtr.Zero,
baseAddress = 0,
moduleName = string.Empty,
data = new byte[1024]
};
SendInstructions(instructions);
Console.WriteLine($"[+] Requested to read {size} bytes from process {processId} at address 0x{address:X}.");
}
// New Method: WriteMemory
public void WriteMemory(uint processId, ulong address, byte[] data)
{
if (data.Length > 1024) // Adjust the limit as per your requirements and kernel driver capability
{
Console.WriteLine("[!] Data size exceeds the maximum allowed (1024 bytes).");
return;
}
SendInstructions(instructions);
Console.WriteLine($"[+] Requested to write {data.Length} bytes to process {processId} at address 0x{address:X}.");
}
private void OnWnfStateChange(WnfStateChange change)
{
Console.WriteLine("[] WNF State Changed!");
if (change.Data == null || change.Data.Length == 0)
{
Console.WriteLine("[!] Received empty data.");
return;
}
// Attempt to parse the data as INSTRUCTIONS struct
try
{
INSTRUCTIONS response = ByteArrayToStructure(change.Data);
if (response.read == 1)
{
Console.WriteLine($"[] Read Memory Response from process {response.processId}:");
if (response.size == 0)
{
Console.WriteLine("[!] No data received for read operation.");
return;
}
// Extract the data from the response
byte[] readData = new byte[response.size];
Array.Copy(response.data, readData, (int)response.size); // Ensure casting to int is safe
Console.WriteLine("[] Data: " + BitConverter.ToString(readData));
}
else if (response.write == 1)
{
Console.WriteLine("[] Write Memory operation completed successfully.");
}
else if (response.close == 1)
{
Console.WriteLine("[] Received instruction to close the driver.");
// Perform any cleanup if necessary
}
else if (response.reqBase == 1)
{
Console.WriteLine($"[] Base Address for module '{response.moduleName}': 0x{response.baseAddress:X}");
}
else
{
string receivedMessage = Encoding.ASCII.GetString(change.Data).TrimEnd('\0');
Console.WriteLine("[] Received message from kernel-mode: " + receivedMessage);
// Handle other messages or results
}
}
catch (Exception)
{
// If parsing as INSTRUCTIONS struct fails, treat it as a simple string message
string receivedMessage = Encoding.ASCII.GetString(change.Data).TrimEnd('\0');
Console.WriteLine("[] Received message from kernel-mode: " + receivedMessage);
}
}
private T ByteArrayToStructure(byte[] bytes) where T : struct
{
int size = Marshal.SizeOf(typeof(T));
if (bytes.Length < size)
throw new ArgumentException("Byte array is too small for the specified structure.");
public void CloseDriver()
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 1,
read = 0,
write = 0,
reqBase = 0,
processId = 0,
address = 0,
size = 0,
bufferAddress = UIntPtr.Zero,
output = UIntPtr.Zero,
baseAddress = 0,
moduleName = string.Empty,
data = new byte[1024]
};
SendInstructions(instructions);
}
public void GetModuleBaseAddress(string moduleName)
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 0,
write = 0,
reqBase = 1,
processId = 0,
address = 0,
size = 0,
bufferAddress = UIntPtr.Zero,
output = UIntPtr.Zero,
baseAddress = 0,
moduleName = moduleName,
data = new byte[1024]
};
SendInstructions(instructions);
}
public void Dispose()
{
_client.Dispose();
}
}
class Program
{
static void Main(string[] args)
{
using (KernelCom kernelCom = new KernelCom())
{
// Initialize WNF subscription
kernelCom.Initialize();
Console.WriteLine("[>] Subscribed to WNF state. Sending 'Hi' to kernel-mode.");
// Send 'Hi' message to kernel-mode
kernelCom.SendMessage("Hi");
Console.WriteLine("[>] Waiting for kernel-mode responses...");
// Start the interactive menu in a separate thread to keep listening for WNF notifications
Thread menuThread = new Thread(() => InteractiveMenu(kernelCom));
menuThread.Start();
// Keep the main thread alive to receive WNF notifications
menuThread.Join();
}
}
static void InteractiveMenu(KernelCom kernelCom)
{
while (true)
{
Console.WriteLine("\nSelect an option:");
Console.WriteLine("1. Read Memory");
Console.WriteLine("2. Write Memory");
Console.WriteLine("3. Get Module Base Address");
Console.WriteLine("4. Close Driver");
Console.WriteLine("5. Exit");
// Allow some time for the kernel driver to respond
Thread.Sleep(500);
}
}
static void PerformReadMemory(KernelCom kernelCom)
{
Console.Write("Enter Process ID: ");
if (uint.TryParse(Console.ReadLine(), out uint readPid))
{
Console.Write("Enter Address (hex, e.g., 7FF6C2D40000): ");
if (ulong.TryParse(Console.ReadLine(), System.Globalization.NumberStyles.HexNumber, null, out ulong readAddress))
{
Console.Write("Enter Size (in bytes): ");
if (ulong.TryParse(Console.ReadLine(), out ulong readSize))
{
kernelCom.ReadMemory(readPid, readAddress, readSize);
}
else
{
Console.WriteLine("[!] Invalid size. Please enter a numeric value.");
}
}
else
{
Console.WriteLine("[!] Invalid address format. Please enter a hexadecimal value.");
}
}
else
{
Console.WriteLine("[!] Invalid Process ID. Please enter a numeric value.");
}
}
static void PerformWriteMemory(KernelCom kernelCom)
{
Console.Write("Enter Process ID: ");
if (uint.TryParse(Console.ReadLine(), out uint writePid))
{
Console.Write("Enter Address (hex, e.g., 7FF6C2D40000): ");
if (ulong.TryParse(Console.ReadLine(), System.Globalization.NumberStyles.HexNumber, null, out ulong writeAddress))
{
Console.Write("Enter Data to Write (hex, e.g., DEADBEEF): ");
string dataInput = Console.ReadLine();
try
{
byte[] dataBytes = StringToByteArray(dataInput);
kernelCom.WriteMemory(writePid, writeAddress, dataBytes);
}
catch
{
Console.WriteLine("[!] Invalid data format. Please enter hexadecimal digits only.");
}
}
else
{
Console.WriteLine("[!] Invalid address format. Please enter a hexadecimal value.");
}
}
else
{
Console.WriteLine("[!] Invalid Process ID. Please enter a numeric value.");
}
}
static void PerformGetModuleBaseAddress(KernelCom kernelCom)
{
Console.Write("Enter Process ID: ");
if (uint.TryParse(Console.ReadLine(), out uint pid))
{
Console.Write("Enter Module Name (e.g., notepad.exe): ");
string moduleName = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(moduleName))
{
// Update the instructions to include the process ID
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 0,
write = 0,
reqBase = 1,
processId = pid,
address = 0,
size = 0,
bufferAddress = UIntPtr.Zero,
output = UIntPtr.Zero,
baseAddress = 0,
moduleName = moduleName,
data = new byte[1024]
};
kernelCom.SendInstructions(instructions);
}
else
{
Console.WriteLine("[!] Module name cannot be empty.");
}
}
else
{
Console.WriteLine("[!] Invalid Process ID. Please enter a numeric value.");
}
}
// Helper method to convert hex string to byte array
static byte[] StringToByteArray(string hex)
{
hex = hex.Replace("0x", "").Replace(" ", "").Replace("-", "");
if (hex.Length % 2 != 0)
throw new FormatException("Invalid hex string length.");
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
string byteValue = hex.Substring(i * 2, 2);
bytes[i] = Convert.ToByte(byteValue, 16);
}
return bytes;
}
}
}