Adversarial C2 Protocol Implemented in Zig
0
fork

Configure Feed

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

start porting to 0.16.0

+105 -91
+1 -10
build.zig
··· 41 41 .target = target, 42 42 }); 43 43 44 - mod.addImport("network", b.dependency("network", .{}).module("network")); 45 - mod.addImport("gatorcat", b.dependency("gatorcat", .{}).module("gatorcat")); 46 - 47 44 // Here we define an executable. An executable needs to have a root module 48 45 // which needs to expose a `main` function. While we could add a main function 49 46 // to the module defined above, it's sometimes preferable to split business 50 - // business logic and the CLI into two separate modules. 47 + // logic and the CLI into two separate modules. 51 48 // 52 49 // If your goal is to create a Zig library for others to use, consider if 53 50 // it might benefit from also exposing a CLI tool. A parser library for a ··· 82 79 // can be extremely useful in case of collisions (which can happen 83 80 // importing modules from different packages). 84 81 .{ .name = "zaprus", .module = mod }, 85 - .{ .name = "clap", .module = b.dependency("clap", .{}).module("clap") }, 86 82 }, 87 83 }), 88 84 }); ··· 92 88 // step). By default the install prefix is `zig-out/` but can be overridden 93 89 // by passing `--prefix` or `-p`. 94 90 b.installArtifact(exe); 95 - b.installArtifact(b.addLibrary(.{ 96 - .linkage = .static, 97 - .name = "zaprus", 98 - .root_module = mod, 99 - })); 100 91 101 92 // This creates a top level step. Top level steps have a name and can be 102 93 // invoked by name when running `zig build` (e.g. `zig build run`).
+2 -15
build.zig.zon
··· 28 28 29 29 // Tracks the earliest Zig version that the package considers to be a 30 30 // supported use case. 31 - .minimum_zig_version = "0.14.0", 31 + .minimum_zig_version = "0.16.0", 32 32 33 33 // This field is optional. 34 34 // Each dependency must either provide a `url` and `hash`, or a `path`. 35 35 // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. 36 36 // Once all dependencies are fetched, `zig build` no longer requires 37 37 // internet connectivity. 38 - .dependencies = .{ 39 - .network = .{ 40 - .url = "git+https://github.com/ikskuh/zig-network#7947237eec317d9458897f82089f343a05450c2b", 41 - .hash = "network-0.1.0-Pm-Agl8xAQBmkwohveGOfTk4zQnuqDs0Ptfbms4KP5Ce", 42 - }, 43 - .clap = .{ 44 - .url = "git+https://github.com/Hejsil/zig-clap#9cfa61596cd44ef7be35f8d2e108d2025e09868e", 45 - .hash = "clap-0.10.0-oBajB_TnAQB0l5UdW9WYhhJDEswbedvwFOzzZwGknYeR", 46 - }, 47 - .gatorcat = .{ 48 - .url = "git+https://github.com/jeffective/gatorcat#db73d0f7780331d82e785e85773d1afaf154c2e6", 49 - .hash = "gatorcat-0.3.11-WcrpTQn0BwArrCFVHy9FPBIPDJQqPrFdJlhiyH7Ng5x4", 50 - }, 51 - }, 38 + .dependencies = .{}, 52 39 .paths = .{ 53 40 "build.zig", 54 41 "build.zig.zon",
+101 -65
src/main.zig
··· 1 1 const is_debug = builtin.mode == .Debug; 2 2 3 - /// This creates a debug allocator that can only be referenced in debug mode. 4 - /// You should check for is_debug around every reference to dba. 5 - var dba: DebugAllocator = 6 - if (is_debug) 7 - DebugAllocator.init 8 - else 9 - @compileError("Should not use debug allocator in release mode"); 3 + const help = 4 + \\-h, --help Display this help and exit. 5 + \\-r, --relay <str> A relay message to send. 6 + \\-d, --dest <str> An IPv4 or <= 4 ASCII byte string. 7 + \\-c, --connect <str> A connection message to send. 8 + \\ 9 + ; 10 10 11 - pub fn main() !void { 12 - defer if (is_debug) { 13 - _ = dba.deinit(); 14 - }; 15 - 16 - const gpa = if (is_debug) dba.allocator() else std.heap.smp_allocator; 11 + const Option = enum { help, relay, dest, connect }; 12 + const to_option: StaticStringMap(Option) = .initComptime(.{ 13 + .{ "-h", .help }, 14 + .{ "--help", .help }, 15 + .{ "-r", .relay }, 16 + .{ "--relay", .relay }, 17 + .{ "-d", .dest }, 18 + .{ "--dest", .dest }, 19 + .{ "-c", .connect }, 20 + .{ "--connect", .connect }, 21 + }); 17 22 23 + pub fn main(init: std.process.Init) !void { 18 24 // CLI parsing adapted from the example here 19 - // https://github.com/Hejsil/zig-clap/blob/e47028deaefc2fb396d3d9e9f7bd776ae0b2a43a/README.md#examples 25 + // https://codeberg.org/ziglang/zig/pulls/30644 20 26 21 - // First we specify what parameters our program can take. 22 - // We can use `parseParamsComptime` to parse a string into an array of `Param(Help)`. 23 - const params = comptime clap.parseParamsComptime( 24 - \\-h, --help Display this help and exit. 25 - \\-r, --relay <str> A relay message to send. 26 - \\-d, --dest <str> An IPv4 or <= 4 ASCII byte string. 27 - \\-c, --connect <str> A connection message to send. 28 - \\ 29 - ); 27 + const args = try init.minimal.args.toSlice(init.arena.allocator()); 30 28 31 - // Initialize our diagnostics, which can be used for reporting useful errors. 32 - // This is optional. You can also pass `.{}` to `clap.parse` if you don't 33 - // care about the extra information `Diagnostics` provides. 34 - var diag = clap.Diagnostic{}; 35 - var res = clap.parse(clap.Help, &params, clap.parsers.default, .{ 36 - .diagnostic = &diag, 37 - .allocator = gpa, 38 - }) catch |err| { 39 - // Report useful error and exit. 40 - try diag.reportToFile(.stderr(), err); 41 - return err; 42 - }; 43 - defer res.deinit(); 44 - 45 - if (res.args.help != 0) { 46 - return clap.helpToFile(.stderr(), clap.Help, &params, .{}); 29 + if (args.len == 1) { 30 + std.debug.print("{s}", .{help}); 31 + return; 47 32 } 48 33 49 - var sock_buffer: [1500]u8 = undefined; 50 - var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev 51 - var net_buffer: [1500]u8 = undefined; 52 - var net_writer: NetWriter = try .init(&raw_socket_writer.interface, &net_buffer); 53 - var client = try SaprusClient.init(&net_writer.interface); 54 - defer client.deinit(); 34 + var flags: struct { 35 + relay: ?[]const u8 = null, 36 + dest: ?[]const u8 = null, 37 + connect: ?[]const u8 = null, 38 + } = .{}; 55 39 56 - if (res.args.relay) |r| { 57 - const dest = parseDest(res.args.dest); 58 - try client.sendRelay( 59 - if (r.len > 0) r else "Hello darkness my old friend", 60 - dest, 61 - ); 62 - return; 63 - } else if (res.args.connect) |c| { 64 - if (false) { 65 - _ = client.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) { 66 - error.WouldBlock => null, 67 - else => return err, 68 - }; 69 - return; 40 + { 41 + var i: usize = 1; 42 + while (i < args.len) : (i += 1) { 43 + if (to_option.get(args[i])) |opt| { 44 + switch (opt) { 45 + .help => { 46 + std.debug.print("{s}\n", .{help}); 47 + return; 48 + }, 49 + .relay => { 50 + i += 1; 51 + if (i < args.len) { 52 + flags.relay = args[i]; 53 + } else { 54 + std.debug.print("-r/--relay requires a string\n", .{}); 55 + return; 56 + } 57 + }, 58 + .dest => { 59 + i += 1; 60 + if (i < args.len) { 61 + flags.dest = args[i]; 62 + } else { 63 + std.debug.print("-d/--dest requires a string\n", .{}); 64 + return; 65 + } 66 + }, 67 + .connect => { 68 + i += 1; 69 + if (i < args.len) { 70 + flags.connect = args[i]; 71 + } else { 72 + std.debug.print("-c/--connect requires a string\n", .{}); 73 + return; 74 + } 75 + }, 76 + } 77 + } else { 78 + std.debug.print("Unknown argument: {s}\n", .{args[i]}); 79 + } 70 80 } 71 - @panic("Not implemented"); 72 81 } 73 82 74 - return clap.helpToFile(.stderr(), clap.Help, &params, .{}); 83 + std.debug.print("relay: {s}\n", .{flags.relay orelse "<null>"}); 84 + std.debug.print("dest: {s}\n", .{flags.dest orelse "<null>"}); 85 + std.debug.print("connect: {s}\n", .{flags.connect orelse "<null>"}); 86 + 87 + // var sock_buffer: [1500]u8 = undefined; 88 + // var raw_socket_writer: RawSocketWriter = try .init("enp7s0", &sock_buffer); // /proc/net/dev 89 + // var net_buffer: [1500]u8 = undefined; 90 + // var net_writer: NetWriter = try .init(&raw_socket_writer.interface, &net_buffer); 91 + // var client = try SaprusClient.init(&net_writer.interface); 92 + // defer client.deinit(); 93 + 94 + // if (res.args.relay) |r| { 95 + // const dest = parseDest(res.args.dest); 96 + // try client.sendRelay( 97 + // if (r.len > 0) r else "Hello darkness my old friend", 98 + // dest, 99 + // ); 100 + // return; 101 + // } else if (res.args.connect) |c| { 102 + // if (false) { 103 + // _ = client.connect(if (c.len > 0) c else "Hello darkness my old friend") catch |err| switch (err) { 104 + // error.WouldBlock => null, 105 + // else => return err, 106 + // }; 107 + // return; 108 + // } 109 + // @panic("Not implemented"); 110 + // } 111 + 112 + // return clap.helpToFile(.stderr(), clap.Help, &params, .{}); 75 113 } 76 114 77 115 fn parseDest(in: ?[]const u8) [4]u8 { ··· 90 128 91 129 const builtin = @import("builtin"); 92 130 const std = @import("std"); 93 - const DebugAllocator = std.heap.DebugAllocator(.{}); 94 131 const ArrayList = std.ArrayList; 132 + const StaticStringMap = std.StaticStringMap; 95 133 96 134 const zaprus = @import("zaprus"); 97 135 const SaprusClient = zaprus.Client; 98 136 const SaprusMessage = zaprus.Message; 99 137 const RawSocketWriter = zaprus.RawSocketWriter; 100 - const NetWriter = zaprus.NetWriter; 101 - 102 - const clap = @import("clap"); 138 + // const NetWriter = zaprus.NetWriter;
+1 -1
src/root.zig
··· 1 1 pub const Client = @import("Client.zig"); 2 2 pub const Connection = @import("Connection.zig"); 3 3 pub const RawSocketWriter = @import("RawSocketWriter.zig"); 4 - pub const NetWriter = @import("NetWriter.zig"); 4 + // pub const NetWriter = @import("NetWriter.zig"); 5 5 6 6 const msg = @import("message.zig"); 7 7