idiotc4t's blog
搜索文档…
GitBook 提供支持
攻击demo的bof改造

简介

银河系第一C2 cobaltstrike4.1更新了一个神奇的功能,原文直接贴。
Cobalt Strike has weaponization options for PowerShell, .NET, and Reflective DLLs. These three options rely on Beacon’s fork&run mechanism. This is Cobalt Strike’s pattern to spawn a process, inject a capability into it, and receive output over a named pipe. This is OK in some engagements. It’s too OPSEC-expensive in others.
We’ve long had requests for some option to run custom capability directly within the Beacon payload, without fork&run. Beacon Object Files are our answer to this request. Beacon Object Files are a way to build small post-ex capabilities that execute in Beacon, parse arguments, call a few Win32 APIs, report output, and exit.
A Beacon Object File is an object file, produced by a C compiler, that is linked and loaded by Cobalt Strike. Beacon Object Files can call Win32 APIs and have access to some internal Beacon APIs (e.g., for output, token impersonation, etc.).
Here’s an example Beacon Object File (it’s Beacon’s ‘net domain’ command)
其他特性我倒是没觉得有什么基本都是些常规技术,主要是小,在dns等传输速率慢的信道内会比较有优势。

流程

其实也挺简单的
    1.
    引入beacon.h头文件
    2.
    把所有字符串和函数改成ascii的
    3.
    把所有函数改成beacon.h定义的编写约定
    4.
    生成bof文件

操作&代码

找到一份需要武器化的代码,这里使用get-computer-installed-software的demo。
1
2
#include <stdio.h>
3
#include <Windows.h>
4
#include <tchar.h>
5
6
7
BOOL EnumInstalledSoft(TCHAR* subKey, TCHAR* subKeyName) {
8
9
HKEY hKey = NULL;
10
HKEY hSubKey = NULL;
11
DWORD dwIndexs = 0;
12
TCHAR keyName[MAX_PATH] = { 0 };
13
DWORD dwLength = 256;
14
TCHAR subKeyValue[MAX_PATH] = { 0 };
15
16
17
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
18
{
19
while (RegEnumKeyEx(hKey, dwIndexs, keyName, &dwLength, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
20
{
21
RegOpenKey(hKey, keyName, &hSubKey);
22
23
RegQueryValueEx(hSubKey,
24
subKeyName,
25
NULL,
26
NULL,
27
(LPBYTE)subKeyValue,
28
&dwLength);
29
30
printf("%s : %s \n", keyName, subKeyValue);
31
RegCloseKey(hSubKey);
32
hSubKey = 0;
33
++dwIndexs;
34
dwLength = 256;
35
}
36
}
37
else
38
{
39
return FALSE;
40
}
41
if (hKey != NULL)
42
{
43
RegCloseKey(hKey);
44
return TRUE;
45
}
46
}
47
48
int main()
49
{
50
51
52
EnumInstalledSoft((TCHAR*)"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",(TCHAR*)"DisplayName");
53
EnumInstalledSoft((TCHAR*)"Software\\Classes\\Installer\\Products", (TCHAR*)"ProductName");
54
system("pause");
55
56
57
return 0;
58
}
Copied!
然后我们导入beacon.h,和替换bof约定的写法,函数原型我们可以使用一个bof_helper的项目,这个项目自动化帮我们生成好bof约定的函数原型和写法,如把GetProcAddress换成KERNEL32$GetProcAddress的写法,这里直接使用工具,同时也需要把输出函数换成beacon导出的函数。
还需要把入口点main改名成go最后整个代码看上去是这样的(比较粗糙没传参)。
1
2
#include <stdio.h>
3
#include <windows.h>
4
#include "beacon.h"
5
6
DECLSPEC_IMPORT WINADVAPI LONG WINAPI ADVAPI32$RegOpenKeyExA(HKEY, LPCWSTR, DWORD, REGSAM, PHKEY);
7
8
DECLSPEC_IMPORT WINADVAPI LONG WINAPI ADVAPI32$RegOpenKeyA(HKEY, LPCWSTR, PHKEY);
9
DECLSPEC_IMPORT WINADVAPI LONG WINAPI ADVAPI32$RegCloseKey(HKEY);
10
DECLSPEC_IMPORT WINADVAPI LONG WINAPI ADVAPI32$RegEnumKeyExA(
11
HKEY,
12
DWORD,
13
LPWSTR,
14
LPDWORD,
15
LPDWORD,
16
LPWSTR,
17
LPDWORD,
18
PFILETIME
19
);
20
DECLSPEC_IMPORT WINADVAPI LONG WINAPI ADVAPI32$RegQueryValueExA(
21
HKEY,
22
LPCWSTR,
23
LPDWORD,
24
LPDWORD,
25
LPBYTE,
26
LPDWORD
27
);
28
29
BOOL EnumInstalledSoft(CHAR* subKey, CHAR* subKeyName) {
30
31
HKEY hKey = NULL;
32
HKEY hSubKey = NULL;
33
DWORD dwIndexs = 0;
34
CHAR keyName[MAX_PATH] = { 0 };
35
DWORD dwLength = 256;
36
CHAR subKeyValue[MAX_PATH] = { 0 };
37
38
39
if (ADVAPI32$RegOpenKeyExA(HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
40
{
41
while (ADVAPI32$RegEnumKeyExA(hKey, dwIndexs, keyName, &dwLength, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
42
{
43
ADVAPI32$RegOpenKeyA(hKey, keyName, &hSubKey);
44
45
ADVAPI32$RegQueryValueExA(hSubKey,
46
subKeyName,
47
NULL,
48
NULL,
49
(LPBYTE)subKeyValue,
50
&dwLength);
51
52
BeaconPrintf(CALLBACK_OUTPUT, "%s : %s \n", keyName, subKeyValue);
53
ADVAPI32$RegCloseKey(hSubKey);
54
hSubKey = 0;
55
++dwIndexs;
56
dwLength = 256;
57
}
58
}
59
else
60
{
61
return FALSE;
62
}
63
if (hKey != NULL)
64
{
65
ADVAPI32$RegCloseKey(hKey);
66
return TRUE;
67
}
68
}
69
70
int main()
71
{
72
73
74
EnumInstalledSoft((CHAR*)"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", (CHAR*)"DisplayName");
75
EnumInstalledSoft((CHAR*)"Software\\Classes\\Installer\\Products", (CHAR*)"ProductName");
76
return 0;
77
}
Copied!
效果。

缺点

    1.
    似乎无法使用初始化为0的全局变量
    2.
    不太适合跑驻留型的任务,跑大量循环会崩溃
    3.
    一旦引发崩溃整个beacon就会崩掉
    4.
    不易调试
    5.
    似乎输出不能使用unicode。

LINKS

Cobalt Strike 4.1 - The Mark of Injection - Cobalt Strike Research and Development
Cobalt Strike Research and Development
GitHub - dtmsecurity/bof_helper: Beacon Object File (BOF) Creation Helper
GitHub
最近更新 6mo ago