Jump to content
Gonzalez

[MASM] RC4 Encryption

Recommended Posts

Posted

.386p
.model flat, stdcall
option casemap:none ; Case sensitive
include \Masm32\include\windows.inc
include \Masm32\include\kernel32.inc
include \Masm32\include\user32.inc
includelib \Masm32\lib\kernel32.lib
includelib \Masm32\lib\user32.lib
EnDeCryptfile proto :DWORD
EnDeCryptfile2 proto :DWORD,:DWORD
Rc4_setkey proto :DWORD,:DWORD
Rc4_crypt proto :DWORD, :DWORD

CTEXT MACRO text:VARARG
LOCAL TxtName
.data
TxtName BYTE text,0
.code
EXITM <ADDR TxtName>
ENDM

.data
mykey db "omgwtfjusthappenedtherethen",0


.data?
data db 1024 dup(?) ; bytes read/write, can be turned up
rc4keytable db 256 dup (?)
HoldBuf db 256 dup(?)
hFileRead dd ?
SizeWritten dd ?
SizeWritten2 dd ?
hFileRead2 dd ?

.code
start:


invoke EnDeCryptfile,CTEXT("RC4d.exe") ; encrypts RC4.exe
;invoke EnDeCryptfile,CTEXT("RC4d.exe"); decrypt RC4.exe

;file test 2
invoke EnDeCryptfile2,CTEXT("original.exe"),CTEXT("ENCRYPTED")
invoke EnDeCryptfile2,CTEXT("ENCRYPTED"),CTEXT("DENCRYPTED.exe")




invoke ExitProcess,0

;writes encrypted/decrypted data back into same file
EnDeCryptfile proc inputfile:DWORD
invoke CreateFile,inputfile,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
cmp eax,INVALID_HANDLE_VALUE
je @F
mov hFileRead,eax
.while TRUE
invoke ReadFile,hFileRead,addr data,1,ADDR SizeWritten,NULL
.break .if eax && SizeWritten == 0
invoke lstrlen,addr mykey
invoke Rc4_setkey,addr mykey,eax
invoke Rc4_crypt,addr data,1
invoke SetFilePointer,hFileRead,-1,0,FILE_CURRENT
invoke WriteFile,hFileRead,addr data,1,ADDR SizeWritten2,NULL
.endw
invoke CloseHandle,hFileRead
@@:
ret
EnDeCryptfile endp


;writes en/decrypted file to external file, reads and writes 1024 bytes at a time but you can read as many as you like if you change the buffer size
EnDeCryptfile2 proc inputfile:DWORD,savepath:DWORD
invoke CreateFile,inputfile,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
cmp eax,INVALID_HANDLE_VALUE
je @F
mov hFileRead,eax
invoke CreateFile,savepath,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL
mov hFileRead2,eax
.while TRUE
invoke ReadFile,hFileRead,addr data,1024,ADDR SizeWritten,NULL
.break .if eax && SizeWritten == 0
invoke lstrlen,addr mykey
invoke Rc4_setkey,addr mykey,eax
invoke Rc4_crypt,addr data,1024
invoke WriteFile,hFileRead2,addr data,1024,ADDR SizeWritten2,NULL
.endw
invoke CloseHandle,hFileRead
invoke CloseHandle,hFileRead2
@@:
ret
EnDeCryptfile2 endp





Rc4_setkey proc Pass:DWORD, LenPass:DWORD
pushad

mov eax, 0FFFEFDFCh
mov ecx, 256/4
Init_rc4keytable:
mov dword ptr [rc4keytable+4*ecx-4], eax
sub eax, 04040404h
dec ecx
jnz Init_rc4keytable

xor eax, eax
mov edi, Pass

Key_return:
xor ebx, ebx
mov esi ,LenPass
jmp New_key

Key_loop:
inc bl
dec esi
jz Key_return

New_key:
mov dl, byte ptr [rc4keytable+ecx]
add al, byte ptr [edi+ebx]
add al, dl
mov dh, byte ptr [rc4keytable+eax]
mov byte ptr [rc4keytable+ecx], dh
mov byte ptr [rc4keytable+eax], dl
inc cl
jnz Key_loop

popad
ret
Rc4_setkey endp

Rc4_crypt proc iData:DWORD, LenData:DWORD
pushad
mov edi, LenData
mov esi, iData
test edi, edi
jz Rc4_enc_exit

xor eax, eax
xor edx, edx
xor ecx, ecx
xor ebx, ebx

Rc4_enc_loop:
inc bl
mov dl, byte ptr [rc4keytable+ebx]
add al, dl
mov cl, byte ptr [rc4keytable+eax]
mov byte ptr [rc4keytable+ebx], cl
mov byte ptr [rc4keytable+eax], dl
add cl, dl
mov cl, byte ptr [rc4keytable+ecx]
xor byte ptr [esi], cl
inc esi
dec edi
jnz Rc4_enc_loop

xor eax, eax
mov edi, offset rc4keytable
mov ecx, 256/4
cld
rep stosd

Rc4_enc_exit:
popad
ret
Rc4_crypt endp

end start

eNj0Y!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...