Foro de elhacker.net

Programación => ASM => Mensaje iniciado por: EAX_ en 19 Diciembre 2009, 19:32 pm



Título: Ventana en ASM
Publicado por: EAX_ en 19 Diciembre 2009, 19:32 pm
Hola.. bueno les cuento.. estoy tratando de entender el lenguaje ASM, con ayuda de un amigo y leyendo un poco he conseguido crear un programa que muestra un msgbox al inicio, se crea una ventana y al clickarla sale otro msgbox.. necesito un poco de ayuda para poder hacer un boton que haga alguna acción (por ejemplo ejecutar CMD) o algo así.

 Buehh este es el code..

Código
  1. hInstance equ 0x00400000
  2. format PE GUI 4.0 at hInstance as 'exe'
  3. entry codee
  4. include 'WIN32A.INC'
  5.  
  6. section '.bss' readable writeable
  7. msg             MSG
  8. bools           db ? ; [1]Exit
  9.  
  10. section '.text' code readable executable
  11.  
  12. codee:
  13.        push    MB_ICONEXCLAMATION
  14.        push    _msgb0
  15.        push    _msgb1
  16.        push    0
  17.        call    [MessageBox]
  18.        push    _classname
  19.        push    0
  20.        push    0
  21.        call    [CreateMutex]
  22.        call    [GetLastError]
  23.        TEST    EAX,EAX
  24.        jnz     .end
  25.        push    BLACK_BRUSH
  26.        call    [GetStockObject]
  27.        mov     [wc.hbrBackground],EAX
  28.        push    IDC_ARROW
  29.        push    0
  30.        call    [LoadCursor]
  31.        mov     [wc.hCursor],EAX
  32.        push    IDI_APPLICATION
  33.        push    0
  34.        call    [LoadIcon]
  35.        mov     [wc.hIcon],EAX
  36.        push    wc
  37.        call    [RegisterClass]
  38.        push    0
  39.        push    [wc.hInstance]
  40.        push    0
  41.        push    0
  42.        push    300
  43.        push    500
  44.        push    CW_USEDEFAULT
  45.        push    CW_USEDEFAULT
  46.        push    WS_VISIBLE+WS_CAPTION+WS_SYSMENU+WS_MAXIMIZEBOX
  47.        push    _title
  48.        push    _classname
  49.        push    0
  50.        call    [CreateWindowEx]
  51.        jmp     .jump0
  52.        .jump:
  53.        push    msg
  54.        call    [TranslateMessage]
  55.        push    msg
  56.        call    [DispatchMessage]
  57.        test    [bools],1
  58.        jnz     .end
  59.  
  60.        .jump0:
  61.        push    0
  62.        push    0
  63.        push    0
  64.        push    msg
  65.        call    [GetMessage]
  66.        cmp     EAX,1
  67.        je      .jump
  68. .end:
  69.        ret
  70.  
  71. WindowProc:
  72.        mov     EAX,DWORD PTR SS: ESP+8
  73.        cmp     EAX,WM_LBUTTONDOWN
  74.        je      .lbdwn
  75.        cmp     EAX,WM_DESTROY
  76.        je      .wm_destroy
  77.        jmp     [DefWindowProc]
  78.        .lbdwn:
  79.        push    0
  80.        push    _titlebox
  81.        push    _msgboxcnt
  82.        push    0
  83.        call    [MessageBox]
  84.        retn    16
  85.        .wm_destroy:
  86.        or      [bools],1
  87.        retn    16
  88.  
  89.  
  90. section '.data' writable readable
  91.  
  92. _msgb0          TCHAR 'ALERT',0
  93. _msgb1          TCHAR 'Test program, use under your own risk',0
  94. _text1          TCHAR 'Testing this program fucking',0
  95. _msgboxcnt      TCHAR 'Do not click my program fucking',0
  96. _titlebox       TCHAR 'WARNING',0
  97. _title          TCHAR 'Program fucking',0
  98. _classname      TCHAR 'MyFuckingProgram'
  99. wc              WNDCLASS 0,WindowProc,0,0,hInstance,0,0,0,0,_classname
  100.  
  101. data import
  102. U_KERNEL32     equ 1
  103. U_NTDLL       equ 0
  104. U_USER32       equ 1
  105. U_GDI32       equ 1
  106. U_DWM         equ 0
  107. U_SHELL32     equ 0
  108. U_ADVAPI32     equ 0
  109. U_WS2_32       equ 0
  110. U_COMCTL32     equ 0
  111. U_MSIMG32     equ 0
  112. U_MSVCRT       equ 0
  113. U_OLE32       equ 1
  114. include 'LIBS.INC'
  115. end data

Si me pudieran explicar o darme algun texto donde se explique el funcionamiento de la pila les agradeceria tambien..


EAX_

Edito:

Se me olvidaba.. para programar uso FASM y me ayudo con OllyDbg para tratar de entender bien..


Título: Re: Ventana en ASM
Publicado por: Eternal Idol en 19 Diciembre 2009, 19:50 pm
Para empezar a probar cambia el codigo de la etiqueta lbdwn, para ejecutar un programa la forma mas simple es usar WinExec (http://msdn.microsoft.com/en-us/library/ms687393(VS.85).aspx) (despues ya podras usar CreateProcess o ShellExecute).
Despues cuando ya sepas ejecutar algo tendras que crear una ventana hija de la primera para el control del tipo boton (nombre de clase BUTTON) y manejando el mensaje WM_COMMAND podras ver el ID del boton. Lee al respecto en la MSDN: http://msdn.microsoft.com/en-us/library/bb775943(VS.85).aspx

Tal vez esto te sirva para la pila:
http://insecure.org/stf/smashstack.html

Leer todo esto no viene mal tampoco:
http://foro.elhacker.net/asm/entry_point-t256455.0.html


Título: Re: Ventana en ASM
Publicado por: EAX_ en 19 Diciembre 2009, 19:57 pm
Gracias por responder, cuando tenga tiempo voy a leer lo que me mandaste y ahora voy a tratar de agregar esa funcion que me dijiste para ver si sale algo..

EAX_

EI: juntando mensajes.

Ufff.. despues de varios intentos y acordandome de algunas cosas que me decia un amigo que me ayuda con esto.. pude abrir CMD.. ahora, no sé si sobra algo o si está mal porque todavia no entiendo bien a la pila.. creo que puse algo de más..

Código
  1. WindowProc:
  2.        mov     EAX,DWORD PTR SS: ESP+8
  3.        cmp     EAX,WM_LBUTTONDOWN
  4.        je      .lbdwn
  5.        cmp     EAX,WM_DESTROY
  6.        je      .wm_destroy
  7.        jmp     [DefWindowProc]
  8.        .lbdwn:
  9.        push    SW_SHOWNORMAL
  10.        push    _cmd
  11.        call    [WinExec]
  12.        retn    16                   ; Al quitarlo se cierra el programa y se abre cmd
  13.        .wm_destroy:
  14.        or      [bools],1
  15.        retn    16
  16.  
  17.        section '.data' writable readable
  18.  
  19. _cmd            TCHAR 'C:\Windows\system32\cmd.exe',0
  20.  

Ese retn 16 todavia no lo entiendo bien.. si pudieras explicarme que hace te lo agradeceria.. creo que tiene que ver algo con los push, pero no recuerdo bien..

EAX_

PD: Perdón por el multipost..


Título: Re: Ventana en ASM
Publicado por: Eternal Idol en 19 Diciembre 2009, 20:58 pm
http://pdos.csail.mit.edu/6.828/2009/readings/i386/RET.htm

Retorna al llamador y saca los parametros de la pila (que para esta funcion son 4 DWORDS, 16 bytes).

PD. ¿De donde sale libs.inc? Cada dia odioa mas FASM y sus includes ...


Título: Re: Ventana en ASM
Publicado por: EAX_ en 20 Diciembre 2009, 20:21 pm
Libs.inc es esto:

Código
  1. library kernel32,'KERNEL32.DLL',U_KERNEL32,\
  2. ntdll,'NTDLL.DLL',U_NTDLL,\
  3. user32,'USER32.DLL',U_USER32,\
  4. gdi32,'GDI32.DLL',U_GDI32,\
  5. dwm,'DWMAPI.DLL',U_DWM,\
  6. shell32,'SHELL32.DLL',U_SHELL32,\
  7. advapi32,'ADVAPI32.DLL',U_ADVAPI32,\
  8. ws2_32,'WS2_32.DLL',U_WS2_32,\
  9. comctl32,'COMCTL32.DLL',U_COMCTL32,\
  10. msvcrt,'MSVCRT.DLL',U_MSVCRT,\
  11. msimg32,'MSIMG32.DLL',U_MSIMG32,\
  12. ole32,'OLE32.DLL',U_OLE32,\
  13. oleaut32,'OLEAUT32.DLL',U_OLE32
  14.  
  15. include 'API/KERNEL32.INC'
  16. include 'API/NTDLL.INC'
  17. include 'API/USER32.INC'
  18. include 'API/GDI32.INC'
  19. include 'API/DWM.INC'
  20. include 'API/SHELL32.INC'
  21. include 'API/ADVAPI32.INC'
  22. include 'API/WS2_32.INC'
  23. include 'API/COMCTL32.INC'
  24. include 'API/MSIMG32.INC'
  25. include 'API/MSVCRT.INC'
  26. include 'API/OLE32.INC'

Ya voy entendiendo un poco sobre el ret.. tambien pude usar la funcion WinExec con algunas propiedades y eso.. ahora voy a ver como me va con ShellExecute porque no tube suerte con CreateProcess..

EAX_

Edito

Bueno, con ShellExecute me fue bien.. ahora, queria hacer una pregunta. Cómo podría yo obtener la letra de la unidad donde esta instalado windows? Me refiero a %HomeDrive% o %SystemRoot%.

EAX_


Título: Re: Ventana en ASM
Publicado por: Eternal Idol en 20 Diciembre 2009, 20:56 pm
Podes usar GetWindowsDirectory o GetSystemDirectory; siempre consulta la MSDN (http://msdn.microsoft.com), esta la referencia donde explica que representa cada parametro y muchas veces incluye ejemplos.


Título: Re: Ventana en ASM
Publicado por: EAX_ en 20 Diciembre 2009, 21:13 pm
estuve buscando sobre GetWindowDirectory, leí en msdn pero no se me ocurre como poder usar lo que GetWindowDirectory devuelve.. o aunque sea imprimirlo en la ventana que tengo..

Cómo puedo hacer eso?

Código
  1. hInstance equ 0x00400000
  2. format PE GUI 4.0 at hInstance as 'exe'
  3. entry codee
  4. include 'WIN32A.INC'
  5.  
  6. section '.bss' readable writeable
  7. msg             MSG
  8. bools           db ? ; [1]Exit
  9.  
  10. section '.text' code readable executable
  11.  
  12. codee:
  13.        push    _classname
  14.        push    0
  15.        push    0
  16.        call    [CreateMutex]
  17.        call    [GetLastError]
  18.        TEST    EAX,EAX
  19.        jnz     .end
  20.        push    DKGRAY_BRUSH
  21.        call    [GetStockObject]
  22.        mov     [wc.hbrBackground],EAX
  23.        push    IDC_ARROW
  24.        push    0
  25.        call    [LoadCursor]
  26.        mov     [wc.hCursor],EAX
  27.        push    IDI_APPLICATION
  28.        push    0
  29.        call    [LoadIcon]
  30.        mov     [wc.hIcon],EAX
  31.        push    wc
  32.        call    [RegisterClass]
  33.        push    0
  34.        push    [wc.hInstance]
  35.        push    0
  36.        push    0
  37.        push    300
  38.        push    500
  39.        push    CW_USEDEFAULT
  40.        push    CW_USEDEFAULT
  41.        push    WS_VISIBLE+WS_CAPTION+WS_SYSMENU+WS_MAXIMIZEBOX
  42.        push    _title
  43.        push    _classname
  44.        push    0
  45.        call    [CreateWindowEx]
  46.        jmp     .jump0
  47.        .jump:
  48.        push    msg
  49.        call    [TranslateMessage]
  50.        push    msg
  51.        call    [DispatchMessage]
  52.        test    [bools],1
  53.        jnz     .end
  54.  
  55.        .jump0:
  56.        push    0
  57.        push    0
  58.        push    0
  59.        push    msg
  60.        call    [GetMessage]
  61.        cmp     EAX,1
  62.        je      .jump
  63. .end:
  64.        ret
  65.  
  66. WindowProc:
  67.        mov     EAX,DWORD PTR SS: ESP+8
  68.        cmp     EAX,WM_LBUTTONDOWN
  69.        je      .lbdwn
  70.        cmp     EAX,WM_DESTROY
  71.        je      .wm_destroy
  72.        jmp     [DefWindowProc]
  73.        .lbdwn:
  74.        push    0
  75.        push    _c
  76.        push    _cmdp
  77.        push    _cmd
  78.        push    _open
  79.        push    0
  80.        call    [ShellExecute]
  81.        retn    16
  82.        .wm_destroy:
  83.        or      [bools],1
  84.        retn    16
  85.  
  86.  
  87. section '.data' writable readable
  88.  
  89. _open           TCHAR 'open',0
  90. _c              TCHAR 'C:\',0
  91. _cmd            TCHAR 'cmd',0
  92. _cmdp           TCHAR '/c msg * asd',0
  93. _msgb0          TCHAR 'ALERT',0
  94. _text1          TCHAR 'Testing this program fucking',0
  95. _msgboxcnt      TCHAR 'Do not click my program fucking',0
  96. _title          TCHAR 'Program fucking',0
  97. _classname      TCHAR 'MyFuckingProgram'
  98. wc              WNDCLASS 0,WindowProc,0,0,hInstance,0,0,0,0,_classname
  99.  
  100. data import
  101.  
  102. U_KERNEL32     equ 1
  103. U_NTDLL       equ 0
  104. U_USER32       equ 1
  105. U_GDI32       equ 1
  106. U_DWM         equ 0
  107. U_SHELL32     equ 1
  108. U_ADVAPI32     equ 0
  109. U_WS2_32       equ 0
  110. U_COMCTL32     equ 0
  111. U_MSIMG32     equ 0
  112. U_MSVCRT       equ 0
  113. U_OLE32       equ 1
  114. include        'LIBS.INC'
  115.  
  116. end data
  117.  

EAX_


Título: Re: Ventana en ASM
Publicado por: Eternal Idol en 20 Diciembre 2009, 22:28 pm
Código
  1. .data
  2. buff db 512 dup(0)
  3.  
  4. .code
  5. main:
  6. push 512
  7. push offset buff
  8. call GetSystemDirectoryA


Título: Re: Ventana en ASM
Publicado por: EAX_ en 20 Diciembre 2009, 22:55 pm
Gracias por contestar, traté poniendo eso y me da error con "offset".. el error es: undefined symbol 'offset'.
Faltará algún include?

EAX_



Título: Re: Ventana en ASM
Publicado por: Eternal Idol en 20 Diciembre 2009, 23:07 pm
No, es MASM ... saca offset y listo.


Título: Re: Ventana en ASM
Publicado por: EAX_ en 20 Diciembre 2009, 23:13 pm
Sisi, recien lo saque y con un msgbox confirme que funcionó, iba a responder pero ganaste.. gracias..

EAX_


Título: Re: Ventana en ASM
Publicado por: YST en 21 Diciembre 2009, 01:27 am
Citar
%HomeDrive% o %SystemRoot%.

Puedes tambien obtener las variables de entorno con GetEnvironmentVariable

Código
  1. include "win32ax.inc"
  2. .code
  3. start:
  4.  invoke GetEnvironmentVariable,"HomeDrive",Buffer,MAX_PATH
  5. invoke MessageBox,0,Buffer,0,0
  6. invoke ExitProcess,0
  7. .data
  8. Buffer db MAX_PATH dup (0)
  9. .end start


Título: Re: Ventana en ASM
Publicado por: EAX_ en 21 Diciembre 2009, 02:58 am
Uff, despues de unos intentos me sirvio lo que me pasaste, gracias.. pero no logré usar invoke asi que lo hice asi:
Código
  1. .codee:
  2.        push    MAX_PATH
  3.        push    buff2
  4.        push    _home
  5.        call    [GetEnvironmentVariable]
  6. .data
  7. buff2   db MAX_PATH dup (0)
  8. end data

Gracias..

Edito

Me quede sin memoria (out of memory) y le di más con FASM, ahora nose porque pero me cierra el programa y sale la ventanita de enviar informe de errores

Que tengo mal?
Código
  1. hInstance equ 0x00400000
  2. format PE GUI 4.0 at hInstance as 'exe'
  3. entry codee
  4. include 'WIN32A.INC'
  5.  
  6. section '.bss' readable writeable
  7. msg             MSG
  8. bools           db ? ; [1]Exit
  9.  
  10.  
  11. section '.text' code readable executable
  12.  
  13. codee:
  14.        push    _classname
  15.        push    0
  16.        push    0
  17.        call    [CreateMutex]
  18.        call    [GetLastError]
  19.        TEST    EAX,EAX
  20.        jnz     .fiiin
  21.        push    MAX_PATH
  22.        push    buff2
  23.        push    _home
  24.        call    [GetEnvironmentVariable]
  25.        push    DKGRAY_BRUSH
  26.        call    [GetStockObject]
  27.        mov     [wc.hbrBackground],EAX
  28.        push    IDC_ARROW
  29.        push    0
  30.        call    [LoadCursor]
  31.        mov     [wc.hCursor],EAX
  32.        push    IDI_APPLICATION
  33.        push    0
  34.        call    [LoadIcon]
  35.        mov     [wc.hIcon],EAX
  36.        push    wc
  37.        call    [RegisterClass]
  38.        push    0
  39.        push    [wc.hInstance]
  40.        push    0
  41.        push    0
  42.        push    300
  43.        push    500
  44.        push    CW_USEDEFAULT
  45.        push    CW_USEDEFAULT
  46.        push    WS_VISIBLE+WS_CAPTION+WS_SYSMENU+WS_MAXIMIZEBOX+WS_THICKFRAME
  47.        push    _title
  48.        push    _classname
  49.        push    0
  50.        call    [CreateWindowEx]
  51.        jmp     .jump0
  52.        .jump:
  53.        push    msg
  54.        call    [TranslateMessage]
  55.        push    msg
  56.        call    [DispatchMessage]
  57.        test    [bools],1
  58.        jnz     .fiiin
  59.  
  60.        .jump0:
  61.        push    0
  62.        push    0
  63.        push    0
  64.        push    msg
  65.        call    [GetMessage]
  66.        cmp     EAX,1
  67.        je      .jump
  68. .fiiin:
  69.        ret
  70.  
  71. WindowProc:
  72.        mov     EAX,DWORD PTR SS: ESP+8
  73.        cmp     EAX,WM_LBUTTONDOWN
  74.        je      .lbdwn
  75.        cmp     EAX,WM_DESTROY
  76.        je      .wm_destroy
  77.        jmp     [DefWindowProc]
  78.        .lbdwn:
  79.        push    SW_SHOWNORMAL
  80.        push    0
  81.        push    _c
  82.        push    _cmdp
  83.        push    _cmd
  84.        push    _open
  85.        push    0
  86.        call    [ShellExecute]
  87.        retn    16
  88.        .wm_destroy:
  89.        or      [bools],1
  90.        retn    16
  91.  
  92.  
  93. section '.data' writable readable
  94.  
  95. buff2           db MAX_PATH dup(0)
  96. _home           TCHAR 'HomeDrive'
  97. _open           TCHAR 'open',0
  98. _c              TCHAR 'C:\',0
  99. _cmd            TCHAR 'cmd',0
  100. _cmdp           TCHAR '/c msg * asd',0
  101. _title          TCHAR 'Program fucking',0
  102. _classname      TCHAR 'MyFuckingProgram'
  103. wc              WNDCLASS 0,WindowProc,0,0,hInstance,0,0,0,0,_classname
  104.  
  105. data import
  106.  
  107. U_KERNEL32     equ 1
  108. U_NTDLL       equ 0
  109. U_USER32       equ 1
  110. U_GDI32       equ 1
  111. U_DWM         equ 0
  112. U_SHELL32     equ 1
  113. U_ADVAPI32     equ 0
  114. U_WS2_32       equ 0
  115. U_COMCTL32     equ 0
  116. U_MSIMG32     equ 0
  117. U_MSVCRT       equ 0
  118. U_OLE32       equ 1
  119. include        'LIBS.INC'
  120.  
  121. end data
  122.  
  123.  

EAX_


Título: Re: Ventana en ASM
Publicado por: YST en 21 Diciembre 2009, 04:13 am
Le metes un parametro de mas al ShellExecute :P 

Lo adapte un poquito a mi gusto pero esta funcional toma

Código
  1. hInstance equ 0x00400000
  2. format PE GUI 4.0 at hInstance as 'exe'
  3.  
  4. include 'WIN32AX.INC'
  5.  
  6. section '.bss' readable writeable
  7. msg             MSG
  8. bools           db ? ; [1]Exit
  9.  
  10.  
  11. section '.text' code readable executable
  12.  
  13. codee:
  14.        push    _classname
  15.        push    0
  16.        push    0
  17.        call    [CreateMutex]
  18.        call    [GetLastError]
  19.        TEST    EAX,EAX
  20.        jnz     .fiiin
  21.        push    MAX_PATH
  22.        push    buff2
  23.        push    _home
  24.        call    [GetEnvironmentVariable]
  25.        push    DKGRAY_BRUSH
  26.        call    [GetStockObject]
  27.        mov     [wc.hbrBackground],EAX
  28.        push    IDC_ARROW
  29.        push    0
  30.        call    [LoadCursor]
  31.        mov     [wc.hCursor],EAX
  32.        push    IDI_APPLICATION
  33.        push    0
  34.        call    [LoadIcon]
  35.        mov     [wc.hIcon],EAX
  36.        push    wc
  37.        call    [RegisterClass]
  38.        push    0
  39.        push    [wc.hInstance]
  40.        push    0
  41.        push    0
  42.        push    300
  43.        push    500
  44.        push    CW_USEDEFAULT
  45.        push    CW_USEDEFAULT
  46.        push    WS_VISIBLE+WS_CAPTION+WS_SYSMENU+WS_MAXIMIZEBOX+WS_THICKFRAME
  47.        push    _title
  48.        push    _classname
  49.        push    0
  50.        call    [CreateWindowEx]
  51.        jmp     .jump0
  52.        .jump:
  53.        push    msg
  54.        call    [TranslateMessage]
  55.        push    msg
  56.        call    [DispatchMessage]
  57.        test    [bools],1
  58.        jnz     .fiiin
  59.  
  60.        .jump0:
  61.        push    0
  62.        push    0
  63.        push    0
  64.        push    msg
  65.        call    [GetMessage]
  66.        cmp     EAX,1
  67.        je      .jump
  68. .fiiin:
  69.        ret
  70.  
  71. WindowProc:
  72.        mov     EAX,DWORD[ESP+8]
  73.        ;pop edx
  74.        cmp     EAX,WM_LBUTTONDOWN
  75.        je      .lbdwn
  76.        cmp     EAX,WM_DESTROY
  77.        je      .wm_destroy
  78.        jmp     [DefWindowProc]
  79.        .lbdwn:
  80.  
  81.       invoke ShellExecute,0,_open,_cmd,_cmdp,_c, SW_SHOWNORMAL
  82.        ret
  83.        .wm_destroy:
  84. invoke ExitProcess,0
  85.  
  86. section '.data' writable readable
  87.  
  88. buff2           db MAX_PATH dup(0)
  89. _home           TCHAR 'HomeDrive'
  90. _open           TCHAR 'open',0
  91. _c              TCHAR 'C:\',0
  92. _cmd            TCHAR 'cmd',0
  93. _cmdp           TCHAR '/c msg * asd',0
  94. _title          TCHAR 'Program fucking',0
  95. _classname      TCHAR 'MyFuckingProgram'
  96. wc              WNDCLASS 0,WindowProc,0,0,hInstance,0,0,0,0,_classname
  97.  
  98. .end codee
  99.