Programming

All about programming stuff

by invalid

Ini source code assembly yang sempat saya bikin tahun 2006 untuk mengekstrak resource berupa gambar JPG pada aplikasi yang dibuat menggunakan Borland Delphi. Prinsip kerjanya sederhana, yaitu dengan mencari header JFIF pada aplikasi yang dibuat menggunakan Borland Delphi. Jika ditemukan, maka akan disimpan dengan nama yang formatnya berurutan. Gunakan flatassembler untuk meng-assemble source code ini:

    format PE GUI 4.0
    include 'win32a.inc'
    section '' code data executable readable writeable

;[ code ]####################################################################

    push    L3
    push    dword [fs:0]
    mov     dword [fs:0], esp                   ; pasang SEH biar aman ;)
    invoke  GetOpenFileName,ofn                 ; tampilkan dialog OpenFile
    test    eax, eax                            ; apakah user memilih file?
    jz      L3                                  ; jika tidak, langsung keluar
    invoke  CreateFile,szfile,GENERIC_READ,0,0,OPEN_EXISTING,0,0
    cmp     eax, INVALID_HANDLE_VALUE
    je      L2
    mov     [hfile], eax
    invoke  GetFileSize,eax,0
    mov     [dwsize], eax
    invoke  GlobalAlloc,GPTR,eax
    mov     [hmems], eax
    invoke  ReadFile,[hfile],eax,[dwsize],dwrwxx,0
    invoke  CloseHandle,[hfile]
    call    fold
    test    eax, eax
    jnz     @F
    invoke  MessageBox,0,szwarn,sztitl,MB_OK+MB_ICONINFORMATION
    invoke  lstrcpy,szpath,szfile
    invoke  PathRemoveFileSpec,szpath
@@: mov     edi, [hmems]                        ; metoda pencarian yg sangat
    xor     ecx, ecx                            ; tidak optimal, sorry :(
L1: cmp     dword [edi], 0E0FFD8FFh
    jne     @F
    cmp     dword [edi+6], 'JFIF'
    jne     @F
    push    ecx
    mov     edx, dword [edi-4]
    push    edx
    push    edx
    push    edi
    call    ripp
    pop     edx
    pop     ecx
    dec     edx
    add     edi, edx
    add     ecx, edx
    inc     dword [dwhits]
@@: inc     edi
    inc     ecx
    cmp     ecx, dword [dwsize]
    jl      L1
    invoke  GlobalFree,[hmems]
    invoke  wsprintf,szfile,szfrm2,[dwhits]
    add     esp, 0Ch
    invoke  MessageBox,0,szfile,sztitl,MB_OK+MB_ICONINFORMATION
    jmp     L3
L2: call    oops
L3: pop     dword [fs:0]                        ; habis manis SEH dibuang
    add     esp, 4                              ; stack balancing
    invoke  ExitProcess,0                       ; selesai

; browse for folder ---------------------------------------------------------
proc fold
    push    ebp
    mov     ebp, esp
    add     esp, -24h
    mov     dword [ebp-24h], 0
    mov     dword [ebp-20h], 0
    mov     dword [ebp-1Ch], 0
    mov     dword [ebp-18h], szinfo
    mov     dword [ebp-14h], 3
    mov     dword [ebp-10h], ffun
    mov     dword [ebp-0Ch], sztitl
    mov     dword [ebp-8], 0
    lea     eax, dword [ebp-24h]
    push    eax
    call    [SHBrowseForFolder]
    mov     dword [ebp-4], eax
    test    eax, eax
    jnz     @F
    xor     eax, eax
    push    eax
    jmp     L4
@@: lea     eax, [szpath]
    push    eax
    push    dword [ebp-4]
    call    [SHGetPathFromIDList]
    mov     eax, 1
    push    eax
L4: push    dword [ebp-4]
    call    [CoTaskMemFree]
    pop     eax
    leave
    ret
endp

; fungsi callback browse for folder -----------------------------------------
proc ffun
    push    ebp
    mov     ebp, esp
    push    dword [ebp+14h]
    push    dword [ebp+8]
    call    [SetWindowText]
    leave
    ret     10h
endp

; simpan gambar ke file -----------------------------------------------------
proc ripp
    push    ebp
    mov     ebp, esp
    invoke  wsprintf,szfile,szfrm1,szpath,[dwhits] ; wsprintf
    add     esp, 10h                            ; stack balancing
    invoke  CreateFile,szfile,GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0
    cmp     eax, INVALID_HANDLE_VALUE
    je      @F
    push    eax
    push    0
    push    dwrwxx
    push    dword [ebp+0Ch]
    push    dword [ebp+8]
    push    eax
    call    [WriteFile]
    call    [CloseHandle]
    jmp     L5
@@: call    oops
L5: leave
    ret     8
endp

; minimal error handler -----------------------------------------------------
proc oops
    push    ebp
    mov     ebp, esp
    push    0
    push    0
    lea     eax, dword [ebp-4]
    push    eax
    push    0
    invoke  GetLastError
    push    eax
    push    0
    push    FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM
    call    [FormatMessage]
    mov     ecx, dword [ebp-4]
    invoke  MessageBox,0,ecx,szcerr,MB_OK+MB_ICONERROR
    mov     edx, dword [ebp-4]
    invoke  LocalFree,edx
    leave
    ret
endp

;[ data ]####################################################################

    szcerr  db "Oops!",0
    szfrm1  db "%s\img_%03d.jpg",0
    szfrm2  db "%d gambar berhasil diekstrak",0
    szinfo  db "Pilih direktori untuk menyimpan file JPEG",0
    szwarn  db "File JPEG akan diekstrak ke direktori executable",0
    sztitl  db "Jpg Ripper v0.10",0
    szfilt  db "file executable saja",0,"*.exe;*.dll;*.ocx",0
            db "semua jenis file",0,"*.*",0,0
    szpath  rb 260
    szfile  rb 260
    ofn     dd 4Ch,0,0,szfilt,0,0,0,szfile,259,0,0,0,sztitl
            dd OFN_EXPLORER+OFN_FILEMUSTEXIST+OFN_HIDEREADONLY
            dd 0,0,0,0,0,0
    hfile   dd 0                ; handle file
    hmems   dd 0                ; handle memori
    dwsize  dd 0                ; ukuran file
    dwrwxx  dd 0                ; jumlah read/write
    dwhits  dd 0                ; jumlah gambar

;[ imports ]#################################################################

    data import
    library kernel32,'kernel32.dll',\
            comdlg32,'comdlg32.dll',\
            shell32,'shell32.dll',\
            shlwapi,'shlwapi.dll',\
            user32,'user32.dll',\
            ole32,'ole32.dll'
    include 'api/kernel32.inc'
    include 'api/comdlg32.inc'
    include 'api/shell32.inc'
    include 'api/user32.inc'
    import  shlwapi,\
            PathRemoveFileSpec,'PathRemoveFileSpecA'
    import  ole32,\
            CoTaskMemFree,'CoTaskMemFree'
    end data

; EOF

ASM old but gold :D