Adversarial C2 Protocol Implemented in Zig
0
fork

Configure Feed

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

Fix extra bytes in connection message.

+62 -31
+62 -31
src/message.zig
··· 38 38 39 39 pub fn getPayload(self: *align(1) Relay) []u8 { 40 40 const len: *u16 = @ptrFromInt(@intFromPtr(self) - @sizeOf(u16)); 41 - return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @sizeOf(Relay)]; 41 + return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @bitSizeOf(Relay) / 8]; 42 42 } 43 43 }; 44 44 const Connection = packed struct { ··· 52 52 53 53 pub fn getPayload(self: *align(1) Connection) []u8 { 54 54 const len: *u16 = @ptrFromInt(@intFromPtr(self) - @sizeOf(u16)); 55 - return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @sizeOf(Connection)]; 55 + return @as([*]u8, @ptrCast(&self.payload))[0 .. len.* - @bitSizeOf(Connection) / 8]; 56 56 } 57 57 58 58 fn nativeFromNetworkEndian(self: *align(1) Connection) void { ··· 91 91 92 92 /// Compute the number of bytes required to store a given payload size for a given message type. 93 93 pub fn calcSize(comptime @"type": PacketType, payload_len: usize) MessageTypeError!u16 { 94 - std.debug.assert(payload_len < std.math.maxInt(u16)); 95 - const header_size = @sizeOf(switch (@"type") { 94 + const header_size = @bitSizeOf(switch (@"type") { 96 95 .relay => Relay, 97 96 .connection => Connection, 98 97 .file_transfer => return MessageTypeError.NotImplementedSaprusType, 99 98 else => return MessageTypeError.UnknownSaprusType, 100 - }); 99 + }) / 8; 101 100 return @intCast(payload_len + @sizeOf(Self) + header_size); 102 101 } 103 102 ··· 191 190 }; 192 191 193 192 test "testing variable length zero copy struct" { 194 - const payload = "Hello darkness my old friend"; 195 - var msg_bytes: [try Message.calcSize(.relay, payload.len)]u8 align(@alignOf(Message)) = undefined; 193 + { 194 + // Relay test 195 + const payload = "Hello darkness my old friend"; 196 + var msg_bytes: [try Message.calcSize(.relay, payload.len)]u8 align(@alignOf(Message)) = undefined; 196 197 197 - // Create a view of the byte slice as a Message 198 - const msg: *Message = .init(.relay, &msg_bytes); 198 + // Create a view of the byte slice as a Message 199 + const msg: *Message = .init(.relay, &msg_bytes); 199 200 200 - { 201 - // Set the message values 202 201 { 203 - // These are both set by the init call. 204 - // msg.type = .relay; 205 - // msg.length = payload_len; 202 + // Set the message values 203 + { 204 + // These are both set by the init call. 205 + // msg.type = .relay; 206 + // msg.length = payload_len; 207 + } 208 + const relay = (try msg.getSaprusTypePayload()).relay; 209 + relay.dest = .{ 1, 2, 3, 4 }; 210 + @memcpy(relay.getPayload(), payload); 211 + } 212 + 213 + { 214 + // Print the message as hex using the network byte order 215 + try msg.networkFromNativeEndian(); 216 + // We know the error from nativeFromNetworkEndian is unreachable because 217 + // it would have returned an error from networkFromNativeEndian. 218 + defer msg.nativeFromNetworkEndian() catch unreachable; 219 + std.debug.print("relay network bytes: {x}\n", .{msg_bytes}); 220 + std.debug.print("bytes len: {d}\n", .{msg_bytes.len}); 221 + } 222 + 223 + if (false) { 224 + // Illegal behavior 225 + std.debug.print("{any}\n", .{(try msg.getSaprusTypePayload()).connection}); 206 226 } 207 - const relay = (try msg.getSaprusTypePayload()).relay; 208 - relay.dest = .{ 1, 2, 3, 4 }; 209 - @memcpy(relay.getPayload(), payload); 227 + 228 + try std.testing.expectEqualDeep(msg, try Message.bytesAsValue(msg.asBytes())); 210 229 } 211 230 212 231 { 213 - const bytes = msg.asBytes(); 232 + // Connection test 233 + const payload = "Hello darkness my old friend"; 234 + var msg_bytes: [try Message.calcSize(.connection, payload.len)]u8 align(@alignOf(Message)) = undefined; 214 235 215 - // Print the message as hex using the network byte order 216 - try msg.networkFromNativeEndian(); 217 - // We know the error from nativeFromNetworkEndian is unreachable because 218 - // it would have returned an error from networkFromNativeEndian. 219 - defer msg.nativeFromNetworkEndian() catch unreachable; 220 - std.debug.print("network bytes: {x}\n", .{bytes}); 221 - std.debug.print("bytes len: {d}\n", .{bytes.len}); 222 - } 236 + // Create a view of the byte slice as a Message 237 + const msg: *Message = .init(.connection, &msg_bytes); 223 238 224 - if (false) { 225 - // Illegal behavior 226 - std.debug.print("{any}\n", .{(try msg.getSaprusTypePayload()).connection}); 239 + { 240 + // Initializing connection header values 241 + const connection = (try msg.getSaprusTypePayload()).connection; 242 + connection.src_port = 1; 243 + connection.dest_port = 2; 244 + connection.seq_num = 3; 245 + connection.msg_id = 4; 246 + connection.reserved = 5; 247 + connection.options = @bitCast(@as(u8, 6)); 248 + @memcpy(connection.getPayload(), payload); 249 + } 250 + 251 + { 252 + // Print the message as hex using the network byte order 253 + try msg.networkFromNativeEndian(); 254 + // We know the error from nativeFromNetworkEndian is unreachable because 255 + // it would have returned an error from networkFromNativeEndian. 256 + defer msg.nativeFromNetworkEndian() catch unreachable; 257 + std.debug.print("connection network bytes: {x}\n", .{msg_bytes}); 258 + std.debug.print("bytes len: {d}\n", .{msg_bytes.len}); 259 + } 227 260 } 228 - 229 - try std.testing.expectEqualDeep(msg, try Message.bytesAsValue(msg.asBytes())); 230 261 } 231 262 232 263 const std = @import("std");