-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathifconfig.zig
More file actions
273 lines (238 loc) · 10.4 KB
/
Copy pathifconfig.zig
File metadata and controls
273 lines (238 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
///name: "ifconfig"
///description: "Displays the status of the currently active network interfaces; Manipulates current state of the device (euid = 0 or CAP_NET_ADMIN is required for that)"
///author: "Z-Labs"
///tags: ['linux','TA0007', 'T1016','z-labs']
///category: 'SAL-BOF'
///OS: 'Linux'
///sources:
/// - 'https://raw.githubusercontent.com/The-Z-Labs/bof-launcher/main/bofs/src/net-tools/ifconfig.zig'
///arguments:
///- name: User
/// desc: "Prints user and group information for this user"
/// type: string
/// required: false
///examples: '
/// ifconfig
/// ifconfig eth0 down
/// ifconfig eth0 promisc
/// ifconfig eth0 -promisc
///'
// TODO:
// get MTU value
const std = @import("std");
const c = @import("std").c;
const beacon = @import("bof_api").beacon;
const posix = @import("bof_api").posix;
pub const SIOCGIFFLAGS = 0x8913;
pub const SIOCSIFFLAGS = 0x8914;
// https://github.com/ziglang/zig/blob/a2b834e8c7152f70d71c71107db40b9182909647/lib/libc/include/generic-glibc/netdb.h
const NI_MAXHOST = 1025;
const NI_NUMERICHOST = 1;
// https://github.com/ziglang/zig/blob/a2b834e8c7152f70d71c71107db40b9182909647/lib/libc/musl/include/net/if.h
pub const IFF_UP = 0x1;
pub const IFF_BROADCAST = 0x2;
pub const IFF_DEBUG = 0x4;
pub const IFF_LOOPBACK = 0x8;
pub const IFF_POINTOPOINT = 0x10;
pub const IFF_NOTRAILERS = 0x20;
pub const IFF_RUNNING = 0x40;
pub const IFF_NOARP = 0x80;
pub const IFF_PROMISC = 0x100;
pub const IFF_ALLMULTI = 0x200;
pub const IFF_MASTER = 0x400;
pub const IFF_SLAVE = 0x800;
pub const IFF_MULTICAST = 0x1000;
pub const IFF_PORTSEL = 0x2000;
pub const IFF_AUTOMEDIA = 0x4000;
pub const IFF_DYNAMIC = 0x8000;
pub const IFF_LOWER_UP = 0x10000;
pub const IFF_DORMANT = 0x20000;
pub const IFF_ECHO = 0x40000;
// https://man7.org/linux/man-pages/man3/getifaddrs.3.html
pub const ifaddrs = extern struct {
ifa_next: ?*ifaddrs,
ifa_name: ?[*:0]u8,
ifa_flags: u32,
ifa_addr: ?*std.posix.sockaddr,
ifa_netmask: ?*std.posix.sockaddr,
ifa_ifu: extern union {
ifu_broadaddr: ?*std.posix.sockaddr,
ifu_dstaddr: ?*std.posix.sockaddr,
},
ifa_data: ?*anyopaque,
};
pub extern fn getifaddrs(ifap: **ifaddrs) callconv(.c) i32;
pub extern fn freeifaddrs(ifap: *ifaddrs) callconv(.c) void;
// https://man7.org/linux/man-pages/man3/getnameinfo.3.html
pub extern fn getnameinfo(
addr: *std.posix.sockaddr,
addrlen: c.socklen_t,
noalias host: ?[*]u8,
hostlen: c.socklen_t,
noalias serv: ?[*]u8,
servlen: c.socklen_t,
flags: u32,
) callconv(.c) c.EAI;
// https://github.com/ziglang/zig/blob/1b90888f576b4863f4a61213a9ca32b97aa57859/lib/libc/include/generic-glibc/netpacket/packet.h#L22
// https://man7.org/linux/man-pages/man7/packet.7.html
pub const sockaddr_ll = extern struct {
sll_family: u16,
sll_protocol: u16,
sll_ifindex: i32,
sll_hatype: i16,
sll_pkttype: u8,
sll_halen: u8,
sll_addr: [8]u8,
};
fn flagsDisplay(flags: u32) void {
const printf = beacon.printf;
_ = printf(.output, "flags=%u<", flags);
if (flags & IFF_UP != 0) {
_ = printf(.output, "UP");
} else _ = printf(.output, "DOWN");
if (flags & IFF_BROADCAST != 0)
_ = printf(.output, ",BROADCAST");
if (flags & IFF_DEBUG != 0)
_ = printf(.output, ",DEBUG");
if (flags & IFF_LOOPBACK != 0)
_ = printf(.output, ",LOOPBACK");
if (flags & IFF_POINTOPOINT != 0)
_ = printf(.output, ",POINT-TO-POINT");
if (flags & IFF_RUNNING != 0)
_ = printf(.output, ",RUNNING");
if (flags & IFF_NOARP != 0)
_ = printf(.output, ",NOARP");
if (flags & IFF_PROMISC != 0)
_ = printf(.output, ",PROMISC");
if (flags & IFF_NOTRAILERS != 0)
_ = printf(.output, ",NOTRAILERS");
if (flags & IFF_ALLMULTI != 0)
_ = printf(.output, ",ALLMULTI");
if (flags & IFF_MASTER != 0)
_ = printf(.output, ",MASTER");
if (flags & IFF_SLAVE != 0)
_ = printf(.output, ",SLAVE");
if (flags & IFF_MULTICAST != 0)
_ = printf(.output, ",MULTICAST");
_ = printf(.output, ">");
}
fn flagsParseOption(flags: std.os.linux.IFF, opt: []u8) std.os.linux.IFF {
var ret_flags: std.os.linux.IFF = flags;
if (std.mem.eql(u8, opt, "up"))
ret_flags.UP = true;
if (std.mem.eql(u8, opt, "down"))
ret_flags.UP = false;
if (std.mem.eql(u8, opt, "promisc"))
ret_flags.PROMISC = true;
if (std.mem.eql(u8, opt, "-promisc"))
ret_flags.PROMISC = false;
return ret_flags;
}
pub export fn go(adata: ?[*]u8, alen: i32) callconv(.c) u8 {
@import("bof_api").init(adata, alen, .{});
const allocator = std.heap.page_allocator;
const printf = beacon.printf;
// argument was provided parse it
// https://man7.org/linux/man-pages/man7/netdevice.7.html
// TODO: check also for CAP_NET_ADMIN
if (alen > 0 and posix.geteuid() == 0) {
var parser = beacon.datap{};
beacon.dataParse(&parser, adata, alen);
var if_name_len: i32 = 0;
const if_name_ptr = beacon.dataExtract(&parser, &if_name_len);
const if_name = if_name_ptr.?[0..@intCast(if_name_len - 1)];
var opt_len: i32 = 0;
const opt_ptr = beacon.dataExtract(&parser, &opt_len);
const opt = opt_ptr.?[0..@intCast(opt_len - 1)];
const sockfd = std.posix.socket(std.posix.AF.INET, std.posix.SOCK.DGRAM | std.posix.SOCK.CLOEXEC, 0) catch unreachable;
defer std.posix.close(sockfd);
var ifr: std.os.linux.ifreq = undefined;
@memcpy(ifr.ifrn.name[0..if_name.len], if_name);
ifr.ifrn.name[if_name.len] = 0;
// get current flag state
_ = std.os.linux.ioctl(sockfd, SIOCGIFFLAGS, @intFromPtr(&ifr));
ifr.ifru.flags = flagsParseOption(ifr.ifru.flags, opt);
// set new flag state
_ = std.os.linux.ioctl(sockfd, SIOCSIFFLAGS, @intFromPtr(&ifr));
return 0;
}
var list: *ifaddrs = undefined;
if (getifaddrs(&list) == -1) {
_ = printf(.output, "getifaddrs failed. Aborted.\n");
return 1;
}
var iter: ?*ifaddrs = list;
// add all identified interfaces (distincted by name) to the interfaces collection
var interfaces = std.array_list.Managed([]const u8).init(allocator);
defer interfaces.deinit();
outer: while (iter != null) : (iter = iter.?.ifa_next) {
const cur_iface_name = std.mem.sliceTo(iter.?.ifa_name.?, 0);
// check if given interface was already added
for (interfaces.items) |iface| {
if (std.mem.eql(u8, iface, cur_iface_name))
continue :outer;
} else interfaces.append(cur_iface_name) catch unreachable;
}
// iterate over each interface name and print its statistics
for (interfaces.items) |iface| {
iter = list;
while (iter != null) : (iter = iter.?.ifa_next) {
var host = [_]u8{0} ** NI_MAXHOST;
var netmask = [_]u8{0} ** NI_MAXHOST;
var aux = [_]u8{0} ** NI_MAXHOST;
const family = iter.?.ifa_addr.?.family;
const cur_iface_name = std.mem.sliceTo(iter.?.ifa_name.?, 0);
const flags = iter.?.ifa_flags;
if (std.mem.eql(u8, iface, cur_iface_name)) {
if (family == std.os.linux.AF.INET) {
_ = getnameinfo(iter.?.ifa_addr.?, @sizeOf(std.posix.sockaddr.in), &host, NI_MAXHOST, null, 0, NI_NUMERICHOST);
_ = getnameinfo(iter.?.ifa_netmask.?, @sizeOf(std.posix.sockaddr.in), &netmask, NI_MAXHOST, null, 0, NI_NUMERICHOST);
if (flags & IFF_BROADCAST != 0) {
_ = getnameinfo(iter.?.ifa_ifu.ifu_broadaddr.?, @sizeOf(std.posix.sockaddr.in), &aux, NI_MAXHOST, null, 0, NI_NUMERICHOST);
_ = printf(.output, "\tinet %s netmask=%s broadcast=%s\n", &host, &netmask, &aux);
} else {
_ = getnameinfo(iter.?.ifa_ifu.ifu_dstaddr.?, @sizeOf(std.posix.sockaddr.in), &aux, NI_MAXHOST, null, 0, NI_NUMERICHOST);
_ = printf(.output, "\tinet %s netmask=%s point2point=%s\n", &host, &netmask, &aux);
}
}
if (family == std.os.linux.AF.INET6) {
_ = getnameinfo(iter.?.ifa_addr.?, @sizeOf(std.posix.sockaddr.in6), &host, NI_MAXHOST, null, 0, NI_NUMERICHOST);
_ = getnameinfo(iter.?.ifa_netmask.?, @sizeOf(std.posix.sockaddr.in6), &netmask, NI_MAXHOST, null, 0, NI_NUMERICHOST);
_ = printf(.output, "\tinet %s netmask=%s\n", &host, &netmask);
}
if (family == std.os.linux.AF.PACKET) {
_ = printf(.output, "%s: ", iface.ptr);
flagsDisplay(flags);
_ = printf(.output, "\n");
// display HW address
if (!std.mem.eql(u8, iface, "lo")) {
if (iter.?.ifa_addr) |addr| {
const s = @as(*sockaddr_ll, @ptrCast(@alignCast(addr)));
var i: u32 = 0;
_ = printf(.output, "\tether ");
while (i < s.sll_halen) : (i += 1) {
_ = printf(.output, "%x", s.sll_addr[i]);
if (i + 1 != s.sll_halen) {
_ = printf(.output, ":");
} else {
_ = printf(.output, "\n");
}
}
}
}
if (iter.?.ifa_data != null) {
const stats = @as(*std.os.linux.rtnl_link_stats, @ptrCast(@alignCast(iter.?.ifa_data)));
_ = printf(.output, "\tRX packets %d bytes %d\n", stats.rx_packets, stats.rx_bytes);
_ = printf(.output, "\tRX errors %d dropped %d overruns %d frame %d\n", stats.rx_errors, stats.rx_dropped, stats.rx_fifo_errors, stats.rx_frame_errors);
_ = printf(.output, "\tTX packets %d bytes %d\n", stats.tx_packets, stats.tx_bytes);
_ = printf(.output, "\tTX errors %d dropped %d overruns %d carrier %d collisions %d\n", stats.tx_errors, stats.tx_dropped, stats.tx_fifo_errors, stats.tx_carrier_errors, stats.collisions);
}
}
}
//std.debug.print("{any}\n\n\n", .{iter.?.*});
}
_ = printf(.output, "\n");
}
freeifaddrs(list);
return 0;
}