The MASM Forum

General => The Campus => Topic started by: Bugaga on April 02, 2020, 08:05:30 PM

Title: Masm32 password input dialog window
Post by: Bugaga on April 02, 2020, 08:05:30 PM
Hello to the forum! I am very novice to assembly, so want to say sorry if my question is too weird  :dazzled:

I want to write a simple program that outputs (or not) one piece of data based on if password did match or not. The password must be xor encrypted (there's not much of a problem here).

I found a great example in examples/dialogs/ called "nested". It is almost perfectly what I need, but with all the code in .inc file + main file, it is too complex for me.

So I wanted to ask, maybe there are simplier examples of the input window? Or maybe this nested example can be trimmed down a little bit? I actually only need one input window and a result MessageBox based on the input. Or, if not all of that, maybe someone could comment just a little bit of the code in this "nested" example to show the path of the input data from the moment I input it to the moment it invokes in MessageBox, so I would know where I need to modify the code? I am beforehand thankful to any help  :thumbsup:

Update:
Here is the gist for this "nested" example, the thing I changed are printed strings and added all the code from the .inc file to this one .asm file, that's it - https://gist.github.com/Buogogo/dce2469dc3af9ec711ec01dfb23f0817
Title: Re: Masm32 password input dialog window
Post by: Vortex on April 03, 2020, 02:03:59 AM
Hello,

Maybe not the best option but this can give you an idea :

http://www.masmforum.com/board/index.php?topic=18485.0
Title: Re: Masm32 password input dialog window
Post by: jj2007 on April 03, 2020, 02:40:37 AM
Are you eager to roll your own, or do you just want a job done?

include \masm32\MasmBasic\MasmBasic.inc         ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
 DlgDefine "Hi there:", 0, 0, 120, -2, , 12    ; x, y ignored, 120 wide, 2 lines, blank, font size 12
  DlgControl dcEdit,   "歡迎", WS_BORDER or WS_TABSTOP or ES_PASSWORD, 1, -2
  DlgControl dcStatic, "Type your password:", SS_LEFT, 1, -1, 70.0
  DlgControl dcButton, "OK", BS_DEFPUSHBUTTON or WS_TABSTOP, 60.0, -1, 15.0, , IDOK
  DlgControl dcButton, "Cancel", BS_PUSHBUTTON or WS_TABSTOP, 77.0, -1, 23.0, , IDCANCEL
  DlgShow
  .if eax==IDOK                         ; user clicked OK, let's see what was typed into the edit box:
        .if StringsDiffer(Utf8$(Dlg$(0)), "歡迎", 1)  ; 1=case-insensitive
                MsgBox 0, "No cigar!!!!", "Retry?", MB_YESNO
                If_ eax==IDYES Then Launch "Password.exe"       ; just for fun, try again
        .else
                wMsgBox 0, Dlg$(0), "Congrats!!!!", MB_OK
                ; insert ACTION here
        .endif
  .endif
EndOfCode


Source attached. As you can see, it works with Unicode, too.
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 03, 2020, 03:36:43 AM
Quote from: jj2007 on April 03, 2020, 02:40:37 AM
Are you eager to roll your own, or do you just want a job done?
Thank you for this example! I would like to do at least something myself, if you mean that  :biggrin:. Is there a way to do this using just bare masm32 libs? If there were just simpliest ever example with inputbox and result with if condition in MessageBox - I think I would be able to add what I need it to do. It would just be good to see the simpliest example that I would understand, but I am struggling to find any on the Internet.
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 03, 2020, 03:42:56 AM
Maybe it would be better to state question like that: if I needed to insert xor encryption of input, "if" comparing it to the actual password and then output, where in the example I attached is the best place to insert it? Cause I don't even understand what variable\register holds the input after confirmation.
Title: Re: Masm32 password input dialog window
Post by: hutch-- on April 03, 2020, 09:22:48 AM
What you are after if you know how to write the code is a resource dialog that has a text box and a couple of buttons. When the dialog is closed you get the text in the text box into a buffer that you allocate then do what you like with the text data. The catch is that no-one will write it for you.
Title: Re: Masm32 password input dialog window
Post by: jj2007 on April 03, 2020, 11:44:19 AM
Quote from: Bugaga on April 03, 2020, 03:42:56 AM
Maybe it would be better to state question like that: if I needed to insert xor encryption of input, "if" comparing it to the actual password and then output, where in the example I attached is the best place to insert it? Cause I don't even understand what variable\register holds the input after confirmation.

After the .if eax==IDOK, insert mov esi, Dlg$(0). Now esi holds a pointer to the password text.
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 03, 2020, 11:19:35 PM
Quote from: jj2007 on April 03, 2020, 11:44:19 AM
After the .if eax==IDOK, insert mov esi, Dlg$(0). Now esi holds a pointer to the password text.
And may I ask where can it be in this "nested" example I attached? If I understand properly - the ecx register (or buffer var) holds it there, but I might be wrong.

And if I understand correctly, I need to code a check for a password match and ifs related to that somewhere here, in dlgproc, am I right?

dlgproc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD

    LOCAL buffer:DWORD

    .if uMsg == WM_INITDIALOG
      invoke SetWindowLong,hWin,GWL_USERDATA,lParam
      mov eax, lParam
      mov eax, [eax]
      invoke SendMessage,hWin,WM_SETICON,1,[eax]

    .elseif uMsg == WM_COMMAND
      .if wParam == IDOK
        invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT, 32
        mov buffer, eax
        invoke GetWindowLong,hWin,GWL_USERDATA
        mov ecx, [eax]
        invoke GetText,hWin,[ecx],buffer
        mov ecx, buffer
        cmp BYTE PTR [ecx], 0
        je @F
        invoke MessageBox,hWin,buffer,SADD("Target data"),MB_OK
        jmp nxt
      @@:
        invoke MessageBox,hWin,SADD("You did not enter any password"),
                               SADD("No data to display"),MB_OK
      nxt:
        invoke GlobalFree,buffer

      .elseif wParam == IDCANCEL
        jmp quit_dialog
      .endif

    .elseif uMsg == WM_CLOSE
      quit_dialog:
      invoke EndDialog,hWin,0

    .endif

    xor eax, eax
    ret

dlgproc endp
Title: Re: Masm32 password input dialog window
Post by: hutch-- on April 04, 2020, 01:08:00 AM
Do you know how to create a resource dialog with a dialog editor ?
Title: Re: Masm32 password input dialog window
Post by: jj2007 on April 04, 2020, 01:22:27 AM
Quote from: Bugaga on April 03, 2020, 11:19:35 PMAnd may I ask where can it be in this "nested" example I attached? If I understand properly - the ecx register (or buffer var) holds it there, but I might be wrong.

Yes, that's correct.
        mov ecx, buffer
        cmp BYTE PTR [ecx], 0
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 04, 2020, 01:30:52 AM
Quote from: hutch-- on April 04, 2020, 01:08:00 AM
Do you know how to create a resource dialog with a dialog editor ?

No, I never did that. Do I need to do it with Visual Studio? Now I am only using Masm32 Editor.
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 04, 2020, 02:49:57 AM
Quote from: jj2007 on April 04, 2020, 01:22:27 AM
Quote from: Bugaga on April 03, 2020, 11:19:35 PMAnd may I ask where can it be in this "nested" example I attached? If I understand properly - the ecx register (or buffer var) holds it there, but I might be wrong.

Yes, that's correct.
        mov ecx, buffer
        cmp BYTE PTR [ecx], 0

If I understand right, that is the place where I can add the comparison of variables:
je @F
        mov esi, buffer
        mov edi, password
        repe cmpsd
        je nequal
        invoke MessageBox,hWin,buffer,SADD("Target data"),MB_OK
        jmp nxt
        nequal:
            invoke MessageBox,hWin,SADD("You entered wrong password"),SADD("Wrong password"),MB_OK
            jmp nxt
      @@:
        invoke MessageBox,hWin,SADD("You did not enter any password"),
                               SADD("No data to display"),MB_OK
      nxt:
        invoke GlobalFree,buffer


The password is the dword variable with the password.
The problem is I tried using a debugger and when I do mov esi, buffer - the esi appears to be always different with same text input, that is quite strange. + on repe cmpsd I get Program received signal SIGSEGV, Segmentation fault. error and program exits. The thing I don't understand how to match my input (buffer I guess) and the password variable.
Title: Re: Masm32 password input dialog window
Post by: jj2007 on April 04, 2020, 07:34:36 AM
I have a suspicion that you should study the logic of repX cmpsd, and in particular the role of ecx
        mov esi, buffer
        mov edi, password
        repe cmpsd
Title: Re: Masm32 password input dialog window
Post by: hutch-- on April 04, 2020, 09:43:50 AM
> No, I never did that. Do I need to do it with Visual Studio? Now I am only using Masm32 Editor.

Get the dialog editor at URL http://masm32.com/board/index.php?topic=6512.0

You use it to make a resource script file (.rc). This is how to make the dialog you require for the text entry.

What we need to know is where the dialog will be called from, will it be a free standing executable OR will it be a dialog called from within an application because the code is very similar but is called from a different location in the main source.

The nested example that I wrote many years ago is probably a bit advanced for you at the moment as it creates the dialog in memory, a resource dialog is simpler and more orthodox in programming terms.
Title: Re: Masm32 password input dialog window
Post by: hutch-- on April 04, 2020, 12:14:10 PM
As learning dialogs from scratch is hard going, here is a working example of a proper resource dialog that does the basic interface stuff that you need. The main app is a resource dialog and it calls another resource dialog where you enter text. The text is then available to the caller and from there you can do whatever you need to do in terms of encryption and saving the data to disk.

Learning how to make dialogs is a bit complicated at first but once you are familiar with the techniques and code, they are reasonably straight forward after that.
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 04, 2020, 07:00:24 PM
Quote from: hutch-- on April 04, 2020, 12:14:10 PM
As learning dialogs from scratch is hard going, here is a working example of a proper resource dialog that does the basic interface stuff that you need. The main app is a resource dialog and it calls another resource dialog where you enter text. The text is then available to the caller and from there you can do whatever you need to do in terms of encryption and saving the data to disk.

Learning how to make dialogs is a bit complicated at first but once you are familiar with the techniques and code, they are reasonably straight forward after that.

I can't be thankful enough for your help and also others in this thread! Maybe it's me, but it was so hard to find any info about creating dialogs on Internet. :thumbsup:
Title: Re: Masm32 password input dialog window
Post by: Bugaga on April 05, 2020, 09:17:17 PM
Quote from: hutch-- on April 04, 2020, 12:14:10 PM
As learning dialogs from scratch is hard going, here is a working example of a proper resource dialog that does the basic interface stuff that you need.
I am sorry for more questions, in this example you attached, how the input is stored to pTxt variable? I see it is a DWORD, but it does not match with other DWORD strings I create. Tried using a debugger to see how the input is stored in pTxt, but no luck.
Here is the code:

; --------------------
  ; GLOBAL scope handles
  ; --------------------
    .data
      key dd 'pass', 0

And later:

    LOCAL pTxt  :DWORD
    LOCAL buffer[260]:BYTE

And later:

            lea eax, buffer                                         ; load buffer address
            mov pTxt, eax                                           ; cast it to a pointer
            invoke DialogBoxParam,hInstance,200,0,ADDR TxtProc,pTxt ; pass pointer to text dialog
          ; ----------------------------------------------------
          ; result if not cancelled is in buffer at pTxt address
          ; ----------------------------------------------------
            invoke lstrcmp, addr pTxt , addr key
            .if eax == 0
              fn MessageBox,hWin,pTxt,"Result",MB_OK
            .else
              fn MessageBox,hWin,"Wrong password!","Result",MB_OK
            .endif
Title: Re: Masm32 password input dialog window
Post by: hutch-- on April 05, 2020, 09:57:59 PM
pTxt is a pointer to a buffer, the example did it as a local to simplify the use of memory rather than having to dynamically allocate memory. That pointer is passed through the last argument,

invoke DialogBoxParam,hInstance,200,0,ADDR TxtProc,pTxt

to the second dialog procedure. In the second procedure the pointer is received by the "lParam" argument and copied to the decorated global variable.

        .data?
          pbuf@@@@ dd ?                                             ; decorate to prevent accidental duplicates
        .code

This is so the pointer is not lost when another message is processed by the 2nd dialog proc.

The code,

invoke GetWindowText,dlgEd,pbuf@@@@,260

gets the text from the edit box if it exists and passes it back via the code,

invoke EndDialog,hWin,pbuf@@@@

What you have back at the caller is the local memory filled with whatever has been typed into the edit box. It is tested for an empty string and if not a zero, the data is then stored in the buffer that "pTxt" points to.

Now for the comparison you want to do with your password, you then use a string compare algo to test if they are the same or not. To use the API function "lstrcmp" you need to ensure that you are comparing the same thing, the address of each string, the pTxt from the dialog AND the stored password that you want to compare it with. Also look up the return values for "lstrcmp" as they are the type used in sort algos.

Return Values

If the function succeeds and the string pointed to by lpString1 is less than the string pointed to by lpString2, the return value is negative; if the string pointed to by lpString1 is greater than the string pointed to by lpString2, it is positive. If the strings are equal, the return value is zero.