Adversarial C2 Protocol Implemented in Zig
1// Copyright 2026 Robby Zambito
2//
3// This file is part of zaprus.
4//
5// Zaprus is free software: you can redistribute it and/or modify it under the
6// terms of the GNU General Public License as published by the Free Software
7// Foundation, either version 3 of the License, or (at your option) any later
8// version.
9//
10// Zaprus is distributed in the hope that it will be useful, but WITHOUT ANY
11// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License along with
15// Zaprus. If not, see <https://www.gnu.org/licenses/>.
16
17const std = @import("std");
18const zaprus = @import("zaprus");
19
20// Opaque types for C API
21const ZaprusClient = opaque {};
22const ZaprusConnection = opaque {};
23
24const alloc = std.heap.c_allocator;
25const io = std.Io.Threaded.global_single_threaded.io();
26
27export fn zaprus_init_client() ?*ZaprusClient {
28 const client = alloc.create(zaprus.Client) catch return null;
29 client.* = zaprus.Client.init() catch {
30 alloc.destroy(client);
31 return null;
32 };
33 return @ptrCast(client);
34}
35
36export fn zaprus_deinit_client(client: ?*ZaprusClient) void {
37 const c: ?*zaprus.Client = @ptrCast(@alignCast(client));
38 if (c) |zc| {
39 zc.deinit();
40 alloc.destroy(zc);
41 }
42}
43
44export fn zaprus_client_send_relay(
45 client: ?*ZaprusClient,
46 payload: [*c]const u8,
47 payload_len: usize,
48 dest: [*c]const u8,
49) c_int {
50 const c: ?*zaprus.Client = @ptrCast(@alignCast(client));
51 const zc = c orelse return 1;
52
53 zc.sendRelay(io, payload[0..payload_len], dest[0..4].*) catch return 1;
54 return 0;
55}
56
57export fn zaprus_connect(
58 client: ?*ZaprusClient,
59 payload: [*c]const u8,
60 payload_len: usize,
61) ?*ZaprusConnection {
62 const c: ?*zaprus.Client = @ptrCast(@alignCast(client));
63 const zc = c orelse return null;
64
65 const connection = alloc.create(zaprus.Connection) catch return null;
66 connection.* = zc.connect(io, payload[0..payload_len]) catch {
67 alloc.destroy(connection);
68 return null;
69 };
70 return @ptrCast(connection);
71}
72
73export fn zaprus_deinit_connection(connection: ?*ZaprusConnection) void {
74 const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection));
75 if (c) |zc| {
76 alloc.destroy(zc);
77 }
78}
79
80export fn zaprus_connection_next(
81 connection: ?*ZaprusConnection,
82 out: [*c]u8,
83 capacity: usize,
84 out_len: *usize,
85) c_int {
86 const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection));
87 const zc = c orelse return 1;
88
89 const result = zc.next(io, out[0..capacity]) catch return 1;
90 out_len.* = result.len;
91 return 0;
92}
93
94export fn zaprus_connection_send(
95 connection: ?*ZaprusConnection,
96 payload: [*c]const u8,
97 payload_len: usize,
98) c_int {
99 const c: ?*zaprus.Connection = @ptrCast(@alignCast(connection));
100 const zc = c orelse return 1;
101
102 zc.send(io, .{}, payload[0..payload_len]) catch return 1;
103 return 0;
104}