-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.asm
173 lines (152 loc) · 4.03 KB
/
decrypt.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
section .data
prompt db "Enter the file name to decrypt: ", 0
prompt_len equ $ - prompt
key db "thishouldbeuniqueasduckduckpook" ; Same XOR key for decryption
error_open db "Error: Unable to open file", 10, 0
error_open_len equ $ - error_open
error_read db "Error: Unable to read file", 10, 0
error_read_len equ $ - error_read
error_write db "Error: Unable to write file", 10, 0
error_write_len equ $ - error_write
section .bss
filename resb 256
file_buffer resb 1048576 ; 1MB buffer for file content
file_size resd 1
file_descriptor resd 1 ; Space for file descriptor
section .text
global _start
_start:
; Display prompt and get filename
mov eax, 4
mov ebx, 1
mov ecx, prompt
mov edx, prompt_len
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, filename
mov edx, 256
int 0x80
; Remove newline from filename
mov esi, filename
call remove_newline
; Open encrypted file
mov eax, 5
mov ebx, filename
mov ecx, 0 ; O_RDONLY
int 0x80
test eax, eax
js file_open_error
mov [file_descriptor], eax
; Read file content
mov eax, 3
mov ebx, [file_descriptor]
mov ecx, file_buffer
mov edx, 1048576
int 0x80
test eax, eax
js file_read_error
mov [file_size], eax
; Close input file
mov eax, 6
mov ebx, [file_descriptor]
int 0x80
; Decrypt file content using XOR
mov esi, file_buffer
xor ecx, ecx
mov ecx, [file_size]
mov edi, key
xor_loop:
mov al, [esi]
xor al, [edi]
mov [esi], al
inc esi
inc edi
cmp byte [edi], 0
jnz xor_loop
loop xor_loop
; Open output file (remove ".enc" from filename if present)
mov esi, filename
call remove_enc
mov eax, 5
mov ebx, filename
mov ecx, 0x42 ; O_WRONLY | O_CREAT | O_TRUNC
mov edx, 0644 ; Permissions
int 0x80
test eax, eax
js file_open_error
mov [file_descriptor], eax
; Write decrypted content
mov eax, 4
mov ebx, [file_descriptor]
mov ecx, file_buffer
mov edx, [file_size]
int 0x80
test eax, eax
js file_write_error
; Close output file
mov eax, 6
mov ebx, [file_descriptor]
int 0x80
; Exit program
mov eax, 1
xor ebx, ebx
int 0x80
file_open_error:
mov eax, 4
mov ebx, 1
mov ecx, error_open
mov edx, error_open_len
int 0x80
jmp exit_program
file_read_error:
mov eax, 4
mov ebx, 1
mov ecx, error_read
mov edx, error_read_len
int 0x80
jmp exit_program
file_write_error:
mov eax, 4
mov ebx, 1
mov ecx, error_write
mov edx, error_write_len
int 0x80
exit_program:
mov eax, 1
mov ebx, 0
int 0x80
remove_newline:
mov ecx, 256
.loop:
mov al, [esi]
cmp al, 10
je .found
inc esi
dec ecx
jnz .loop
ret
.found:
mov byte [esi], 0
ret
remove_enc:
mov edi, esi ; EDI points to the filename
mov ecx, 256 ; Set a counter for the filename length
.find_end:
mov al, [edi] ; Load the current character
cmp al, 0 ; Check if it's the null terminator
je .check_enc ; If yes, jump to check for ".enc"
inc edi ; Move to the next character
dec ecx ; Decrease the counter
jnz .find_end ; Repeat until end of string or counter is exhausted
.check_enc:
sub edi, 4 ; Move back four characters to check for ".enc"
mov ecx, edi ; Save the position to restore if no match
mov eax, [edi] ; Load the last four bytes of the filename
cmp eax, 0x636e652e ; Compare with ".enc" (".enc" in reverse order because of little-endian)
jne .no_enc ; If not equal, jump to no_enc
mov byte [edi], 0 ; If ".enc" found, remove it by setting null terminator
ret
.no_enc:
mov edi, ecx ; Restore the original position
ret ; Return without modifying the filename