-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbuild.zig
More file actions
353 lines (298 loc) · 13.2 KB
/
Copy pathbuild.zig
File metadata and controls
353 lines (298 loc) · 13.2 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const std = @import("std");
pub const supported_zig_version = std.SemanticVersion{ .major = 0, .minor = 15, .patch = 2 };
pub fn build(b: *std.Build) !void {
ensureZigVersion() catch return;
std.fs.cwd().deleteTree("zig-out") catch {};
const supported_targets: []const std.Target.Query = &.{
.{ .cpu_arch = .x86, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .x86, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu },
.{
.cpu_arch = .arm,
.os_tag = .linux,
.abi = .gnueabihf,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.arm1176jz_s }, // ARMv6kz
},
};
const optimize = b.option(
std.builtin.OptimizeMode,
"optimize",
"Prioritize performance, safety, or binary size (-O flag)",
) orelse .ReleaseSmall;
const osTagStr = @import("bof_launcher_lib").osTagStr;
const cpuArchStr = @import("bof_launcher_lib").cpuArchStr;
const libFileName = @import("bof_launcher_lib").libFileName;
const has_python = blk: {
_ = b.findProgram(&.{"python"}, &.{}) catch break :blk false;
break :blk true;
};
//
// Install and post-process BOFs
//
const bofs_install_path = "bin/bofs/";
const bofs_dep = b.dependency("bof_launcher_bofs", .{ .optimize = optimize });
for (@import("bof_launcher_bofs").bofs_to_build) |bof| {
const full_name = bof.fullName(b.allocator);
var prev_step = &b.addInstallArtifact(
bofs_dep.artifact(full_name),
.{
.dest_dir = .{ .override = .{ .custom = bofs_install_path } },
.dest_sub_path = if (bof.optimize != .Debug) b.fmt("{s}.o", .{full_name}) else null,
},
).step;
if (@import("builtin").cpu.arch == .x86_64) strip: {
// Always skip debug exe
if (std.mem.containsAtLeast(u8, full_name, 1, "debug")) break :strip;
// Blacklist (llvm-objcopy can't remove section for some reason)
if (std.mem.containsAtLeast(u8, full_name, 1, "udpScanner")) break :strip;
if (std.mem.containsAtLeast(u8, full_name, 1, "tcpScanner")) break :strip;
if (std.mem.containsAtLeast(u8, full_name, 1, "grep")) break :strip;
if (std.mem.containsAtLeast(u8, full_name, 1, "find")) break :strip;
if (std.mem.containsAtLeast(u8, full_name, 1, "socat")) break :strip;
if (std.mem.containsAtLeast(u8, full_name, 1, "coff")) {
const run = b.addSystemCommand(&.{
"bin/llvm-objcopy",
"--remove-section=.red",
"--strip-unneeded",
b.fmt("zig-out/" ++ bofs_install_path ++ "{s}.o", .{full_name}),
});
run.step.dependOn(prev_step);
prev_step = &run.step;
}
}
if (has_python) boflint: {
// Always skip debug exe
if (std.mem.containsAtLeast(u8, full_name, 1, "debug")) break :boflint;
// Blacklist (BOFs which use our custom bof*() API)
if (std.mem.containsAtLeast(u8, full_name, 1, "runBofFromBof")) break :boflint;
if (std.mem.containsAtLeast(u8, full_name, 1, "z-beac0n")) break :boflint;
if (std.mem.containsAtLeast(u8, full_name, 1, "wAsmTest")) break :boflint;
if (std.mem.containsAtLeast(u8, full_name, 1, "coff")) {
const run = b.addSystemCommand(&.{
"python",
"utils/boflint.py",
"--logformat",
"vs",
"--loader",
"cs",
b.fmt("zig-out/" ++ bofs_install_path ++ "{s}.o", .{full_name}),
});
run.step.dependOn(prev_step);
prev_step = &run.step;
}
}
b.getInstallStep().dependOn(prev_step);
}
b.getInstallStep().dependOn(&b.addInstallFile(
bofs_dep.namedLazyPath("bof_collection_doc"),
"bof-collection.yaml",
).step);
//
// Install bof launcher library
//
for (supported_targets) |target_query| {
const target = b.resolveTargetQuery(target_query);
const bof_launcher_dep = b.dependency(
"bof_launcher_lib",
.{ .target = target, .optimize = optimize },
);
b.installArtifact(bof_launcher_dep.artifact(
libFileName(b.allocator, target, null),
));
// TODO: Shared library fails to build on Linux x86.
if (target.result.cpu.arch == .x86 and target.result.os.tag == .linux) continue;
b.installArtifact(bof_launcher_dep.artifact(
libFileName(b.allocator, target, "shared"),
));
if (target.result.os.tag == .linux) {
b.installArtifact(bof_launcher_dep.artifact(
libFileName(b.allocator, target, "shared_nolibc"),
));
}
}
//
// Install examples
//
for (supported_targets) |target_query| {
const target = b.resolveTargetQuery(target_query);
// integration with c
{
const dep = b.dependency("integration_with_c", .{ .target = target, .optimize = optimize });
const exe = dep.artifact(b.fmt(
"integration_with_c_{s}_{s}",
.{ osTagStr(target), cpuArchStr(target) },
));
b.installArtifact(exe);
}
// bof stager
const dep = b.dependency("bof_stager", .{ .target = target, .optimize = optimize });
const exe = dep.artifact(b.fmt("bof_stager_{s}_{s}", .{ osTagStr(target), cpuArchStr(target) }));
b.installArtifact(exe);
}
// shellcode in zig
for ([_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
}) |target_query| {
const target = b.resolveTargetQuery(target_query);
const dep = b.dependency("shellcode_in_zig", .{ .target = target, .optimize = optimize });
const shellcode_launcher_exe = dep.artifact(b.fmt(
"shellcode_launcher_{s}_{s}",
.{ osTagStr(target), cpuArchStr(target) },
));
b.installArtifact(shellcode_launcher_exe);
const shellcode_name = b.fmt("shellcode_{s}_{s}", .{ osTagStr(target), cpuArchStr(target) });
const shellcode_exe = dep.artifact(shellcode_name);
b.installArtifact(shellcode_exe);
if (target.result.os.tag == .linux) {
const copy = b.addObjCopy(shellcode_exe.getEmittedBin(), .{ .format = .bin, .only_section = ".text" });
const install = b.addInstallBinFile(copy.getOutput(), b.fmt("{s}.bin", .{shellcode_name}));
b.getInstallStep().dependOn(&install.step);
}
}
// process injection chain
for ([_]std.Target.Query{
.{ .cpu_arch = .x86, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
}) |target_query| {
const target = b.resolveTargetQuery(target_query);
const dep = b.dependency("process_injection_chain", .{ .target = target, .optimize = optimize });
const exe = dep.artifact(b.fmt("process_injection_chain_{s}_{s}", .{ osTagStr(target), cpuArchStr(target) }));
b.installArtifact(exe);
}
// implant
for ([_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
}) |target_query| {
const target = b.resolveTargetQuery(target_query);
const dep = b.dependency("implant", .{ .target = target, .optimize = optimize });
const implant_exe = dep.artifact(b.fmt(
"z-beac0n_{s}_{s}.elf",
.{ osTagStr(target), cpuArchStr(target) },
));
b.installArtifact(implant_exe);
const implant_so = dep.artifact(b.fmt(
"z-beac0n_{s}_{s}",
.{ osTagStr(target), cpuArchStr(target) },
));
b.installArtifact(implant_so);
const shellcode_name = b.fmt("temp_implant_{s}_{s}", .{ osTagStr(target), cpuArchStr(target) });
const shellcode_exe = dep.artifact(shellcode_name);
b.installArtifact(shellcode_exe);
const copy = b.addObjCopy(shellcode_exe.getEmittedBin(), .{ .format = .bin, .only_section = ".text" });
const install = b.addInstallBinFile(
copy.getOutput(),
b.fmt("z-beac0n_{s}_{s}.bin", .{ osTagStr(target), cpuArchStr(target) }),
);
b.getInstallStep().dependOn(&install.step);
}
for ([_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .x86, .os_tag = .windows, .abi = .gnu },
}) |target_query| {
const target = b.resolveTargetQuery(target_query);
const z_beacon = bofs_dep.artifact(b.fmt("z-beac0n-core.coff.{s}", .{cpuArchStr(target)}));
const bof_launcher_dep = b.dependency(
"bof_launcher_lib",
.{ .target = target, .optimize = optimize },
);
const bof_launcher_lib = bof_launcher_dep.artifact(
libFileName(b.allocator, target, "shared"),
);
const win32_dep = b.dependency("bof_launcher_win32", .{});
const win32_module = win32_dep.module("bof_launcher_win32");
const exe = b.addExecutable(.{
.name = b.fmt("z-beac0n_{s}_{s}", .{ osTagStr(target), cpuArchStr(target) }),
.root_module = b.createModule(.{
.root_source_file = b.path("utils/implant_srdi.zig"),
.target = target,
.optimize = optimize,
}),
});
exe.root_module.linkSystemLibrary("ws2_32", .{});
exe.root_module.linkSystemLibrary("ole32", .{});
exe.root_module.linkSystemLibrary("user32", .{});
exe.root_module.linkSystemLibrary("advapi32", .{});
exe.root_module.linkSystemLibrary("secur32", .{});
exe.root_module.addImport("bof_launcher_win32", win32_module);
exe.root_module.addAnonymousImport("srdi", .{
.root_source_file = b.path("bofs/src/include/srdi.zig"),
});
exe.root_module.addAnonymousImport("z_beacon_embed", .{
.root_source_file = z_beacon.getEmittedBin(),
});
exe.root_module.addAnonymousImport("bof_launcher_lib_embed", .{
.root_source_file = bof_launcher_lib.getEmittedBin(),
});
b.installArtifact(exe);
if (@import("builtin").os.tag == .windows) {
const run = b.addRunArtifact(exe);
run.addArg("--dump-shellcode");
run.setCwd(b.path("zig-out/bin"));
b.getInstallStep().dependOn(&run.step);
}
}
//
// Build, install and run tests
//
const test_step = b.step("test", "Run all tests");
for (supported_targets) |target_query| {
const target = b.resolveTargetQuery(target_query);
const bof_launcher_dep = b.dependency(
"bof_launcher_lib",
.{ .target = target, .optimize = optimize },
);
const bof_launcher_api_module = bof_launcher_dep.module("bof_launcher_api");
const bof_launcher_lib = bof_launcher_dep.artifact(
libFileName(b.allocator, target, null),
);
const bof_api_module = bofs_dep.module("bof_api");
const win32_dep = b.dependency("bof_launcher_win32", .{});
const win32_module = win32_dep.module("bof_launcher_win32");
const tests = b.addTest(.{
.name = "bof-launcher-tests",
//.filters = &.{"udpScanner"},
.root_module = b.createModule(.{
.root_source_file = bofs_dep.path("src/tests/tests.zig"),
.target = target,
.optimize = optimize,
}),
});
tests.addIncludePath(bof_launcher_dep.path("src"));
tests.linkLibrary(bof_launcher_lib);
tests.addCSourceFile(.{
.file = bofs_dep.path("src/tests/tests.c"),
.flags = &.{"-std=c99"},
});
tests.linkLibC();
tests.root_module.addImport("bof_api", bof_api_module);
tests.root_module.addImport("bof_launcher_api", bof_launcher_api_module);
tests.root_module.addImport("bof_launcher_win32", win32_module);
const run = b.addRunArtifact(tests);
run.skip_foreign_checks = true;
run.step.dependOn(b.getInstallStep());
test_step.dependOn(&run.step);
}
}
fn ensureZigVersion() !void {
var installed_ver = @import("builtin").zig_version;
installed_ver.build = null;
if (installed_ver.order(supported_zig_version) != .eq) {
std.log.err("\n" ++
\\---------------------------------------------------------------------------
\\
\\Installed Zig compiler version is not supported.
\\
\\Required version is: {any}
\\Installed version: {any}
\\
\\Please install supported version and try again.
\\
\\---------------------------------------------------------------------------
\\
, .{ supported_zig_version, installed_ver });
return error.ZigIsTooOld;
}
}