Guest User

Untitled

a guest
Jan 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .486
  2. .model flat,stdcall
  3. option casemap:none
  4. include \masm32\include\windows.inc
  5. include \masm32\include\kernel32.inc
  6. includelib \masm32\lib\kernel32.lib
  7.  
  8. .const
  9. ;The call to WS2_32.send
  10. MEMORY_ADDRESS_Send_Pointer     equ 00597600h
  11.  
  12. ;The value in the mov ecx before the call to WS2_32.send
  13. MEMORY_ADDRESS_Socket_Pointer       equ 00768C50h
  14.  
  15. ;The key used to encrypt/decrypt the packets
  16. MEMORY_ADDRESS_XTEA_Key         equ MEMORY_ADDRESS_Socket_Pointer + 2Ch
  17.  
  18. .code
  19. DllEntry proc hInstance:HINSTANCE,reason:DWORD,reserved1:DWORD
  20.     mov eax,TRUE
  21.     ret
  22. DllEntry Endp
  23.  
  24. XTEA PROC Encrypt:BYTE,ProcessID:DWORD,Key:DWORD,Packet:DWORD,XTEAPacket:DWORD,SafeArray:BYTE
  25.     LOCAL Process               :DWORD
  26.     LOCAL XTEAkey[16]           :BYTE
  27.     LOCAL XTEAkeyPointer        :DWORD
  28.     LOCAL NumberOfBlocks        :WORD
  29.     LOCAL PacketPointer         :DWORD
  30.     LOCAL XTEAPacketPointer     :DWORD
  31.    
  32.     ;Fix key
  33.     .IF ProcessID!=NULL
  34.         invoke OpenProcess,PROCESS_ALL_ACCESS,NULL,ProcessID
  35.         .IF eax!=0
  36.             mov Process,eax
  37.            
  38.             ;Read the XTEA Key from Tibias memory
  39.             invoke ReadProcessMemory,Process,MEMORY_ADDRESS_XTEA_Key,ADDR XTEAkey,16,NULL
  40.            
  41.             INVOKE CloseHandle,Process
  42.            
  43.             lea eax,XTEAkey
  44.             mov XTEAkeyPointer,eax
  45.            
  46.         .ENDIF
  47.        
  48.     .ELSEIF Key!=NULL
  49.        
  50.         ;Fix the pointer variables
  51.         mov eax,Key
  52.         .IF SafeArray!=NULL
  53.             mov eax,DWORD PTR [eax]
  54.             add eax,12
  55.             mov eax,DWORD PTR [eax]
  56.         .ENDIF
  57.         mov XTEAkeyPointer,eax
  58.        
  59.     .ENDIF
  60.    
  61.     .IF ProcessID!=NULL || Key!=NULL
  62.        
  63.         ;Fix pointers
  64.         mov eax,Packet
  65.         mov ecx,XTEAPacket
  66.         .IF SafeArray!=NULL
  67.             mov eax,DWORD PTR [eax]
  68.             add eax,12
  69.             mov eax,DWORD PTR [eax]
  70.             mov ecx,DWORD PTR [ecx]
  71.             add ecx,12
  72.             mov ecx,DWORD PTR [ecx]
  73.         .ENDIF
  74.         mov PacketPointer,eax
  75.         mov XTEAPacketPointer,ecx
  76.        
  77.         ;Calculate how many blocks of 8-bytes that is going to be encrypted/decrypted
  78.         movzx eax,WORD PTR [eax]
  79.         .IF Encrypt!=FALSE
  80.            
  81.             add eax,2
  82.             mov cx,8
  83.             xor edx,edx
  84.             div cx
  85.             .IF edx!=0
  86.                 inc ax
  87.             .ENDIF
  88.             mov NumberOfBlocks,ax
  89.            
  90.             ;Calculate the length of the encrypted packet and also write the header
  91.             lea eax,[eax*8]
  92.             mov ecx,XTEAPacketPointer
  93.             mov WORD PTR [ecx],ax
  94.             add XTEAPacketPointer,2
  95.            
  96.         .ELSEIF Encrypt==FALSE
  97.            
  98.             shr eax,3
  99.             mov NumberOfBlocks,ax
  100.            
  101.             add PacketPointer,2
  102.            
  103.         .ENDIF
  104.        
  105.         movzx eax,Encrypt
  106.         push eax
  107.        
  108.         ;Main encryption loop
  109.         .REPEAT
  110.             mov edx,PacketPointer
  111.             .IF BYTE PTR [esp]!=FALSE
  112.                 xor eax,eax
  113.                 mov ecx,DWORD PTR [edx]
  114.                 mov edx,DWORD PTR [edx+4]
  115.             .ELSEIF BYTE PTR [esp]==FALSE
  116.                 mov eax,0C6EF3720h
  117.                 mov ecx,DWORD PTR [edx+4]
  118.                 mov edx,DWORD PTR [edx]
  119.             .ENDIF
  120.             push ebx
  121.             push ebp
  122.             push esi
  123.             mov esi,XTEAkeyPointer
  124.             push edi
  125.             mov edi,32
  126.             .REPEAT
  127.                 MOV EBX,EDX
  128.                 SHR EBX,5
  129.                 MOV EBP,EDX
  130.                 SHL EBP,4
  131.                 XOR EBX,EBP
  132.                 ADD EBX,EDX
  133.                 MOV EBP,EAX
  134.                 .IF BYTE PTR [esp+4*SIZEOF DWORD]==FALSE
  135.                     SHR EBP,0Bh
  136.                 .ENDIF
  137.                 AND EBP,3
  138.                 MOV EBP,DWORD PTR DS:[ESI+EBP*4]
  139.                 ADD EBP,EAX
  140.                 XOR EBX,EBP
  141.                 .IF BYTE PTR [esp+4*SIZEOF DWORD]!=FALSE
  142.                     ADD ECX,EBX
  143.                     SUB EAX,61C88647h
  144.                 .ELSEIF BYTE PTR [esp+4*SIZEOF DWORD]==FALSE
  145.                     SUB ECX,EBX
  146.                     ADD EAX,61C88647h
  147.                 .ENDIF
  148.                 MOV EBX,ECX
  149.                 SHR EBX,5
  150.                 MOV EBP,ECX
  151.                 SHL EBP,4
  152.                 XOR EBX,EBP
  153.                 ADD EBX,ECX
  154.                 MOV EBP,EAX
  155.                 .IF BYTE PTR [esp+4*SIZEOF DWORD]!=FALSE
  156.                     SHR EBP,0Bh
  157.                 .ENDIF
  158.                 AND EBP,3
  159.                 MOV EBP,DWORD PTR DS:[ESI+EBP*4]
  160.                 ADD EBP,EAX
  161.                 XOR EBX,EBP
  162.                 .IF BYTE PTR [esp+4*SIZEOF DWORD]!=FALSE
  163.                     ADD EDX,EBX
  164.                 .ELSEIF BYTE PTR [esp+4*SIZEOF DWORD]==FALSE
  165.                     SUB EDX,EBX
  166.                 .ENDIF
  167.                 DEC EDI
  168.             .UNTIL edi==0
  169.             pop edi
  170.             pop esi
  171.             pop ebp
  172.             pop ebx
  173.             mov eax,XTEAPacketPointer
  174.             .IF BYTE PTR [esp]!=FALSE
  175.                 mov DWORD PTR [eax],ecx
  176.                 mov DWORD PTR [eax+4],edx
  177.             .ELSEIF BYTE PTR [esp]==FALSE
  178.                 mov DWORD PTR [eax],edx
  179.                 mov DWORD PTR [eax+4],ecx
  180.             .ENDIF
  181.             add XTEAPacketPointer,8
  182.             add PacketPointer,8
  183.             dec NumberOfBlocks
  184.         .UNTIL NumberOfBlocks==0
  185.        
  186.         add esp,4
  187.        
  188.         ;Return the length of the packet
  189.         mov ecx,XTEAPacket
  190.         movzx eax,WORD PTR [ecx]
  191.         .IF SafeArray!=NULL
  192.             mov ecx,DWORD PTR [ecx]
  193.             add ecx,12
  194.             mov eax,DWORD PTR [ecx]
  195.             movzx eax,WORD PTR [eax]
  196.             add eax,2
  197.             mov WORD PTR [ecx+4],ax
  198.         .ENDIF
  199.         add eax,2
  200.        
  201.     .ENDIF
  202.    
  203.     ret
  204. XTEA endp
  205.  
  206. SendPacket PROC ProcessID:DWORD,Packet:DWORD,Encrypt:BYTE,SafeArray:BYTE
  207.     LOCAL Process               :DWORD
  208.     LOCAL Socket                :DWORD
  209.     LOCAL PacketPointer         :DWORD
  210.     LOCAL EncryptedPacketLength :DWORD
  211.     LOCAL PacketLength          :DWORD
  212.     LOCAL TotalLength           :DWORD
  213.     LOCAL AllocatedMemory       :DWORD
  214.     LOCAL PacketInfo            :DWORD
  215.     LOCAL RemoteThread          :DWORD
  216.    
  217.     jmp @SendPacket
  218.    
  219. @SendPacketThread:
  220.     push 0
  221.     push DWORD PTR DS:[ebx]
  222.     add ebx,4
  223.     push ebx
  224.     mov eax,DWORD PTR DS:[MEMORY_ADDRESS_Socket_Pointer]
  225.     push DWORD PTR DS:[eax+4]
  226.     call DWORD PTR DS:[MEMORY_ADDRESS_Send_Pointer]
  227.     retn
  228.    
  229. @SendPacket:
  230.     INVOKE OpenProcess,PROCESS_ALL_ACCESS,NULL,ProcessID
  231.     .IF eax!=0
  232.         mov Process,eax
  233.         mov Socket,0
  234.         INVOKE ReadProcessMemory,Process,MEMORY_ADDRESS_Socket_Pointer,ADDR Socket,4,NULL
  235.         .IF Socket!=0
  236.             mov eax,Packet
  237.             .IF SafeArray!=NULL
  238.                 mov eax,DWORD PTR [eax]
  239.                 add eax,12
  240.                 mov eax,DWORD PTR [eax]
  241.             .ENDIF
  242.             mov PacketPointer,eax
  243.            
  244.             .IF Encrypt!=NULL
  245.                
  246.                 ;Make space for the encrypted packet
  247.                 mov eax,PacketPointer
  248.                 movzx eax,WORD PTR [eax]
  249.                 add eax,2
  250.                 mov cx,8
  251.                 xor edx,edx
  252.                 div cx
  253.                 .IF edx!=0
  254.                     inc ax
  255.                 .ENDIF
  256.                 lea eax,[eax*8+4]
  257.                 mov EncryptedPacketLength,eax
  258.                 sub esp,eax
  259.                 mov eax,esp
  260.                
  261.                 INVOKE XTEA,TRUE,ProcessID,NULL,PacketPointer,eax,NULL
  262.                
  263.                 mov PacketPointer,esp
  264.                
  265.             .ENDIF
  266.            
  267.             mov eax,PacketPointer
  268.             movzx eax,WORD PTR [eax]
  269.             add eax,2
  270.             mov PacketLength,eax
  271.             add eax,((OFFSET @SendPacket - OFFSET @SendPacketThread) + SIZEOF DWORD)
  272.             mov TotalLength,eax
  273.             INVOKE VirtualAllocEx,Process,NULL,TotalLength,MEM_COMMIT+MEM_RESERVE,PAGE_EXECUTE_READWRITE
  274.             mov AllocatedMemory,eax
  275.             mov PacketInfo,eax
  276.             INVOKE WriteProcessMemory,Process,AllocatedMemory,OFFSET @SendPacketThread,(OFFSET @SendPacket - OFFSET @SendPacketThread),NULL
  277.             add PacketInfo,(OFFSET @SendPacket - OFFSET @SendPacketThread)
  278.             INVOKE WriteProcessMemory,Process,PacketInfo,ADDR PacketLength,4,NULL
  279.             add PacketInfo,SIZEOF DWORD
  280.             INVOKE WriteProcessMemory,Process,PacketInfo,PacketPointer,PacketLength,NULL
  281.             .IF Encrypt!=NULL
  282.                 add esp,EncryptedPacketLength
  283.             .ENDIF
  284.             sub PacketInfo,SIZEOF DWORD
  285.             INVOKE CreateRemoteThread,Process,NULL,NULL,AllocatedMemory,PacketInfo,NULL,NULL
  286.             mov RemoteThread,eax
  287.             INVOKE WaitForSingleObject,RemoteThread,INFINITE
  288.             INVOKE CloseHandle,RemoteThread
  289.             INVOKE VirtualFreeEx,Process,AllocatedMemory,0,MEM_RELEASE
  290.         .ENDIF
  291.         INVOKE CloseHandle,Process
  292.     .ENDIF
  293.     ret
  294. SendPacket endp
  295.  
  296. End DllEntry
Add Comment
Please, Sign In to add comment