-
Notifications
You must be signed in to change notification settings - Fork 16
/
id.zig
133 lines (109 loc) · 3.67 KB
/
id.zig
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
///name: id
///description: "Print user and group information for each specified USER, or (when USER omitted) for the current process"
///author: Z-Labs
///tags: ['linux','host-recon','z-labs']
///OS: linux
///sources:
/// - 'https://raw.githubusercontent.com/The-Z-Labs/bof-launcher/main/bofs/src/coreutils/id.zig'
///examples: '
/// id
/// id root
///'
///arguments:
///- name: user
/// desc: "Prints user and group information for this user"
/// type: string
/// required: false
const std = @import("std");
const os = std.posix;
const c = std.c;
const mem = std.mem;
const beacon = @import("bof_api").beacon;
const posix = @import("bof_api").posix;
// https://man7.org/linux/man-pages/man3/getgrouplist.3.html
pub extern fn getgrouplist(user: [*:0]const u8, group: c.gid_t, groups: [*]c.gid_t, ngroups: *i32) callconv(.C) i32;
const NGROUPS_MAX = 32;
pub export fn go(args: ?[*]u8, args_len: i32) callconv(.C) u8 {
var ruid: c.uid_t = undefined;
var rgid: c.gid_t = undefined;
var euid: c.gid_t = undefined;
var egid: c.gid_t = undefined;
var ngroups: i32 = NGROUPS_MAX;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var groups_gids: [NGROUPS_MAX]c.gid_t = undefined;
var groups_names = std.ArrayList([]const u8).init(allocator);
defer groups_names.deinit();
var pwd: ?*c.passwd = null;
var grp: ?*posix.group = null;
// no username provided
if (args_len == 0) {
ruid = posix.getuid();
rgid = posix.getgid();
euid = posix.geteuid();
egid = posix.getegid();
pwd = posix.getpwuid(ruid);
grp = posix.getgrgid(rgid);
ngroups = posix.getgroups(NGROUPS_MAX, &groups_gids);
} else {
var parser = beacon.datap{};
beacon.dataParse(&parser, args, args_len);
const name = beacon.dataExtract(&parser, null);
if (name) |n| {
pwd = posix.getpwnam(@as([*:0]u8, @ptrCast(n)));
if (pwd) |p| {
ruid = p.pw_uid;
rgid = p.pw_gid;
if (getgrouplist(p.pw_name.?, p.pw_gid, &groups_gids, &ngroups) == -1)
return 1;
} else return 1;
} else return 1;
}
if (ngroups != -1) {
var i: usize = 0;
while (i < ngroups) {
const g = posix.getgrgid(groups_gids[i]);
const name = mem.Allocator.dupeZ(allocator, u8, mem.sliceTo(g.?.gr_name, 0)) catch return 1;
groups_names.append(name) catch return 1;
i = i + 1;
}
}
if (pwd == null) {
return 1;
}
_ = beacon.printf(0, "uid=%d", ruid);
if (pwd) |p|
_ = beacon.printf(0, "(%s)", p.pw_name);
_ = beacon.printf(0, " gid=%d", rgid);
grp = posix.getgrgid(rgid);
if (grp) |gr|
_ = beacon.printf(0, "(%s)", gr.gr_name);
if (args_len == 0) {
if (euid != ruid) {
_ = beacon.printf(0, " euid=%d", euid);
pwd = posix.getpwuid(euid);
if (pwd) |p| {
_ = beacon.printf(0, "(%s)", p.pw_name);
}
}
if (egid != rgid) {
_ = beacon.printf(0, " egid=%d", egid);
grp = posix.getgrgid(egid);
if (grp) |g| {
_ = beacon.printf(0, "(%s)", g.gr_name);
}
}
}
_ = beacon.printf(0, " groups=");
var i: usize = 0;
for (groups_names.items) |name| {
_ = beacon.printf(0, "%d(%s)", groups_gids[i], name.ptr);
if (i != groups_names.items.len - 1)
_ = beacon.printf(0, ",");
i = i + 1;
allocator.free(name);
}
_ = beacon.printf(0, "\n");
return 0;
}