-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathlskmod.zig
More file actions
62 lines (52 loc) · 1.88 KB
/
lskmod.zig
File metadata and controls
62 lines (52 loc) · 1.88 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
///name: lskmod
///description: "Print the currently loaded kernel modules"
///author: Z-Labs
///tags: ['linux','host-recon','z-labs']
///OS: linux
///sources:
/// - 'https://raw.githubusercontent.com/The-Z-Labs/bof-launcher/main/bofs/src/lskmod.zig'
///examples: '
/// lskmod
///'
///errors:
///- name: FaileOpenFailure
/// code: 0x1
/// message: "Failed to open '/proc/modules' file"
///- name: UnknownError
/// code: 0x2
/// message: "Unknown error"
const std = @import("std");
const beacon = @import("bof_api").beacon;
const posix = @import("bof_api").posix;
fn getModulesList(allocator: std.mem.Allocator) !u8 {
const fd = try std.posix.openZ("/proc/modules", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, 0);
defer std.posix.close(fd);
if (fd == -1)
return 1;
const file: std.fs.File = .{ .handle = fd };
const file_content = try file.readToEndAlloc(allocator, std.math.maxInt(u32));
std.mem.replaceScalar(u8, file_content, '\n', 0);
var line_iter = std.mem.splitScalar(u8, file_content, 0);
while (line_iter.next()) |line| {
var iter = std.mem.tokenizeScalar(u8, line, ' ');
const mod_name = iter.next() orelse return error.BadData;
const mod_size = iter.next() orelse return error.BadData;
const mod_usage = iter.next() orelse return error.BadData;
const mod_usedby = iter.next() orelse return error.BadData;
const mod_entry = try std.mem.joinZ(allocator, " ", &.{
mod_name,
mod_size,
mod_usage,
mod_usedby,
});
_ = beacon.printf(.output, "%s\n", mod_entry.ptr);
allocator.free(mod_entry);
}
allocator.free(file_content);
return 0;
}
pub export fn go(adata: ?[*]u8, alen: i32) callconv(.c) u8 {
@import("bof_api").init(adata, alen, .{});
const allocator = std.heap.page_allocator;
return getModulesList(allocator) catch 2;
}