if (change.Data == null || change.Data.Length == 0)
{
Console.WriteLine("[!] Received empty data.");
return;
}
string receivedMessage = Encoding.ASCII.GetString(change.Data);
Console.WriteLine("[] Received message from kernel-mode: " + receivedMessage);
if (receivedMessage == "Hi")
{
Console.WriteLine("[] Received 'Hi' from kernel-mode. Sending instructions...");
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 1,
reqBase = 0,
bufferAddress = IntPtr.Zero,
address = UIntPtr.Zero,
size = 0,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = "ExampleModule"
};
SendInstructions(instructions);
}
else if (receivedMessage == "Read operation completed")
{
Console.WriteLine("[] Kernel-mode has completed the read operation.");
// Process the results if any
// ...
}
else
{
// Handle other messages or results
Console.WriteLine("[] Received data: " + receivedMessage);
}
}
public void CloseDriver()
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 1,
read = 0,
reqBase = 0,
bufferAddress = IntPtr.Zero,
address = UIntPtr.Zero,
size = 0,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = string.Empty
};
SendInstructions(instructions);
}
public void GetModuleBaseAddress(string moduleName)
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 0,
reqBase = 1,
bufferAddress = IntPtr.Zero,
address = UIntPtr.Zero,
size = 0,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = moduleName
};
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...");
// Keep the application running to receive WNF notifications
while (true)
{
Thread.Sleep(1000);
}
// Example of sending additional instructions
// kernelCom.GetModuleBaseAddress("Notepad.exe");
// kernelCom.CloseDriver();
}
}
}
public class KernelCom : IDisposable
{
private const ulong MY_WNF_STATE_NAME = 0x41C64E6DA3BC1074;
private readonly WnfClient _client;
private bool _initialized = false;
public KernelCom()
{
_client = new WnfClient();
}
public void Initialize()
{
if (_initialized)
return;
_client.SubscribeState(new WnfStateName(MY_WNF_STATE_NAME), OnWnfStateChange);
Console.WriteLine("[+] Subscribed to WNF state.");
_initialized = true;
}
public void SendMessage(string message)
{
byte[] dataToSend = Encoding.ASCII.GetBytes(message);
NtWnf.PublishStateData(new WnfStateName(MY_WNF_STATE_NAME), dataToSend);
Console.WriteLine("[+] Sent message to kernel-mode: " + message);
}
public void SendInstructions(INSTRUCTIONS instructions)
{
int size = Marshal.SizeOf(instructions);
byte[] dataToSend = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(instructions, ptr, false);
Marshal.Copy(ptr, dataToSend, 0, size);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
NtWnf.PublishStateData(new WnfStateName(MY_WNF_STATE_NAME), dataToSend);
Console.WriteLine("[+] Sent instructions to kernel-mode.");
}
// 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 = IntPtr.Zero,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = string.Empty,
data = string.Empty
};
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)
{
Console.WriteLine("[!] Data size exceeds the maximum allowed (1024 bytes).");
return;
}
string dataString = Convert.ToBase64String(data); // Encode data as Base64 string
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 0,
write = 1,
reqBase = 0,
processId = processId,
address = address,
size = (ulong)data.Length,
bufferAddress = IntPtr.Zero,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = string.Empty,
data = dataString
};
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;
}
// Check if the data corresponds to a read response
INSTRUCTIONS response = ByteArrayToStructure<INSTRUCTIONS>(change.Data);
if (response.read == 1 && response.data != null)
{
Console.WriteLine($"[] Read Memory Response from process {response.processId}:");
// Decode the Base64 data received
byte[] readData = Convert.FromBase64String(response.data);
Console.WriteLine("[] Data: " + BitConverter.ToString(readData));
}
else if (response.write == 1)
{
Console.WriteLine("[] Write Memory operation completed successfully.");
}
else
{
string receivedMessage = Encoding.ASCII.GetString(change.Data);
Console.WriteLine("[] Received message from kernel-mode: " + receivedMessage);
if (receivedMessage == "Hi")
{
Console.WriteLine("[] Received 'Hi' from kernel-mode. Sending instructions...");
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 1,
reqBase = 0,
bufferAddress = IntPtr.Zero,
address = UIntPtr.Zero,
size = 0,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = "ExampleModule"
};
SendInstructions(instructions);
}
else if (receivedMessage == "Read operation completed")
{
Console.WriteLine("[] Kernel-mode has completed the read operation.");
// Process the results if any
// ...
}
else
{
// Handle other messages or results
Console.WriteLine("[] Received data: " + receivedMessage);
}
}
}
private T ByteArrayToStructure<T>(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.");
IntPtr ptr = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 0, ptr, size);
return Marshal.PtrToStructure<T>(ptr);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
}
public void CloseDriver()
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 1,
read = 0,
write = 0,
reqBase = 0,
bufferAddress = IntPtr.Zero,
address = UIntPtr.Zero,
size = 0,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = string.Empty,
data = string.Empty
};
SendInstructions(instructions);
}
public void GetModuleBaseAddress(string moduleName)
{
INSTRUCTIONS instructions = new INSTRUCTIONS
{
close = 0,
read = 0,
write = 0,
reqBase = 1,
bufferAddress = IntPtr.Zero,
address = UIntPtr.Zero,
size = 0,
output = IntPtr.Zero,
baseAddress = 0,
moduleName = moduleName,
data = string.Empty
};
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...");
// Example: Wait for user input to perform read/write operations
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");
Console.Write("Choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
Console.Write("Enter Process ID: ");
if (uint.TryParse(Console.ReadLine(), out uint readPid))
{
Console.Write("Enter Address (hex): ");
if (ulong.TryParse(Console.ReadLine(), System.Globalization.NumberStyles.HexNumber, null, out ulong readAddress))
{
Console.Write("Enter Size: ");
if (ulong.TryParse(Console.ReadLine(), out ulong readSize))
{
kernelCom.ReadMemory(readPid, readAddress, readSize);
}
else
{
Console.WriteLine("[!] Invalid size.");
}
}
else
{
Console.WriteLine("[!] Invalid address.");
}
}
else
{
Console.WriteLine("[!] Invalid Process ID.");
}
break;
case "2":
Console.Write("Enter Process ID: ");
if (uint.TryParse(Console.ReadLine(), out uint writePid))
{
Console.Write("Enter Address (hex): ");
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.");
}
}
else
{
Console.WriteLine("[!] Invalid address.");
}
}
else
{
Console.WriteLine("[!] Invalid Process ID.");
}
break;
case "3":
Console.Write("Enter Module Name (e.g., notepad.exe): ");
string moduleName = Console.ReadLine();
kernelCom.GetModuleBaseAddress(moduleName);
break;
case "4":
kernelCom.CloseDriver();
Console.WriteLine("[>] Driver closed.");
break;
case "5":
return;
default:
Console.WriteLine("[!] Invalid choice.");
break;
}
// Allow some time for the kernel driver to respond
Thread.Sleep(1000);
}
}
}
// Helper method to convert hex string to byte array
static byte[] StringToByteArray(string hex)
{
hex = hex.Replace("0x", "").Replace(" ", "");
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
}