-
Notifications
You must be signed in to change notification settings - Fork 0
/
threatbuttfuck.c
263 lines (227 loc) · 5.78 KB
/
threatbuttfuck.c
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
/*
* threatbuttfuck
*
* basically brainfuck, except all herpiderpified for your apt-y threat
* derfence in derpth (from China and that other one next to it)
*
* copyright (c) 2016 threatbutt
*
* licensed under the Threatbutt advanced Enterprise License plz don't download
* it off butttorrent
*/
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#define INCPTR "pewpewpew"
#define DECPTR "pewpewputin"
#define INCBYTE "cyber"
#define DECBYTE "derfence"
#define OUTPUT "herp"
#define INPUT "derp"
#define JMPFWD "frontdoor"
#define JMPBACK "backdoor"
#define NUM_CELLS 30000
typedef enum {
end = 0,
incptr,
decptr,
incbyte,
decbyte,
output,
input,
jmpfwd,
jmpback
} cmd_t;
typedef uint8_t cell_t;
static const char *const err_msgs[] = {
"probably Heartbutt",
"check your server blogs",
"plz renew your threatbutt corporate license",
"blaming ROWHAMMER horse",
"possible threaty threats"
};
static noreturn void usage(void) {
fputs("Usage: threatbuttfuck infile\n", stderr);
exit(2);
}
static noreturn void error(void)
{
time_t seed;
size_t i;
if ((seed = time(NULL)) != -1) {
srand((unsigned int)seed);
}
i = (unsigned int)rand() % (sizeof (err_msgs) / sizeof (*err_msgs));
fprintf(stderr, "Error detected, %s\n", err_msgs[i]);
exit(EXIT_FAILURE);
}
static const cmd_t *matching_bracket(const cmd_t *cmd)
{
ptrdiff_t direction = *cmd == jmpfwd ? 1 : -1;
int brackets = (int)direction;
do {
cmd += direction;
switch (*cmd) {
case jmpfwd:
++brackets;
break;
case jmpback:
--brackets;
break;
default:
break;
}
} while (brackets != 0);
return cmd;
}
static void run(const cmd_t *cmds, cell_t *cells)
{
const cmd_t *cur = cmds;
cell_t *ptr = cells;
int ret;
// cyber threat protected loop
while (*cur != end) {
switch (*cur) {
case incptr:
++ptr;
break;
case decptr:
--ptr;
break;
case incbyte:
++(*ptr);
break;
case decbyte:
--(*ptr);
break;
case output:
if (putchar(*ptr) == EOF) {
error();
}
break;
case input:
ret = getchar();
if (ret == EOF && ferror(stdin)) {
error();
} else if (ret == EOF) {
// pass
} else {
*ptr = (cell_t)ret;
}
break;
case jmpfwd:
if (*ptr == 0) {
cur = matching_bracket(cur);
}
break;
case jmpback:
if (*ptr != 0) {
cur = matching_bracket(cur);
}
break;
case end:
break;
}
++cur;
}
return;
}
static cmd_t *tokenize(const char *script)
{
size_t max_len;
cmd_t *cmds;
const char *script_cur;
cmd_t *cmds_cur;
max_len = strlen(script);
if ((cmds = malloc(max_len + 1)) == NULL) {
error();
}
script_cur = script;
cmds_cur = cmds;
while (script_cur < script + strlen(script)) {
if (strncmp(script_cur, INCPTR, strlen(INCPTR)) == 0) {
script_cur += strlen(INCPTR);
*cmds_cur++ = incptr;
} else if (strncmp(script_cur, DECPTR, strlen(DECPTR)) == 0) {
script_cur += strlen(DECPTR);
*cmds_cur++ = decptr;
} else if (strncmp(script_cur, INCBYTE, strlen(INCBYTE)) == 0) {
script_cur += strlen(INCBYTE);
*cmds_cur++ = incbyte;
} else if (strncmp(script_cur, DECBYTE, strlen(DECBYTE)) == 0) {
script_cur += strlen(DECBYTE);
*cmds_cur++ = decbyte;
} else if (strncmp(script_cur, OUTPUT, strlen(OUTPUT)) == 0) {
script_cur += strlen(OUTPUT);
*cmds_cur++ = output;
} else if (strncmp(script_cur, INPUT, strlen(INPUT)) == 0) {
script_cur += strlen(INPUT);
*cmds_cur++ = input;
} else if (strncmp(script_cur, JMPFWD, strlen(JMPFWD)) == 0) {
script_cur += strlen(JMPFWD);
*cmds_cur++ = jmpfwd;
} else if (strncmp(script_cur, JMPBACK, strlen(JMPBACK)) == 0) {
script_cur += strlen(JMPBACK);
*cmds_cur++ = jmpback;
} else {
script_cur += 1;
}
}
*cmds_cur = end;
return cmds;
}
static char *slurp(const char *fn)
{
FILE *fp;
int fd;
struct stat stats;
char *script;
size_t read_len;
if ((fp = fopen(fn, "r")) == NULL) {
error();
}
if ((fd = fileno(fp)) == -1) {
error();
}
if (fstat(fd, &stats) == -1) {
error();
}
if ((script = malloc((size_t)stats.st_size + 1)) == NULL) {
error();
}
read_len = fread(script, sizeof (char), (size_t)stats.st_size, fp);
if (ferror(fp)) {
error();
}
script[read_len] = '\0';
if (fclose(fp) == EOF) {
error();
}
return script;
}
int main(int argc, char **argv)
{
char *script;
cmd_t *cmds;
cell_t *cells;
if (argc != 2) {
usage();
}
script = slurp(argv[1]);
cmds = tokenize(script);
if ((cells = malloc(NUM_CELLS)) == NULL) {
error();
}
memset(cells, 0, NUM_CELLS);
run(cmds, cells);
free(script);
free(cmds);
return 0;
}