flat assembler
Message board for the users of flat assembler.

Index > Windows > Help aply a algorythm to a string

Author
Thread Post new topic Reply to topic
||CyrUS||



Joined: 11 Mar 2006
Posts: 11
||CyrUS||
Hi can you help me. I have a problem with my code, I want to aply an algorytm to all the chars of the name and to save it in serial. after I want to create a file with the serial. Can you tell me wath is wrong?

format PE GUI 4.0
entry start

include 'win32a.inc'

IDC_EDIT1 = 1000

section '.data' data readable writable

name db 51h
serial db 51h
filenam db "file.nfo",0
section '.code' code readable executable

start:

invoke GetModuleHandle,0
invoke DialogBoxParam,eax,101,HWND_DESKTOP,DialogProc,0
or eax,eax
jz exit

exit:
invoke ExitProcess,0

proc DialogProc hwnddlg,msg,wparam,lparam
push ebx esi edi
cmp [msg],WM_INITDIALOG
je wminitdialog
cmp [msg],WM_COMMAND
je wmcommand
cmp [msg],WM_CLOSE
je wmclose
xor eax,eax
jmp finish
wminitdialog:

jmp processed
wmcommand:
cmp [wparam],BN_CLICKED shl 16 + IDCANCEL
je wmclose
cmp [wparam],BN_CLICKED shl 16 + IDOK
jne processed
invoke GetDlgItemText,[hwnddlg],IDC_EDIT1,name,50h
mov eax,name
mov ebx,eax
xor ecx,ecx

loop1: mov al,byte ptr name ;algo
rol al,2
add al,32
xor al,15
xor al,16
rol al,2
add al,32
xor al,15
xor al,16
mov byte ptr serial
add ecx,1
inc eax
cmp ebx,ecx
jnz loop1 ; end algo
invoke MessageBox,HWND_DESKTOP,serial,serial,MB_OK
xor eax,eax
invoke CreateFile,filenam,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0
xor eax,eax

wmclose:
invoke EndDialog,[hwnddlg],0
processed:
mov eax,1
finish:
pop edi esi ebx
ret
endp

section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
user,'USER32.DLL'

import kernel,\
GetModuleHandle,'GetModuleHandleA',\
ExitProcess,'ExitProcess',\
CreateFile,'CreateFileA'

import user,\
DialogBoxParam,'DialogBoxParamA',\
EndDialog,'EndDialog',\
GetDlgItemText,'GetDlgItemTextA',\
MessageBox,'MessageBoxA'

section '.rsrc' resource from 'C:\fasmw165\EXAMPLES\DIALOG\aa.res' data readable

sorry for my english if there is some errors Smile
Post 11 Mar 2006, 16:14
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
There's a handful of errors that I saw right off ...

1- name DB 51h means you are defining 1 byte with the value 51h
You want name RB 51h which means you are reserving 51h bytes for the name. This goes for serial as well.

2- You use PTR in some cases and in other cases you don't. Get rid of all the PTR's. For instance, mov al,byte ptr name
should be mov al, byte[name]

3- To get the value stored in memory you need to put that memory address in brackets []. So to get the first byte's value in the name you need to use mov al, byte [name]. To get the address of name you don't use brackets and make sure the destination is 32bits so
mov eax, name would give you the address in memory of the name.

4- You didn't check your code carefully. A little syntax error
mov byte ptr serial, I assume you want to store al in the appropriate location in serial, so mov byte [serial], al will work.

5- Your algorithm doesn't iterate through the characters in name or serial.
Code:
invoke GetDlgItemText,[hwnddlg],IDC_EDIT1,name,50h 
mov ecx,eax
mov byte[serial + ecx], 00h ;;make sure the null is at the end
dec ecx 
;;we will iterate through the characters backwards

loop1: 
mov al,byte [name + ecx] ;algo 
rol al,2 
add al,32 
xor al,15 
xor al,16 
rol al,2 
add al,32 
xor al,15 
xor al,16 
mov byte [serial + ecx], al 
dec ecx ;;while ecx >= 0 keep going
jns loop1 ; end algo
    

The above algorithm should work the way you inteded.

There may be more bugs, but assembly programming is best learned by trial, error, and research.
Post 12 Mar 2006, 03:14
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
||CyrUS||



Joined: 11 Mar 2006
Posts: 11
||CyrUS||
Thank you very much. It works.

One more question how can hi get the handle of a CreateFile and do a WriteFile to the file I have created?
Post 12 Mar 2006, 15:51
View user's profile Send private message Reply with quote
||CyrUS||



Joined: 11 Mar 2006
Posts: 11
||CyrUS||
Code:

invoke  CreateFile,filenam,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        invoke  GetDlgItemText,[hwnddlg],IDC_EDIT1,name,50
        mov ecx,eax
        mov byte[serial + ecx], 00h ;;make sure the null is at the end
        dec ecx
                                    ;;we will iterate through the characters backwards
        mov ebx,ecx
        loop1:
        mov al,byte [name + ecx] ;algo
        rol al,2
        add al,32
        xor al,15
        xor al,16
        rol al,2
        add al,32
        xor al,15
        xor al,16
        mov byte [serial + ecx], al
        dec ecx ;;while ecx >= 0 keep going
        jns loop1 ; end algo

        invoke  MessageBox,HWND_DESKTOP,serial,serial,MB_OK
        invoke  WriteFile,ebp,serial,ebx,0,0              ; ebp??? hfile??       
Post 12 Mar 2006, 15:52
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
not sure but i think CreateFile returns the handle to created file if it was successful

to where ever you keep data
Code:
hFile rd 1    

for CreateFile
Code:
invoke  CreateFile,filenam,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
mov [hFile],eax    

for WriteFile
Code:
invoke  WriteFile,[hFile],serial,ebx,0,0    

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 12 Mar 2006, 16:17
View user's profile Send private message MSN Messenger Reply with quote
||CyrUS||



Joined: 11 Mar 2006
Posts: 11
||CyrUS||
Thanks!!

I dont understand why: when I run the program writefile writes the correct bytes in the file and the program crash and leave.


Code:
 invoke  CreateFile,filenam,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        mov [hFile],eax

        invoke  GetDlgItemText,[hwnddlg],IDC_EDIT1,name,50
        mov ecx,eax
        mov byte[serial + ecx], 00h ;;make sure the null is at the end
        mov ebx,ecx
        dec ecx
        loop1:
        mov al,byte [name + ecx] ;algo
        rol al,2h
        add al,32h
        xor al,15h
        xor al,16h

        rol al,2h
        add al,32h
        xor al,15h
        xor al,16h
        mov byte [serial + ecx], al
        dec ecx ;;while ecx >= 0 keep going
        jns loop1 ; end algo
        invoke  WriteFile,[hFile],serial,ebx,0,0            ; ebp??? hfile??
        invoke  CloseHandle,[hFile]       
Post 12 Mar 2006, 18:21
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
Your 2nd to last parameter in WriteFile needs to be a memory address so the api can store how many bytes were written.

A quick hack such as
invoke WriteFile,[hFile],serial,ebx,esp-16,0
should fix the error.
Post 12 Mar 2006, 19:54
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
can you push "esp-16"? Maybe [esp-16]...or you can sub 16 before pushing it
...anyway you can try erasing the handle Wink
Code:
invoke  WriteFile,[hFile],serial,ebx,0,0
    

Because handles opened by the program are mercylessly closed when program is shut down Wink But that is not good programming practice.
You should define
Code:
invoke  WriteFile,[hFile],serial,ebx,WrittenBytes,0
;...
WrittenBytes dd ?
    

instead
Post 13 Mar 2006, 08:07
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
||CyrUS||



Joined: 11 Mar 2006
Posts: 11
||CyrUS||
I've found how to do it work!!

Code:

 hFile dd ?
 written dd ?

     


Code:

invoke WriteFile,[hFile],serial,ebx,written,NULL
    


Thanks all!!
Post 14 Mar 2006, 17:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.