-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathls.zig
More file actions
261 lines (225 loc) · 8.53 KB
/
ls.zig
File metadata and controls
261 lines (225 loc) · 8.53 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
///name: ls
///description: "List given directory content"
///author: Z-Labs
///tags: ['windows', 'linux','host-recon','z-labs']
///OS: cross
///sources:
/// - 'https://raw.githubusercontent.com/The-Z-Labs/bof-launcher/main/bofs/src/ls.zig'
///examples: '
/// ls /
/// ls C:\Windows\System32\
///'
///arguments:
///- name: dir_path
/// desc: "path to the directory to be listed"
/// type: string
/// required: true
///errors:
///- name: AccessDenied
/// code: 0x1
/// message: "Failed to open provided file"
///- name: FileNotFound
/// code: 0x2
/// message: "File not found"
///- name: AntivirusInterference
/// code: 0x3
/// message: "Possible Antivirus Interference while opening the file"
///- name: DirNotProvided
/// code: 0x4
/// message: "No directory provided"
///- name: UnknownError
/// code: 0x5
/// message: "Unknown error"
const std = @import("std");
const linux = std.os.linux;
const bofapi = @import("bof_api");
const posix = bofapi.posix;
const beacon = bofapi.beacon;
comptime {
@import("bof_api").embedFunctionCode("memcpy");
@import("bof_api").embedFunctionCode("memset");
@import("bof_api").embedFunctionCode("memmove");
@import("bof_api").embedFunctionCode("__stackprobe__");
@import("bof_api").embedFunctionCode("__udivdi3");
@import("bof_api").embedFunctionCode("__ashldi3");
@import("bof_api").embedFunctionCode("__aeabi_uldivmod");
@import("bof_api").embedFunctionCode("__aeabi_uidivmod");
@import("bof_api").embedFunctionCode("__aeabi_uidiv");
@import("bof_api").embedFunctionCode("__aeabi_llsl");
}
// BOF-specific error codes
const BofErrors = enum(u8) {
AccessDenied = 0x1,
FileNotFound,
AntivirusInterference,
DirNotProvided,
UnknownError,
};
// RFC3339 implementation taken from:
// https://www.aolium.com/karlseguin/cf03dee6-90e1-85ac-8442-cf9e6c11602a
pub const DateTime = struct {
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
};
pub fn fromTimestamp(ts: u64) DateTime {
const SECONDS_PER_DAY = 86400;
const DAYS_PER_YEAR = 365;
const DAYS_IN_4YEARS = 1461;
const DAYS_IN_100YEARS = 36524;
const DAYS_IN_400YEARS = 146097;
const DAYS_BEFORE_EPOCH = 719468;
const seconds_since_midnight: u64 = @rem(ts, SECONDS_PER_DAY);
var day_n: u64 = DAYS_BEFORE_EPOCH + ts / SECONDS_PER_DAY;
var temp: u64 = 0;
temp = 4 * (day_n + DAYS_IN_100YEARS + 1) / DAYS_IN_400YEARS - 1;
var year: u16 = @intCast(100 * temp);
day_n -= DAYS_IN_100YEARS * temp + temp / 4;
temp = 4 * (day_n + DAYS_PER_YEAR + 1) / DAYS_IN_4YEARS - 1;
year += @intCast(temp);
day_n -= DAYS_PER_YEAR * temp + temp / 4;
var month: u8 = @intCast((5 * day_n + 2) / 153);
const day: u8 = @intCast(day_n - (@as(u64, @intCast(month)) * 153 + 2) / 5 + 1);
month += 3;
if (month > 12) {
month -= 12;
year += 1;
}
return DateTime{
.year = year,
.month = month,
.day = day,
.hour = @intCast(seconds_since_midnight / 3600),
.minute = @intCast(seconds_since_midnight % 3600 / 60),
.second = @intCast(seconds_since_midnight % 60),
};
}
pub fn toRFC3339(dt: DateTime) [20]u8 {
var buf: [20]u8 = undefined;
var w: std.Io.Writer = .fixed(buf[0..4]);
w.printInt(dt.year, 10, .lower, .{ .width = 4, .fill = '0' }) catch unreachable;
buf[4] = '-';
paddingTwoDigits(buf[5..7], dt.month);
buf[7] = '-';
paddingTwoDigits(buf[8..10], dt.day);
buf[10] = ' ';
paddingTwoDigits(buf[11..13], dt.hour);
buf[13] = ':';
paddingTwoDigits(buf[14..16], dt.minute);
buf[16] = ':';
paddingTwoDigits(buf[17..19], dt.second);
buf[19] = 0;
return buf;
}
fn paddingTwoDigits(buf: *[2]u8, value: u8) void {
switch (value) {
0 => buf.* = "00".*,
1 => buf.* = "01".*,
2 => buf.* = "02".*,
3 => buf.* = "03".*,
4 => buf.* = "04".*,
5 => buf.* = "05".*,
6 => buf.* = "06".*,
7 => buf.* = "07".*,
8 => buf.* = "08".*,
9 => buf.* = "09".*,
else => {
var w: std.Io.Writer = .fixed(buf[0..2]);
w.printInt(value, 10, .lower, .{}) catch unreachable;
},
}
}
// end of RFC3339 implementation
fn listDirContent(dir_path: [*:0]u8) !u8 {
const printf = beacon.printf;
var iter_dir = try std.fs.openDirAbsoluteZ(dir_path, .{ .iterate = true });
defer iter_dir.close();
var iter = iter_dir.iterate();
while (try iter.next()) |entry| {
// print entry type
if (entry.kind == .directory) {
_ = printf(.output, "d");
} else if (entry.kind == .sym_link) {
_ = printf(.output, "l");
} else if (entry.kind == .block_device) {
_ = printf(.output, "b");
} else if (entry.kind == .character_device) {
_ = printf(.output, "c");
} else if (entry.kind == .unix_domain_socket) {
_ = printf(.output, "u");
} else _ = printf(.output, "-");
if (@import("builtin").os.tag == .linux) {
var statx: std.os.linux.Statx = undefined;
_ = std.os.linux.statx(iter_dir.fd, @ptrCast(entry.name.ptr), std.os.linux.AT.STATX_SYNC_AS_STAT |
std.os.linux.AT.NO_AUTOMOUNT |
std.os.linux.AT.SYMLINK_NOFOLLOW, std.os.linux.STATX_MODE |
std.os.linux.STATX_UID |
std.os.linux.STATX_GID |
std.os.linux.STATX_MTIME |
std.os.linux.STATX_SIZE, &statx);
// print permissions
if ((statx.mode & std.os.linux.S.IRUSR) != 0) _ = printf(.output, "r") else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IWUSR) != 0) _ = printf(.output, "w") else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IXUSR) != 0) {
if ((statx.mode & std.os.linux.S.ISUID) != 0) {
_ = printf(.output, "s");
} else _ = printf(.output, "x");
} else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IRGRP) != 0) _ = printf(.output, "r") else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IWGRP) != 0) _ = printf(.output, "w") else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IXGRP) != 0) {
if ((statx.mode & std.os.linux.S.ISGID) != 0) {
_ = printf(.output, "s");
} else _ = printf(.output, "x");
} else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IROTH) != 0) _ = printf(.output, "r") else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.IWOTH) != 0) _ = printf(.output, "w") else _ = printf(.output, "-");
if ((statx.mode & std.os.linux.S.ISVTX) != 0) {
_ = printf(.output, "t");
} else if ((statx.mode & std.os.linux.S.IXOTH) != 0) {
_ = printf(.output, "x");
} else _ = printf(.output, "-");
// print file ownership
if (posix.getpwuid(statx.uid)) |pwd| {
_ = printf(.output, "\t%s", pwd.name);
}
if (posix.getgrgid(statx.gid)) |grp| {
_ = printf(.output, " %s", grp.gr_name);
}
// print file size
_ = printf(.output, "\t%9d", statx.size);
// print last modification time
const dt = fromTimestamp(@intCast(statx.mtime.sec));
const timeStr = toRFC3339(dt);
_ = printf(.output, " %s", &timeStr);
}
// print file name
bofapi.print(.output, "\t{s}", .{entry.name});
// additional prints (based on entry type)
if (entry.kind == .directory)
_ = printf(.output, "/");
if (entry.kind == .sym_link) {
var buf: [4096:0]u8 = undefined;
const link = try std.posix.readlinkat(iter_dir.fd, entry.name, buf[0..]);
buf[link.len] = 0;
_ = printf(.output, " -> %s", &buf);
}
// end of line
_ = printf(.output, "\n");
}
return 0;
}
pub export fn go(adata: ?[*]u8, alen: i32) callconv(.c) u8 {
@import("bof_api").init(adata, alen, .{});
var parser = beacon.datap{};
beacon.dataParse(&parser, adata, alen);
if (beacon.dataExtract(&parser, null)) |dir_path| {
return listDirContent(dir_path) catch |err| switch (err) {
error.FileNotFound => @intFromEnum(BofErrors.FileNotFound),
else => @intFromEnum(BofErrors.UnknownError),
};
} else return @intFromEnum(BofErrors.DirNotProvided);
}