Skip to content

Commit c5cb631

Browse files
authored
KUSER_SHARED_DATA and added a proper cleanup block
Added the KUSER_SHARED_DATA support to get the BuildNumber directly which is less prone to tampering. also added a proper cleanup block instead of whatever i was doing
1 parent 65e8050 commit c5cb631

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

AntiCrack-DotNet/Syscalls.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ private static string GetWindowsBuildNumberWinAPI()
353353
return null;
354354
}
355355

356+
/// <summary>
357+
/// Gets the build number using KUSER_SHARED_dATA.
358+
/// </summary>
359+
/// <returns>The build number.</returns>
360+
private static string GetWindowsBuildNumberKUser()
361+
{
362+
byte[] BuildNumberBytes = new byte[4];
363+
IntPtr NtBuildNumber = new IntPtr(0x7FFE0260);
364+
Utils.CopyMem(BuildNumberBytes, NtBuildNumber, false);
365+
try
366+
{
367+
uint BuildNumber = BitConverter.ToUInt32(BuildNumberBytes, 0);
368+
return BuildNumber.ToString();
369+
}
370+
catch
371+
{
372+
return null;
373+
}
374+
}
375+
356376

357377
/// <summary>
358378
/// Searches for the return value from the function bytes.
@@ -365,11 +385,14 @@ private static bool IsTampered(string WinAPI, string WMI, string Registry)
365385
}
366386

367387
/// <summary>
368-
/// Searches for the return value from the function bytes.
388+
/// Gets the best source for the BuildNumber that is harder to tamper with.
369389
/// </summary>
370390
/// <returns>The most suitable build number.</returns>
371-
public static string GetMostMatching(string WinAPI, string WMI, string Registry)
391+
public static string GetMostMatching(string WinAPI, string WMI, string Registry, string KUSER)
372392
{
393+
if (!string.IsNullOrWhiteSpace(KUSER))
394+
return KUSER;
395+
373396
if (Tampered)
374397
{
375398
if (WinAPI == WMI)
@@ -409,6 +432,7 @@ public static string GetBuildNumber(bool ExitOnBuildNumberTamper, bool OnlyShowO
409432
string WinAPI = GetWindowsBuildNumberWinAPI();
410433
string WMI = GetWindowsBuildNumberWMI();
411434
string Registry = GetWindowsBuildNumberReg();
435+
string KUSER = GetWindowsBuildNumberKUser();
412436
if (IsTampered(WinAPI, WMI, Registry))
413437
{
414438
Tampered = true;
@@ -427,7 +451,7 @@ public static string GetBuildNumber(bool ExitOnBuildNumberTamper, bool OnlyShowO
427451
ForceExit();
428452
}
429453
}
430-
return GetMostMatching(WinAPI, WMI, Registry);
454+
return GetMostMatching(WinAPI, WMI, Registry, KUSER);
431455
}
432456

433457
/// <summary>
@@ -506,10 +530,9 @@ public static uint SyscallNtQueryInformationProcess(uint ProcessInfoClass, out u
506530
{
507531
SysNtQueryInformationProcess Executed = (SysNtQueryInformationProcess)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQueryInformationProcess));
508532
uint Result = Executed(new IntPtr(-1), ProcessInfoClass, out ProcessInfo, nSize, out ReturnLength);
509-
FreeCode(Syscall);
510533
return Result;
511534
}
512-
catch
535+
finally
513536
{
514537
FreeCode(Syscall);
515538
}
@@ -528,10 +551,9 @@ public static uint SyscallNtQueryInformationProcess(uint ProcessInfoClass, out I
528551
{
529552
SysNtQueryInformationProcess2 Executed = (SysNtQueryInformationProcess2)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQueryInformationProcess2));
530553
uint Result = Executed(new IntPtr(-1), ProcessInfoClass, out ProcessInfo, nSize, ReturnLength);
531-
FreeCode(Syscall);
532554
return Result;
533555
}
534-
catch
556+
finally
535557
{
536558
FreeCode(Syscall);
537559
}
@@ -550,10 +572,9 @@ public static uint SyscallNtQueryInformationProcess(uint ProcessInfoClass, ref S
550572
{
551573
SysNtQueryInformationProcess3 Executed = (SysNtQueryInformationProcess3)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQueryInformationProcess3));
552574
uint Result = Executed(new IntPtr(-1), ProcessInfoClass, ref ProcessInfo, nSize, ReturnLength);
553-
FreeCode(Syscall);
554575
return Result;
555576
}
556-
catch
577+
finally
557578
{
558579
FreeCode(Syscall);
559580
}
@@ -570,7 +591,6 @@ public static bool SyscallNtClose(IntPtr Handle)
570591
{
571592
SysNtClose Executed = (SysNtClose)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtClose));
572593
bool Result = Executed(Handle);
573-
FreeCode(Syscall);
574594
return Result;
575595
}
576596
finally
@@ -591,10 +611,9 @@ public static uint SyscallNtQuerySystemInformation(uint SystemInformationClass,
591611
{
592612
SysNtQuerySystemInformation Executed = (SysNtQuerySystemInformation)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQuerySystemInformation));
593613
uint Result = Executed(SystemInformationClass, ref SystemInformation, SystemInformationLength, out ReturnLength);
594-
FreeCode(Syscall);
595614
return Result;
596615
}
597-
catch
616+
finally
598617
{
599618
FreeCode(Syscall);
600619
}
@@ -612,10 +631,9 @@ public static uint SyscallNtQuerySystemInformation(uint SystemInformationClass,
612631
{
613632
SysNtQuerySystemInformation2 Executed = (SysNtQuerySystemInformation2)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQuerySystemInformation2));
614633
uint Result = Executed(SystemInformationClass, ref SystemInformation, SystemInformationLength, out ReturnLength);
615-
FreeCode(Syscall);
616634
return Result;
617635
}
618-
catch
636+
finally
619637
{
620638
FreeCode(Syscall);
621639
}
@@ -633,10 +651,9 @@ public static uint SyscallNtQuerySystemInformation(uint SystemInformationClass,
633651
{
634652
SysNtQuerySystemInformation3 Executed = (SysNtQuerySystemInformation3)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQuerySystemInformation3));
635653
uint Result = Executed(SystemInformationClass, ref SystemInformation, SystemInformationLength, out ReturnLength);
636-
FreeCode(Syscall);
637654
return Result;
638655
}
639-
catch
656+
finally
640657
{
641658
FreeCode(Syscall);
642659
}
@@ -654,10 +671,9 @@ public static uint SyscallNtQueryVirtualMemory(IntPtr ProcessHandle, IntPtr Base
654671
{
655672
SysNtQueryVirtualMemory Executed = (SysNtQueryVirtualMemory)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQueryVirtualMemory));
656673
uint Result = Executed(ProcessHandle, BaseAddress, MemoryInformationClass, ref MemoryInformation, MemoryInformationLength, out ReturnLength);
657-
FreeCode(Syscall);
658674
return Result;
659675
}
660-
catch
676+
finally
661677
{
662678
FreeCode(Syscall);
663679
}
@@ -674,10 +690,9 @@ public static int SyscallNtQueryInformationThread(IntPtr ThreadHandle, int Threa
674690
{
675691
SysNtQueryInformationThread Executed = (SysNtQueryInformationThread)Marshal.GetDelegateForFunctionPointer(Syscall, typeof(SysNtQueryInformationThread));
676692
int Result = Executed(ThreadHandle, ThreadInformationClass, ref ThreadInformation, ThreadInformationLength, ReturnLength);
677-
FreeCode(Syscall);
678693
return Result;
679694
}
680-
catch
695+
finally
681696
{
682697
FreeCode(Syscall);
683698
}

0 commit comments

Comments
 (0)