NO

Author Topic: Extracting the icon of an executable  (Read 377 times)

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Extracting the icon of an executable
« on: April 30, 2023, 07:52:40 PM »
Hello,

Here is an example of extracting the icon of an executable with Component Object Modeling ( COM ) :

Code: [Select]
include     SaveIcon.inc

.data

IID_IPicture  GUID <7BF80980h,0BF32h,101Ah,<8Bh,0BBh,00h,0AAh,00h,30h,0Ch,0ABh>>
IconName    db 'TestIcon.ico',0
file1       db 'Bug.exe',0
errmsg      db 'Bug.exe could not be loaded.',13,10,0

.data?

hIcon       dd ?
pBitmap     dd ?
hGlobal     dd ?
pcbSize     dd ?
pStream     dd ?
hLib        dd ?
pd          PICTDESC <?>
bm          BITMAP <?>

.code

start:

    invoke      LoadLibrary,ADDR file1
    test        eax,eax
    jnz         @f
    invoke      StdOut,ADDR errmsg
    invoke      ExitProcess,0
@@:
    mov         hLib,eax
    invoke      LoadIcon,eax,100
    mov         hIcon,eax

    mov         pd.cbSizeofstruct,SIZEOF PICTDESC   ; initialize the PICTDESC structure
    mov         pd.picType,PICTYPE_ICON
    push        hIcon
    pop         pd.icon.hicon
    invoke      OleCreatePictureIndirect,ADDR pd,ADDR IID_IPicture,TRUE,ADDR pBitmap
                                                    ; create the OLE image
    invoke      CreateStreamOnHGlobal,NULL,TRUE,ADDR pStream
                                                    ; create the destination stream to save the icon

    coinvoke    pBitmap,IPicture,SaveAsFile,pStream,TRUE,<OFFSET pcbSize>
    invoke      GetHGlobalFromStream,pStream,ADDR hGlobal
    invoke      GlobalLock,hGlobal                  ; get the address pointing the icon in memory
    invoke      WriteFileToDisc,ADDR IconName,eax,pcbSize
                                                    ; write the icon to disc
    coinvoke    pBitmap,IPicture,Release
    coinvoke    pStream,IStream,Release
    invoke      FreeLibrary,hLib
    invoke      ExitProcess,0

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

StdOut PROC _string:DWORD

    sub         esp,2*4
   
    invoke      GetStdHandle,STD_OUTPUT_HANDLE
    mov         DWORD PTR [esp+4],eax
   
    invoke      lstrlen,DWORD PTR [esp+12]
    mov         edx,esp
   
    invoke      WriteFile,DWORD PTR [esp+20],\
                DWORD PTR [esp+24],\
                eax,edx,0
           
    add         esp,2*4
    retn        4

StdOut ENDP

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef
   
END start
Code it... That's all...