aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gq/malwarefight/nosession/linux/bwrap/BubblewrapBuilder.java
blob: 6a5b9a5eb277e3b80778c81eb2750e3a50a309b8 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package gq.malwarefight.nosession.linux.bwrap;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import gq.malwarefight.nosession.linux.libc.Libc;
import gq.malwarefight.nosession.utils.Utils;
import org.apache.logging.log4j.LogManager;

import javax.net.ssl.HttpsURLConnection;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;

@SuppressWarnings("unused") // library class
public class BubblewrapBuilder {
    final ArrayList<String> bubblewrapArgs = new ArrayList<>();
    final ArrayList<String> programArgs = new ArrayList<>();
    String command = "";

    public BubblewrapBuilder addArgsFromFD(int fd) {
        bubblewrapArgs.add("--args");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder unshareAll() {
        bubblewrapArgs.add("--unshare-all");
        return this;
    }

    public BubblewrapBuilder shareNet() {
        bubblewrapArgs.add("--share-net");
        return this;
    }

    public BubblewrapBuilder unshareUser() {
        return unshareUser(false);
    }

    public BubblewrapBuilder unshareUser(boolean try_) {
        bubblewrapArgs.add(try_ ? "--unshare-user-try" : "--unshare-user");
        return this;
    }

    public BubblewrapBuilder unshareIPC() {
        bubblewrapArgs.add("--unshare-ipc");
        return this;
    }

    public BubblewrapBuilder unsharePID() {
        bubblewrapArgs.add("--unshare-pid");
        return this;
    }

    public BubblewrapBuilder unshareNet() {
        bubblewrapArgs.add("--unshare-net");
        return this;
    }

    public BubblewrapBuilder unshareUTS() {
        bubblewrapArgs.add("--unshare-uts");
        return this;
    }

    public BubblewrapBuilder unshareCGroup() {
        return unshareCGroup(false);
    }

    public BubblewrapBuilder unshareCGroup(boolean try_) {
        bubblewrapArgs.add(try_ ? "--unshare-cgroup-try" : "--unshare-cgroup");
        return this;
    }

    public BubblewrapBuilder setUserNS(int fd) {
        return setUserNS(fd, false);
    }

    public BubblewrapBuilder setUserNS(int fd, boolean afterSetup) {
        bubblewrapArgs.add(afterSetup ? "--userns2" : "--userns");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder disableUserNS() {
        bubblewrapArgs.add("--disable-userns");
        return this;
    }

    public BubblewrapBuilder assertUserNSDisabled() {
        bubblewrapArgs.add("--assert-userns-disabled");
        return this;
    }

    public BubblewrapBuilder setPIDNS(int fd) {
        bubblewrapArgs.add("--pidns");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder setUID(int uid) {
        bubblewrapArgs.add("--uid");
        bubblewrapArgs.add(String.valueOf(uid));
        return this;
    }

    public BubblewrapBuilder setGID(int gid) {
        bubblewrapArgs.add("--gid");
        bubblewrapArgs.add(String.valueOf(gid));
        return this;
    }

    public BubblewrapBuilder setHostname(String name) {
        bubblewrapArgs.add("--hostname");
        bubblewrapArgs.add(name);
        return this;
    }

    public BubblewrapBuilder setChdir(String dir) {
        bubblewrapArgs.add("--chdir");
        bubblewrapArgs.add(dir);
        return this;
    }

    public BubblewrapBuilder clearEnv() {
        bubblewrapArgs.add("--clearenv");
        return this;
    }

    public BubblewrapBuilder setEnv(String key, String value) {
        bubblewrapArgs.add("--setenv");
        bubblewrapArgs.add(key);
        bubblewrapArgs.add(value);
        return this;
    }

    public BubblewrapBuilder unSetEnv(String key) {
        bubblewrapArgs.add("--unsetenv");
        bubblewrapArgs.add(key);
        return this;
    }

    public BubblewrapBuilder setLockFile(String file) {
        bubblewrapArgs.add("--lock-file");
        bubblewrapArgs.add(file);
        return this;
    }

    public BubblewrapBuilder syncFD(int fd) {
        bubblewrapArgs.add("--sync-fd");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder bind(String src, String dest) {
        return bind(src, dest, false, false, false);
    }

    public BubblewrapBuilder bindTry(String src, String dest) {
        return bind(src, dest, true, false, false);
    }

    public BubblewrapBuilder devBind(String src, String dest) {
        return bind(src, dest, false, true, false);
    }

    public BubblewrapBuilder devBindTry(String src, String dest) {
        return bind(src, dest, true, true, false);
    }

    public BubblewrapBuilder readOnlyBind(String src, String dest) {
        return bind(src, dest, false, false, true);
    }

    public BubblewrapBuilder readOnlyBindTry(String src, String dest) {
        return bind(src, dest, true, false, true);
    }

    private BubblewrapBuilder bind(String src, String dest, boolean try_, boolean dev, boolean ro) {
        if (ro && dev) {
            throw new IllegalArgumentException("Cannot bind both read-only and dev");
        }
        StringBuilder argument = new StringBuilder();
        argument.append("--");
        if (dev) {
            argument.append("dev-");
        } else if (ro) {
            argument.append("ro-");
        }
        argument.append("bind");
        if (try_) {
            argument.append("-try");
        }
        bubblewrapArgs.add(argument.toString());
        bubblewrapArgs.add(src);
        bubblewrapArgs.add(dest);
        return this;
    }

    public BubblewrapBuilder readOnlyRemount(String dest) {
        bubblewrapArgs.add("--remount-ro");
        bubblewrapArgs.add(dest);
        return this;
    }

    public BubblewrapBuilder execLabel(String label) {
        bubblewrapArgs.add("--exec-label");
        bubblewrapArgs.add(label);
        return this;
    }

    public BubblewrapBuilder fileLabel(String label) {
        bubblewrapArgs.add("--file-label");
        bubblewrapArgs.add(label);
        return this;
    }

    public BubblewrapBuilder mountProc(String dest) {
        bubblewrapArgs.add("--proc");
        bubblewrapArgs.add(dest);
        return this;
    }


    public BubblewrapBuilder mountDev(String dest) {
        bubblewrapArgs.add("--dev");
        bubblewrapArgs.add(dest);
        return this;
    }

    public BubblewrapBuilder mountTmpfs(String dest) {
        bubblewrapArgs.add("--tmpfs");
        bubblewrapArgs.add(dest);
        return this;
    }

    public BubblewrapBuilder mountMqueue(String dest) {
        bubblewrapArgs.add("--mqueue");
        bubblewrapArgs.add(dest);
        return this;
    }

    public BubblewrapBuilder createDir(String dest) {
        bubblewrapArgs.add("--dir");
        bubblewrapArgs.add(dest);
        return this;
    }


    public BubblewrapBuilder copyFile(int fd, String dest) {
        bubblewrapArgs.add("--file");
        bubblewrapArgs.add(String.valueOf(fd));
        bubblewrapArgs.add(dest);
        return this;
    }


    public BubblewrapBuilder bindData(int fd, String dest) {
        return bindData(fd, dest, false);
    }


    public BubblewrapBuilder bindDataReadOnly(int fd, String dest) {
        return bindData(fd, dest, true);
    }

    private BubblewrapBuilder bindData(int fd, String dest, boolean ro) {
        bubblewrapArgs.add("--" + (ro ? "ro" : "") + "bind-data");
        bubblewrapArgs.add(String.valueOf(fd));
        bubblewrapArgs.add(dest);
        return this;
    }

    public BubblewrapBuilder addSymlink(String src, String target) {
        bubblewrapArgs.add("--symlink");
        bubblewrapArgs.add(src);
        bubblewrapArgs.add(target);
        return this;
    }

    public BubblewrapBuilder loadSeccompFilters(int fd) {
        bubblewrapArgs.add("--seccomp");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder addSeccompFilters(int fd) {
        bubblewrapArgs.add("--add-seccomp-fd");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }


    public BubblewrapBuilder blockOnFD(int fd) {
        bubblewrapArgs.add("--block-fd");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder blockOnFdUntilUserNS(int fd) {
        bubblewrapArgs.add("--userns-block-fd");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder infoFD(int fd) {
        bubblewrapArgs.add("--info-fd");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder jsonStatusFD(int fd) {
        bubblewrapArgs.add("--json-status-fd");
        bubblewrapArgs.add(String.valueOf(fd));
        return this;
    }

    public BubblewrapBuilder newSession() {
        bubblewrapArgs.add("--new-session");
        return this;
    }

    public BubblewrapBuilder dieWithParent() {
        bubblewrapArgs.add("--die-with-parent");
        return this;
    }

    public BubblewrapBuilder asPid1() {
        bubblewrapArgs.add("--as-pid-1");
        return this;
    }

    public BubblewrapBuilder capAdd(String capability) {
        bubblewrapArgs.add("--cap-add");
        bubblewrapArgs.add(capability);
        return this;
    }

    public BubblewrapBuilder capDrop(String capability) {
        bubblewrapArgs.add("--cap-drop");
        bubblewrapArgs.add(capability);
        return this;
    }

    public BubblewrapBuilder chmod(String octal, String path) {
        bubblewrapArgs.add("--chmod");
        bubblewrapArgs.add(octal);
        bubblewrapArgs.add(path);
        return this;
    }

    public BubblewrapBuilder addArgs(Iterable<String> args) {
        args.forEach(programArgs::add);
        return this;
    }

    public BubblewrapBuilder addArgs(String... args) {
        programArgs.addAll(Arrays.asList(args));
        return this;
    }

    public BubblewrapBuilder setCommand(String command) {
        this.command = command;
        return this;
    }

    private String downloadBubblewrap() throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        File f = Files.createTempFile("nosession_bwrap", "").toFile();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            Libc.unlink(f.getAbsolutePath());
        }));
        URL url = new URL("https://api.github.com/repos/pandaninjas/bubblewrap-static/releases/latest");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        String result = Utils.readString(connection.getInputStream(), null);
        JsonObject object = new JsonParser().parse(result).getAsJsonObject();
        URL downloadURL = new URL(object.get("assets").getAsJsonArray().get(0).getAsJsonObject().get("browser_download_url").getAsString());
        HttpsURLConnection download = (HttpsURLConnection) downloadURL.openConnection();
        Utils.copy(download.getInputStream(), Files.newOutputStream(f.toPath()));
        boolean success = f.setExecutable(true);
        if (!success) {
            LogManager.getLogger().error("Failed to mark file as executable. Quitting");
            Utils.shutdown();
        }
        return f.getAbsolutePath();
    }

    public ProcessBuilder build() throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        ProcessBuilder pb = new ProcessBuilder();
        String bubbleWrapPath = downloadBubblewrap();
        ArrayList<String> finalArgs = new ArrayList<>();
        finalArgs.add(bubbleWrapPath);
        finalArgs.addAll(bubblewrapArgs);
        finalArgs.add(command);
        finalArgs.addAll(programArgs);
        pb.command(finalArgs).redirectOutput(new File(System.getProperty("NOSESSION_STDOUT", System.getProperty("user.home") + "/nosession-stdout.log"))).redirectError(new File(System.getProperty("NOSESSION_STDERR", System.getProperty("user.home") + "/nosession-stderr.log")));
        return pb;
    }
}