-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrbin.c
More file actions
130 lines (111 loc) · 3.33 KB
/
crbin.c
File metadata and controls
130 lines (111 loc) · 3.33 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
/*
* Clearly Random Binary (crbin)
* Copyright (C) 2025 comedymoon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* WARNING:
* DO NOT RUN CRBIN AS ROOT.
* DO NOT RUN CRBIN WITH SUDO.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
typedef struct {
char **items;
size_t count;
size_t cap;
} list_t;
static void list_init(list_t *list) {
list->cap = 16;
list->count = 0;
list->items = malloc(list->cap * sizeof(char *));
}
static void list_add(list_t *list, const char *s) {
if (list->count == list->cap) {
list->cap *= 2;
list->items = realloc(list->items, list->cap * sizeof(char *));
}
list->items[list->count++] = strdup(s);
}
static void list_free(list_t *list) {
for (size_t i = 0; i < list->count; i++)
free(list->items[i]);
free(list->items);
}
static void scan_directory(const char *dirpath, list_t *bins, int recurse) {
DIR *d = opendir(dirpath);
if (!d) return;
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
if (ent->d_name[0] == '.' &&
(ent->d_name[1] == '\0' ||
(ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
continue;
char fullpath[4096];
snprintf(fullpath, sizeof(fullpath), "%s/%s", dirpath, ent->d_name);
struct stat st;
if (stat(fullpath, &st) != 0) continue;
if (S_ISREG(st.st_mode)) {
if (access(fullpath, X_OK) == 0)
list_add(bins, fullpath);
} else if (recurse && S_ISDIR(st.st_mode)) {
scan_directory(fullpath, bins, recurse);
}
}
closedir(d);
}
int main(int argc, char **argv) {
list_t bins;
list_init(&bins);
int recurse = 0;
const char *rec = getenv("RECURSE");
if (rec && strcmp(rec, "1") == 0)
recurse = 1;
const char *envdirs = getenv("BINDIRECTORIES");
if (envdirs && envdirs[0]) {
char *copy = strdup(envdirs);
char *dir = strtok(copy, ",");
while (dir) {
scan_directory(dir, &bins, recurse);
dir = strtok(NULL, ",");
}
free(copy);
} else {
const char *defaults[] = {
"/bin",
"/usr/bin",
"/usr/local/bin",
"/sbin",
"/usr/sbin",
"/usr/local/sbin",
NULL
};
for (int i = 0; defaults[i]; i++)
scan_directory(defaults[i], &bins, recurse);
}
if (bins.count == 0)
_exit(1);
srand(time(NULL) ^ getpid());
const char *cmd = bins.items[rand() % bins.count];
char **exec_args = malloc((argc + 1) * sizeof(char *));
exec_args[0] = (char *)cmd;
for (int i = 1; i < argc; i++)
exec_args[i] = argv[i];
exec_args[argc] = NULL;
execvp(cmd, exec_args);
_exit(1);
}