Adversarial C2 Protocol Implemented in Zig
0
fork

Configure Feed

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

refactor: use packed struct for IpAddr

This is more intuitive than using u32 directly, and follows the same pattern as the new MAC addr handling

authored by

Robby Zambito and committed by
Tangled
91fdb2c6 7077aae9

+31 -20
+6 -6
src/Client.zig
··· 46 46 const rand = io_source.interface(); 47 47 48 48 var headers: EthIpUdp = .{ 49 - .src_mac = .fromSlice(self.socket.mac), 49 + .src_mac = .fromBytes(self.socket.mac), 50 50 .ip = .{ 51 51 .id = rand.int(u16), 52 - .src_addr = 0, //rand.int(u32), 53 - .dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }), 52 + .src_addr = .fromBytes(.{ 0, 0, 0, 0 }), //rand.int(u32), 53 + .dst_addr = .fromBytes(.{ 255, 255, 255, 255 }), 54 54 .len = undefined, 55 55 }, 56 56 .udp = .{ ··· 86 86 const rand = io_source.interface(); 87 87 88 88 var headers: EthIpUdp = .{ 89 - .src_mac = .fromSlice(self.socket.mac), 89 + .src_mac = .fromBytes(self.socket.mac), 90 90 .ip = .{ 91 91 .id = rand.int(u16), 92 - .src_addr = 0, //rand.int(u32), 93 - .dst_addr = @bitCast([_]u8{ 255, 255, 255, 255 }), 92 + .src_addr = .fromBytes(.{ 0, 0, 0, 0 }), //rand.int(u32), 93 + .dst_addr = .fromBytes(.{ 255, 255, 255, 255 }), 94 94 .len = undefined, 95 95 }, 96 96 .udp = .{
+25 -14
src/EthIpUdp.zig
··· 14 14 // You should have received a copy of the GNU General Public License along with 15 15 // Zaprus. If not, see <https://www.gnu.org/licenses/>. 16 16 17 + pub const IpAddr = packed struct { 18 + int: I, 19 + 20 + const V = @Vector(4, u8); 21 + const I = u32; 22 + 23 + pub fn fromBytes(s: V) IpAddr { 24 + return .{ .int = @bitCast(s) }; 25 + } 26 + }; 27 + 28 + pub const MacAddr = packed struct { 29 + int: I, 30 + 31 + const V = @Vector(6, u8); 32 + const I = @Int(.unsigned, @bitSizeOf(V)); 33 + 34 + pub fn fromBytes(s: V) MacAddr { 35 + return .{ .int = @bitCast(s) }; 36 + } 37 + }; 38 + 17 39 pub const EthIpUdp = packed struct(u336) { // 42 bytes * 8 bits = 336 18 40 // --- UDP (Last in memory, defined first for LSB->MSB) --- 19 41 udp: packed struct { ··· 25 47 26 48 // --- IP --- 27 49 ip: packed struct { 28 - dst_addr: u32, 29 - src_addr: u32, 50 + dst_addr: IpAddr, 51 + src_addr: IpAddr, 30 52 header_checksum: u16 = 0, 31 53 protocol: u8 = 17, // udp 32 54 ttl: u8 = 0x40, ··· 54 76 // --- Ethernet --- 55 77 eth_type: u16 = std.os.linux.ETH.P.IP, 56 78 src_mac: MacAddr, 57 - dst_mac: MacAddr = .fromSlice(@splat(0xff)), 58 - 59 - pub const MacAddr = packed struct { 60 - int: I, 61 - 62 - pub const V = @Vector(6, u8); 63 - pub const I = @Int(.unsigned, @bitSizeOf(V)); 64 - 65 - pub fn fromSlice(s: V) MacAddr { 66 - return .{ .int = @bitCast(s) }; 67 - } 68 - }; 79 + dst_mac: MacAddr = .fromBytes(@splat(0xff)), 69 80 70 81 pub fn toBytes(self: @This()) [336 / 8]u8 { 71 82 var res: [336 / 8]u8 = undefined;