	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	;: MAIL DLL by diablo2oo2                                    diablo2oo2@gmx.net :
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


.586p
.model flat,stdcall
.code

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\macros\macros.asm

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

include \masm32\include\wsock32.inc
includelib \masm32\lib\wsock32.lib

include base64.asm		;alternative base64 routine

Mys 			PROTO 	:DWORD
AllocMem		PROTO 	:DWORD
FreeMem			PROTO 	:DWORD
d2k2_FileNameOfPath	PROTO	:DWORD
SendMail		PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD

.data
buffer		db 512 dup(00)	;a little buffer
sock		dd 0
AllocedMemory	dd 0		;adress of memory
AttachMemory	dd 0		;adress of memory for the base 64 attach
FileHandle	dd 0		;base64 filehandle
FileSize	dd 0		;base64 filesize
b_read      	dd ?		; for file read
_enter		db 13,10,0

; BEGIN OF MAIL
EHLO 		db "EHLO Localhost",13 ,10
		db "AUTH LOGIN",13,10,0				;RFC 2554 login
		;+ "YW5vLm1hlciyMDA0QGuZXQ=",13,10		;username as Base64
		;+ "Mzz=",13,10,0				;password as Base64
		;---now send this part---
			
MAIL_FROM 	db "MAIL FROM: ",0	
		;+ "from@gmx.net",13,10,0
		;---now send this part---
		
RCPT_TO 	db "RCPT TO: ",0
		;+ "to@gmx.net",13,10,0
		;---now send this part---
		
_data_from	db "DATA",13,10
		db "From: ",0
		;+ "Anonym <from@gmx.net>",13,10
_data_to	db "To: ",0
		;+ "Anonym <to@gmx.net>",13,10
_subject	db "MIME-Version: 1.0",13,10
		db "Subject: ",0
		;+ "my subject",0	
_contenttype	db 13,10,'Content-Type: multipart/mixed;',13,10
		db ' boundary="my_boundary"',13,10,13,10
		db "--my_boundary",13,10
		db "Content-Type: text/plain",13,10,13,10,0
		;+ "I am the mail message"
_file		db 13,10,13,10		
		db "--my_boundary",13,10
		db "Content-Type: application/octet-stream;",13,10
		db "Content-Transfer-Encoding: Base64",13,10
		db "Content-Disposition: attachment;",13,10
		db " FileName=",0
		;+ "filename.txt",13,10,13,10,0
		;+ ...now follow Base64 Attachment
_mail_end	db 13,10,13,10,"--my_boundary--",13,10,13,10,".",13,10,"QUIT",13,10,13,10,0
		;---now send this part---
; END oF MAIL
	
.code
DllEntry proc hInstance:HINSTANCE, reason:DWORD, reserved1:DWORD
	mov  eax,TRUE
	ret
DllEntry Endp

SendMail proc STDCALL p_SMTP:DWORD,p_PORT:DWORD,p_LOGIN_NAME:DWORD,p_LOGIN_PASSWORD:DWORD,p_FROM:DWORD,p_TO:DWORD,p_SUBJECT:DWORD,p_MESSAGE:DWORD,p_ATTACHMENT:DWORD
	LOCAL wsadata 		:WSADATA
	LOCAL sin 		:sockaddr_in
	
	pushad
	;---zero memory [structures..]---
	invoke RtlZeroMemory,addr wsadata,SIZEOF wsadata
	invoke RtlZeroMemory,addr sin,SIZEOF sin
	
	invoke WSAStartup,101h,addr wsadata 	;initialise le socket
	.if eax!=0
		popad
		xor eax,eax
		ret
	.endif
	
	invoke socket,AF_INET,SOCK_STREAM,IPPROTO_IP
	.if eax==INVALID_SOCKET
		popad
		xor eax,eax
		ret
	.endif
	
	mov sock,eax
	mov sin.sin_family,AF_INET
	
	invoke htons,p_PORT 			;port 25 for smtp
	mov sin.sin_port,ax
	
	invoke gethostbyname,p_SMTP 		;"mail.gmx.net" for example
	.if eax == 0
		popad
		xor eax,eax
		ret
	.else
		mov eax,dword ptr [eax+12]
		mov eax,dword ptr [eax]
		mov eax,dword ptr [eax]
		mov sin.sin_addr,eax
	.endif
	
	invoke connect, sock, addr sin ,sizeof sin
	.if eax!=NULL
		popad
		xor eax,eax
		ret
	.endif	
	
	invoke recv, sock, addr buffer, sizeof buffer, 0  ;recieve answear
	.if eax==SOCKET_ERROR
		popad
		xor eax,eax
		ret
	.endif
	
	
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	;				---SEND EHLO PART---
	invoke AllocMem,512
	mov AllocedMemory,eax				;adress of memory which will be send
	invoke RtlZeroMemory,AllocedMemory,512
	
	invoke lstrcat,AllocedMemory,addr EHLO		;EHLO + AUTH LOGIN command
	;---username+password as base64 code
	invoke RtlZeroMemory,addr buffer,512		;will recieve base64 codes
	invoke lstrlen,p_LOGIN_NAME
	invoke Base64_Encode,p_LOGIN_NAME,eax,addr buffer
	invoke lstrcat,AllocedMemory,addr buffer	;attach base64 name
	invoke lstrcat,AllocedMemory,addr _enter	;13,10
	
	invoke RtlZeroMemory,addr buffer,512		;will recieve base64 codes
	invoke lstrlen,p_LOGIN_PASSWORD
	invoke Base64_Encode,p_LOGIN_PASSWORD,eax,addr buffer
	invoke lstrcat,AllocedMemory,addr buffer	;attach base64 password
	invoke lstrcat,AllocedMemory,addr _enter	;13,10
	
	invoke Mys,AllocedMemory                  	;send this part now          
	invoke recv,sock,addr buffer,sizeof buffer,0	;recieve answear...
	;invoke MessageBox,0,addr buffer,NULL, MB_OK
	
	invoke FreeMem,AllocedMemory
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
	
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	;				---SEND MAIL FROM PART---
	invoke AllocMem,512
	mov AllocedMemory,eax				;adress of memory which will be send
	invoke RtlZeroMemory,AllocedMemory,512
	
	invoke lstrcat,AllocedMemory,addr MAIL_FROM	;MAIL FROM: command
	invoke lstrcat,AllocedMemory,p_FROM		;"from@user.net"
	invoke lstrcat,AllocedMemory,addr _enter	;13,10
	
	invoke Mys,AllocedMemory                  	;send this part now          
	invoke recv,sock,addr buffer,sizeof buffer,0	;recieve answear...
	;invoke MessageBox,0,addr buffer,NULL, MB_OK
	
	invoke FreeMem,AllocedMemory
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
	
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	;				---SEND RCPT_TO PART---
	invoke AllocMem,512
	mov AllocedMemory,eax				;adress of memory which will be send
	invoke RtlZeroMemory,AllocedMemory,512
	
	invoke lstrcat,AllocedMemory,addr RCPT_TO	;RCPT_TO: command
	invoke lstrcat,AllocedMemory,p_TO		;"to@user.net"
	invoke lstrcat,AllocedMemory,addr _enter	;13,10
	
	invoke Mys,AllocedMemory                  	;send this part now          
	invoke recv,sock,addr buffer,sizeof buffer,0	;recieve answear...
	;invoke MessageBox,0,addr buffer,NULL, MB_OK
	
	invoke FreeMem,AllocedMemory
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
	
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	;				---SEND DATA PART---
	invoke AllocMem,0800000h			;alloc 8MB
	mov AllocedMemory,eax				;adress of memory which will be send
	invoke RtlZeroMemory,AllocedMemory,0800000h
 	
 	;---from---
	invoke lstrcat,AllocedMemory,addr _data_from	;DATA 13,10 FROM:
	invoke lstrcat,AllocedMemory,p_FROM
	invoke lstrcat,AllocedMemory,addr _enter	;13,10
	;---to---
	invoke lstrcat,AllocedMemory,addr _data_to	;DATA 13,10 FROM:
	invoke lstrcat,AllocedMemory,p_TO
	invoke lstrcat,AllocedMemory,addr _enter	;13,10
	;---subject--
	invoke lstrcat,AllocedMemory,addr _subject
	invoke lstrcat,AllocedMemory,p_SUBJECT		;mail subject
	;---content type--
	invoke lstrcat,AllocedMemory,addr _contenttype
	invoke lstrcat,AllocedMemory,p_MESSAGE		;mail message text
	;---file---
	invoke lstrcat,AllocedMemory,addr _file
	
	.IF p_ATTACHMENT != NULL
		invoke d2k2_FileNameOfPath,p_ATTACHMENT
		invoke lstrcat,AllocedMemory,eax		;the filename of the attachment
		invoke lstrcat,AllocedMemory,addr _enter	;13,10
		invoke lstrcat,AllocedMemory,addr _enter	;13,10
		;---attach Base64 file---	
		invoke AllocMem,0500000h			;alloc 5MB
		mov AttachMemory,eax				;adress of memory of base 64 attachment
		invoke RtlZeroMemory,AttachMemory,0500000h
		invoke CreateFileA,p_ATTACHMENT,GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL+FILE_ATTRIBUTE_HIDDEN,0
		.if eax==-1					;cant read file
			@quit_attachment:
			invoke FreeMem,AttachMemory
			invoke FreeMem,AllocedMemory
			invoke closesocket,sock
			invoke WSACleanup
			popad
			xor eax,eax
			ret
		.endif
		mov FileHandle,eax
		invoke GetFileSize, FileHandle, NULL
		.if eax > 0500000h				;quit it file is bigger than 5 MB
			invoke CloseHandle,FileHandle
			jmp @quit_attachment
		.endif
		mov FileSize,eax				;save the file size in bytes
		invoke ReadFile,FileHandle,AttachMemory,FileSize,addr b_read,0
        	invoke CloseHandle,FileHandle
        	
        	invoke AllocMem,0500000h
        	mov esi,eax	;base64 prebuffer
        	
        	invoke Base64_Encode,AttachMemory,FileSize,esi	;write base64 file to esi
        	
		invoke lstrlen,AllocedMemory
        	add eax,AllocedMemory	
        	mov edi,eax		;edi is target buffer
		invoke lstrlen,esi
        	
        	xor ecx,ecx
        	.while ecx<eax
        		push eax
        		push ecx
        		invoke RtlMoveMemory,edi,esi,64
			invoke lstrcat,edi,addr _enter ;13,10
			pop ecx
			pop eax
			
			add esi,64
			add edi,66
			add ecx,64
        	.endw
        	
        	invoke FreeMem,esi	;base64 prebuffer
        	invoke FreeMem,AttachMemory
	.ENDIF
	
	;---mail end---
	invoke lstrcat,AllocedMemory,addr _mail_end
	
	invoke Mys,AllocedMemory                  	;send this part now          
	invoke recv,sock,addr buffer,sizeof buffer,0	;recieve answear...
	;invoke MessageBox,0,addr buffer,NULL, MB_OK
	
	invoke FreeMem,AllocedMemory
	;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

	invoke closesocket,sock                          ;ferme le socket et quit
	invoke WSACleanup
	
	popad
	xor eax,eax
	inc eax		;return success
	ret
SendMail endp

Mys proc SendStr : DWORD                            
	push edi
	mov edi, SendStr
	.WHILE TRUE
		.BREAK .IF (byte ptr [edi] == 0 )
		invoke send, sock, edi, 1, 0                     
		inc edi                                                  
	.ENDW   
	pop edi
	ret
Mys endp

; --------------- AllocMem
AllocMem		proc	MemSize:dword
			mov	eax,MemSize
			or	eax,eax
			jz	NoAllocMem
			invoke	GlobalAlloc,GMEM_FIXED+GMEM_ZEROINIT,eax
NoAllocMem:		ret		;returns MemHandle
AllocMem		endp

; --------------- FreeMem
FreeMem			proc	MemHandle:dword
			mov	eax,MemHandle
			or	eax,eax
			jz	NoFreeMem
			invoke	GlobalFree,eax
NoFreeMem:		ret
FreeMem			endp

d2k2_FileNameOfPath proc PathBuffer:DWORD	;returns Address of Filename!
	LOCAL _filename:DWORD
	pushad	
	invoke lstrlen,PathBuffer
	mov edx,PathBuffer
	add edx,eax
	xor ecx,ecx
	.WHILE 	ecx!=eax
		mov bl,byte ptr ds:[edx-1]
		.if bl=="\"
			push edx
			pop _filename
			popad
			mov eax,_filename	;success, return filename
			ret
		.endif
		inc ecx
		dec edx	
	.ENDW
	popad
	mov eax,PathBuffer			;failed, return path
	ret
d2k2_FileNameOfPath endp

End DllEntry
