-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
59 lines (54 loc) · 2.83 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Runtime.InteropServices;
using static CS_Unhook.Imports;
namespace CS_Unhook
{
class Program
{
static void Unhook()
{
IntPtr currentProcessHandle = GetCurrentProcess();
MODULEINFO modInfo = new MODULEINFO();
IntPtr dllHandle = GetModuleHandle("ntdll.dll");
GetModuleInformation(currentProcessHandle, dllHandle, out modInfo, (uint)Marshal.SizeOf(modInfo));
IntPtr dllBase = modInfo.lpBaseOfDll;
string ntdll = "C:\\Windows\\System32\\ntdll.dll";
IntPtr ntdllHandle = CreateFileA(ntdll, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
IntPtr ntdllMapping = CreateFileMapping(ntdllHandle, IntPtr.Zero, PageProtection.Readonly | PageProtection.SectionImage, 0, 0, null);
IntPtr ntdllMmapped = MapViewOfFile(ntdllMapping, FileMapAccessType.Read, 0, 0, IntPtr.Zero);
IMAGE_DOS_HEADER dosHeader = (IMAGE_DOS_HEADER)Marshal.PtrToStructure(dllBase, typeof(IMAGE_DOS_HEADER));
IntPtr ptrtoNTHeader = (dllBase + dosHeader.e_lfanew);
IMAGE_NT_HEADERS64 ntHeader = (IMAGE_NT_HEADERS64)Marshal.PtrToStructure(ptrtoNTHeader, typeof(IMAGE_NT_HEADERS64));
try
{
Console.WriteLine("[+] About to start Unhooking process...");
for (int i = 0; i < ntHeader.FileHeader.NumberOfSections; i++)
{
IntPtr ptrtoSectionHeader = (ptrtoNTHeader + Marshal.SizeOf(typeof(IMAGE_NT_HEADERS64)));
IMAGE_SECTION_HEADER sectionHeader = (IMAGE_SECTION_HEADER)Marshal.PtrToStructure((ptrtoSectionHeader + (i * Marshal.SizeOf(typeof(IMAGE_SECTION_HEADER)))), typeof(IMAGE_SECTION_HEADER));
string sectionName = new string(sectionHeader.Name);
if (sectionName.Contains(".text"))
{
uint oldProtect = 0;
IntPtr oldAddress = IntPtr.Add(dllBase, (int)sectionHeader.VirtualAddress);
IntPtr newAddress = IntPtr.Add(ntdllMmapped, (int)sectionHeader.VirtualAddress);
int vProtect = VirtualProtect(oldAddress, sectionHeader.VirtualSize, 0x40, out oldProtect);
memcpy(oldAddress, newAddress, sectionHeader.VirtualSize);
vProtect = VirtualProtect(oldAddress, sectionHeader.VirtualSize, oldProtect, out oldProtect);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
Console.WriteLine("[*] Unhooking completed. Press any key to exit...");
Console.ReadKey();
}
static void Main(string[] args)
{
Unhook();
}
}
}