Adversarial C2 Protocol Implemented in Zig
0
fork

Configure Feed

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

Write ether headers properly

+49 -75
+49 -75
src/Client.zig
··· 1 1 const base64Enc = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '='); 2 2 const base64Dec = std.base64.Base64Decoder.init(std.base64.standard_alphabet_chars, '='); 3 3 4 + const native_endian = @import("builtin").cpu.arch.endian(); 5 + 4 6 rand: Random, 5 7 writer: *std.Io.Writer, 6 8 ··· 31 33 fn broadcastInitialInterestMessage(self: *Self, msg_bytes: []align(@alignOf(SaprusMessage)) u8) !void { 32 34 const writer = self.writer; 33 35 34 - const UdpHeaders = packed struct { 35 - dest_mac: u48, // [6]u8 36 + const EthernetHeaders = packed struct { 37 + dest_mac: @Vector(6, u8), 36 38 37 - src_mac: u48, // [6]u8 39 + src_mac: @Vector(6, u8), 38 40 39 41 ether_type: u16, 42 + }; 43 + 44 + const IpHeaders = packed struct { 40 45 ip_version: u4, 41 - header_length: u4, 42 - type_of_service: u8, 46 + header_length: u4 = 0, 47 + type_of_service: u8 = 0, 43 48 total_length: u16, 44 49 45 - identification: u16, 46 - ethernet_flags: u3, 47 - fragment_offset: u13, 48 - ttl: u8, 49 - protocol: u8, 50 + identification: u16 = 0, 51 + ethernet_flags: u3 = 0, 52 + fragment_offset: u13 = 0, 53 + ttl: u8 = 0, 54 + protocol: u8 = 0, 50 55 51 - header_checksum: u16, // [2]u8 52 - src_ip: u16, // [2]u8 53 - dest_ip: u16, // [2]u8 56 + header_checksum: @Vector(2, u8) = .{ 0, 0 }, 57 + src_ip: @Vector(4, u8), 54 58 55 - src_port: u16, // [2]u8 56 - dest_port: u16, // [2]u8 57 - length: u16, 59 + dest_ip: @Vector(4, u8), 60 + }; 58 61 59 - checksum: u16, // [2]u8 62 + const UdpHeaders = packed struct { 63 + src_port: @Vector(2, u8), 60 64 61 - fn init( 62 - in: struct { 63 - dest_mac: [6]u8, 64 - src_mac: [6]u8, 65 - ether_type: u16, 66 - ip_version: u4, 67 - header_length: u4 = 0, 68 - type_of_service: u4 = 0, 69 - total_length: usize, 70 - identification: u16 = 0, 71 - ethernet_flags: u3 = 0, 72 - fragment_offset: u13 = 0, 73 - ttl: u8 = 0, 74 - protocol: u8 = 0, 75 - header_checksum: [2]u8 = .{ 0, 0 }, 76 - src_ip: [2]u8, 77 - dest_ip: [2]u8, 78 - src_port: [2]u8, 79 - dest_port: [2]u8, 80 - length: usize, 81 - checksum: [2]u8 = .{ 0, 0 }, 82 - }, 83 - ) @This() { 84 - return .{ 85 - .dest_mac = @bitCast(in.dest_mac), 86 - .src_mac = @bitCast(in.src_mac), 87 - .ether_type = in.ether_type, 88 - .ip_version = in.ip_version, 89 - .header_length = in.header_length, 90 - .type_of_service = in.type_of_service, 91 - .total_length = @intCast(in.total_length), 92 - .identification = in.identification, 93 - .ethernet_flags = in.ethernet_flags, 94 - .fragment_offset = in.fragment_offset, 95 - .ttl = in.ttl, 96 - .protocol = in.protocol, 97 - .header_checksum = @bitCast(in.header_checksum), 98 - .src_ip = @bitCast(in.src_ip), 99 - .dest_ip = @bitCast(in.dest_ip), 100 - .src_port = @bitCast(in.src_port), 101 - .dest_port = @bitCast(in.dest_port), 102 - .length = @intCast(in.length), 103 - .checksum = @bitCast(in.checksum), 104 - }; 105 - } 65 + dest_port: @Vector(2, u8), 66 + length: u16, 67 + checksum: @Vector(2, u8) = .{ 0, 0 }, 106 68 }; 107 69 108 70 const total_len = (@bitSizeOf(UdpHeaders) / 8) + msg_bytes.len; 109 71 std.debug.assert(writer.buffer.len >= total_len); 110 72 _ = writer.consumeAll(); 111 73 112 - const headers: UdpHeaders = .init(.{ 113 - .dest_mac = .{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 74 + var ether_headers: EthernetHeaders = .{ 75 + .dest_mac = .{ 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff }, 76 + // .src_mac = .{ 0xee, 0xee, 0xee, 0xee, 0xee, 0xee }, 114 77 .src_mac = blk: { 115 - const r_bytes = try writer.writableArray(6); 116 - self.rand.bytes(r_bytes); 117 - break :blk r_bytes.*; 78 + var output_bytes: [6]u8 = undefined; 79 + // const r_bytes = try writer.writableArray(6); 80 + self.rand.bytes(&output_bytes); 81 + break :blk output_bytes; 118 82 }, 119 83 .ether_type = 0x0800, 84 + }; 85 + 86 + const ip_headers: IpHeaders = .{ 120 87 .ip_version = 0x4, 121 88 .header_length = 0x5, 122 - .total_length = total_len - 8, // 8 is the ethernet frame length (macs + type) 89 + .total_length = @intCast(total_len - 8), // 8 is the ethernet frame length (macs + type) 123 90 .protocol = 0x11, 124 - .src_ip = .{ 0, 0 }, 125 - .dest_ip = .{ 0, 0 }, 91 + .src_ip = .{ 0, 0, 0, 0 }, 92 + .dest_ip = .{ 0, 0, 0, 0 }, 93 + }; 94 + 95 + const udp_headers: UdpHeaders = .{ 126 96 .src_port = .{ 0, 0 }, 127 97 .dest_port = .{ 0xb8, 0x22 }, 128 - .length = msg_bytes.len, 129 - }); 98 + .length = @intCast(msg_bytes.len), 99 + }; 100 + _ = ip_headers; 101 + _ = udp_headers; 102 + _ = &ether_headers; 130 103 131 - _ = try writer.write(&@as([@bitSizeOf(UdpHeaders) / 8]u8, @bitCast(headers))); 104 + // _ = try writer.write(&@as([@bitSizeOf(UdpHeaders) / 8]u8, @bitCast(headers))); 132 105 133 - // try writer.writeStruct(headers, .big); 106 + std.mem.byteSwapAllFields(EthernetHeaders, &ether_headers); 107 + try writer.writeStruct(ether_headers, native_endian); 134 108 135 109 // // Ensure buffer is large enough 136 110 // std.debug.assert(writer.buffer.len > 38 + msg_bytes.len); ··· 190 164 try msg.networkFromNativeEndian(); 191 165 defer msg.nativeFromNetworkEndian() catch unreachable; 192 166 193 - std.debug.print("headers: {x}\n", .{@as([@bitSizeOf(UdpHeaders) / 8]u8, @bitCast(headers))}); 167 + // std.debug.print("headers: {x}\n", .{@as([@bitSizeOf(UdpHeaders) / 8]u8, @bitCast(headers))}); 194 168 std.debug.print("bytes: {x}\n", .{writer.buffer[0..writer.end]}); 195 169 _ = try writer.write(msg_bytes); 196 170