Adversarial C2 Protocol Implemented in Zig
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add C API

+144
+17
build.zig
··· 41 41 .target = target, 42 42 }); 43 43 44 + // Create static library 45 + const lib = b.addLibrary(.{ 46 + .name = "zaprus", 47 + .root_module = b.createModule(.{ 48 + .root_source_file = b.path("src/c_api.zig"), 49 + .target = target, 50 + .optimize = optimize, 51 + .link_libc = true, 52 + .imports = &.{ 53 + .{ .name = "zaprus", .module = mod }, 54 + }, 55 + }), 56 + }); 57 + 58 + b.installArtifact(lib); 59 + lib.installHeader(b.path("include/zaprus.h"), "zaprus.h"); 60 + 44 61 // Here we define an executable. An executable needs to have a root module 45 62 // which needs to expose a `main` function. While we could add a main function 46 63 // to the module defined above, it's sometimes preferable to split business
+33
include/zaprus.h
··· 1 + #ifndef ZAPRUS_H 2 + #define ZAPRUS_H 3 + 4 + #include <stdint.h> 5 + #include <stdlib.h> 6 + 7 + typedef void* zaprus_client; 8 + typedef void* zaprus_connection; 9 + 10 + // Returns NULL if there was an error. 11 + zaprus_client zaprus_init_client(void); 12 + 13 + void zaprus_deinit_client(zaprus_client client); 14 + 15 + // Returns 0 on success, else returns 1. 16 + int zaprus_client_send_relay(zaprus_client client, const char* payload, size_t payload_len, const char dest[4]); 17 + 18 + // Returns NULL if there was an error. 19 + // Caller should call zaprus_deinit_connection when done with the connection. 20 + zaprus_connection zaprus_connect(zaprus_client client, const char* payload, size_t payload_len); 21 + 22 + void zaprus_deinit_connection(zaprus_connection connection); 23 + 24 + // Capacity is the maximum length of the output buffer. 25 + // out_len is modified to specify how much of the capacity is used by the response. 26 + // Blocks until the next message is available, or returns 1 if the underlying socket times out. 27 + // Returns 0 on success, else returns 1. 28 + int zaprus_connection_next(zaprus_connection connection, char *out, size_t capacity, size_t *out_len); 29 + 30 + // Returns 0 on success, else returns 1. 31 + int zaprus_connection_send(zaprus_connection connection, const char *payload, size_t payload_len); 32 + 33 + #endif // ZAPRUS_H
+4
src/Connection.zig
··· 12 12 }; 13 13 } 14 14 15 + pub fn deinit(self: *Connection) void { 16 + self.socket.deinit(); 17 + } 18 + 15 19 pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 { 16 20 _ = io; 17 21 log.debug("Awaiting connection message", .{});
+89
src/c_api.zig
··· 1 + const std = @import("std"); 2 + const zaprus = @import("zaprus"); 3 + 4 + // Opaque types for C API 5 + const ZaprusClient = opaque {}; 6 + const ZaprusConnection = opaque {}; 7 + 8 + const alloc = std.heap.c_allocator; 9 + const io = std.Io.Threaded.global_single_threaded.io(); 10 + 11 + export fn zaprus_init_client() ?*ZaprusClient { 12 + const client = alloc.create(zaprus.Client) catch return null; 13 + client.* = zaprus.Client.init() catch { 14 + alloc.destroy(client); 15 + return null; 16 + }; 17 + return @ptrCast(client); 18 + } 19 + 20 + export fn zaprus_deinit_client(client: ?*ZaprusClient) void { 21 + const c: ?*zaprus.Client = @ptrCast(@alignCast(client)); 22 + if (c) |zc| { 23 + zc.deinit(); 24 + alloc.destroy(zc); 25 + } 26 + } 27 + 28 + export fn zaprus_client_send_relay( 29 + client: ?*ZaprusClient, 30 + payload: [*c]const u8, 31 + payload_len: usize, 32 + dest: [*c]const u8, 33 + ) c_int { 34 + const c: ?*zaprus.Client = @ptrCast(@alignCast(client)); 35 + const zc = c orelse return 1; 36 + 37 + zc.sendRelay(io, payload[0..payload_len], dest[0..4].*) catch return 1; 38 + return 0; 39 + } 40 + 41 + export fn zaprus_connect( 42 + client: ?*ZaprusClient, 43 + payload: [*c]const u8, 44 + payload_len: usize, 45 + ) ?*ZaprusConnection { 46 + const c: ?*zaprus.Client = @ptrCast(@alignCast(client)); 47 + const zc = c orelse return null; 48 + 49 + const connection = alloc.create(zaprus.Connection) catch return null; 50 + connection.* = zc.connect(io, payload[0..payload_len]) catch { 51 + alloc.destroy(connection); 52 + return null; 53 + }; 54 + return @ptrCast(connection); 55 + } 56 + 57 + export fn zaprus_deinit_connection(connection: ?*ZaprusConnection) void { 58 + const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection)); 59 + if (c) |zc| { 60 + zc.deinit(); 61 + alloc.destroy(zc); 62 + } 63 + } 64 + 65 + export fn zaprus_connection_next( 66 + connection: ?*ZaprusConnection, 67 + out: [*c]u8, 68 + capacity: usize, 69 + out_len: *usize, 70 + ) c_int { 71 + const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection)); 72 + const zc = c orelse return 1; 73 + 74 + const result = zc.next(io, out[0..capacity]) catch return 1; 75 + out_len.* = result.len; 76 + return 0; 77 + } 78 + 79 + export fn zaprus_connection_send( 80 + connection: ?*ZaprusConnection, 81 + payload: [*c]const u8, 82 + payload_len: usize, 83 + ) c_int { 84 + const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection)); 85 + const zc = c orelse return 1; 86 + 87 + zc.send(io, payload[0..payload_len]) catch return 1; 88 + return 0; 89 + }
+1
src/main.zig
··· 134 134 var w: Writer = .fixed(&init_con_buf); 135 135 try w.print("{b64}", .{flags.connect.?}); 136 136 var connection = try client.connect(init.io, w.buffered()); 137 + defer connection.deinit(); 137 138 138 139 log.debug("Connection started", .{}); 139 140