flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2, 3, 4 Next |
Author |
|
drobole 12 Nov 2010, 11:05
I will leave that as an exercise for you
![]() You did a good job trying to make things work on your own so I'll give you a hint how I would do it. 1. Create a new buffer (mainBuff) that you append to as you receive more. Make sure it has a 0 at the first byte before you start using it. 2. Insted of printing in the loop you use the c-function strcat to append the tmpBuff to the mainBuff 3. Print mainBuff after the loop is finished PS. Thanks for the ExitProcess hint |
|||
![]() |
|
Overflowz 12 Nov 2010, 11:20
I'll think about that. Thanks. and 1 more question, I hate msvcrt so could I do just lstrcat from kernel32 ?
|
|||
![]() |
|
drobole 12 Nov 2010, 12:08
You could probably use the lstrcat function, but according to MSDN
http://msdn.microsoft.com/en-us/library/ms647487(VS.85).aspx the lstrcat function is not recommended. Instead, you are supposed to use StringCchCat function http://msdn.microsoft.com/en-us/library/ms647518(v=VS.85).aspx I'm not sure if this function is in kernel32.dll though. It doesn't say... You could also append manually of course if you create a counter that keeps track of where you are in the mainBuff. |
|||
![]() |
|
Overflowz 12 Nov 2010, 12:15
I don't understand.. if binary file has already null-byte then it will stop then right ? When I'm converting with lstrcpy it just shows "dd" message not other thing.. whats problem ? I'm sending 4096 dup 0x41 and 4 dup 0x42 4096 = receive size and 4 = for looping to show result AAA..BBBB but it shows nothing from them.. just "dd" whats problem ? >.<
|
|||
![]() |
|
drobole 12 Nov 2010, 12:33
Quote:
The printf function will stop at the first 0 it finds. I'm not sure what you are trying to do. Show the code please |
|||
![]() |
|
Overflowz 12 Nov 2010, 16:19
here's server code for receiving and save all data into mainBuff (my guess):
Code: format PE console 4.0 include 'WIN32AX.INC' entry main section '.data' data readable writeable hSock dd ? hSock2 dd ? wsaData WSADATA saddr sockaddr_in sizeof.saddr = $ - saddr saddrlen dd sizeof.sockaddr_in w_startup db "Initializing winsock...",10,0 w_socket db "Creating new socket...",10,0 w_bind db "Binding on port 7100...",10,0 w_listen db "Going on Listening State...",10,0 w_accept db "Success, Listening on port 7100",10,0 o_msg db "Received request: ",0 tmpBuff rb 100 mainBuff db 100 section '.text' code readable executable proc main cinvoke printf, w_startup invoke WSAStartup, 0202h, wsaData cinvoke printf, w_socket invoke socket, AF_INET, SOCK_STREAM, 0 mov [hSock], eax mov [saddr.sin_family], AF_INET mov [saddr.sin_addr], 0 invoke htons, 7100 mov [saddr.sin_port], ax cinvoke printf, w_bind invoke bind, [hSock], saddr, sizeof.sockaddr_in cinvoke printf, w_listen invoke listen, [hSock], 1 cinvoke printf, w_accept invoke accept, [hSock], saddr, saddrlen mov [hSock2], eax recv_loop: invoke recv, [hSock2], tmpBuff, 100, 0 cmp eax, 0 jle end_loop mov [tmpBuff+eax],0 invoke lstrcpy,mainBuff,tmpBuff jmp recv_loop end_loop: invoke closesocket,[hSock2] invoke closesocket,[hSock] invoke WSACleanup cinvoke printf,mainBuff invoke ExitProcess,0 endp section '.idata' import data readable library kernel32,'kernel32.dll',\ ws2_32,'ws2_32.dll',\ msvcrt,'msvcrt.dll' include 'API\KERNEL32.INC' import ws2_32, WSAStartup, 'WSAStartup',\ WSACleanup, 'WSACleanup',\ socket, 'socket',\ htons, 'htons',\ bind, 'bind',\ listen, 'listen',\ accept, 'accept',\ recv, 'recv',\ closesocket, 'closesocket' import msvcrt, printf, 'printf' section '.reloc' fixups data discardable and here's send client what are sending 104 bytes to server: Code: format PE console 4.0 include 'WIN32AX.INC' entry main section '.data' data readable writeable CR EQU 0x0D LF EQU 0x0A m_pause db "pause>NUL",0 request1 db 100 dup 0x41 request2 db 4 dup 0x42 req_end db 0 sizeof.request1 = $ - request1 szIp db "127.0.0.1",0 sizeof.szIp = $ - szIp hSock dd ? saddr sockaddr_in sizeof.saddr = $ - saddr wsaData WSADATA section '.text' code readable executable proc main invoke WSAStartup,0202h,wsaData invoke socket,AF_INET,SOCK_STREAM,0 mov [hSock],eax mov [saddr.sin_family],AF_INET invoke htons,7100 mov [saddr.sin_port],ax invoke inet_addr,szIp mov [saddr.sin_addr],eax invoke connect,[hSock],saddr,sizeof.saddr invoke send,[hSock],request1,sizeof.request1,0 invoke closesocket,[hSock] invoke WSACleanup invoke ExitProcess,0 endp section '.idata' import data readable library user32,'user32.dll',\ kernel32,'kernel32.dll',\ ws2_32,'ws2_32.dll',\ msvcrt,'msvcrt.dll' include 'API\USER32.INC' include 'API\KERNEL32.INC' include 'API\WS2_32.INC' import msvcrt,printf,'printf' section '.reloc' fixups data discardable P.S I have WS2_32.INC (Thanks to Picnic) so problem is it only shows "d" after sending all this data.. maybe check it out ? Sorry I had mistake.. "lstrcpy,tmpBuff,mainBuff" -- fixed "printf,mainBuff" in loop -- fixed but now it shows only "BBBB" why ?.. |
|||
![]() |
|
Overflowz 12 Nov 2010, 16:36
another problem fixed.. I was using lstrcpy instead of lstrcat.. I'll see what's difference about them. WORKS Perfect now. Thank you!
![]() |
|||
![]() |
|
drobole 12 Nov 2010, 16:56
Thats good.
Just a little headsup The tmpBuff should be at least 101 bytes. 100 bytes received from recv 1 more byte because we add a 0 manually at the end The mainBuff should be at least 105 bytes since we are sending 104 bytes in total (+1 because lstrcat adds a 0 at the end automatically), but actually I would make it much bigger. That way you can continue to add more strings to it when the time comes mainBuff db 8192 edit: Actually the easier way is to make the buffers more than big enough so that we don't get a buffer overrun Anyway, grats with a working socket ![]() |
|||
![]() |
|
Overflowz 12 Nov 2010, 19:41
If I don't know size of file.. How would I do that then ? :/
|
|||
![]() |
|
drobole 12 Nov 2010, 20:36
Well, I would probably look into the GlobalAlloc and GlobalReAlloc functions.
You could allocate 1MB with Alloc and start appending data to that buffer in the recv loop. Then, when you have used all those 1MB and data is still coming in, you could use GlobalReAlloc to increase the size of the buffer. That way you could keep increasing the size of the buffer until there is no more data coming in. I'm not all that familiar with using Windows API and assembly so I won't try to give any example of that, but thats what I would look into You could probably do the same thing with the c-functions malloc/realloc |
|||
![]() |
|
Overflowz 12 Nov 2010, 20:44
Well. I don't know C good.. anyway thanks I got all things now.. and I don't understand whats difference between "less or equal" and "above or equal".. JLE and JAE.
|
|||
![]() |
|
drobole 12 Nov 2010, 21:17
Overflowz wrote: Well. I don't know C good.. anyway thanks I got all things now.. and I don't understand whats difference between "less or equal" and "above or equal".. JLE and JAE. Its a lot of stuff going on with all the jump instructions. Some of the jump instructions will only jump if the carry flag is clear and the zero flag is set, others doesn't care about the carry flag etc. etc. etc I'm really not the right guy to ask since I haven't been using assembly for very long. Maybe someone else around here has a good description of them |
|||
![]() |
|
Overflowz 12 Nov 2010, 21:40
Okay I'll do that later.. but I'm stuck about sending request and receive that back + move to main buffer and print it.. but I fail I tried everything and where I was near I'll post that code..
Code: format PE console 4.0 include 'WIN32AX.INC' entry main CR EQU 0x0D LF EQU 0x0A section '.data' data readable writeable hSock dd ? hSock2 dd ? wsaData WSADATA saddr sockaddr_in sizeof.saddr = $ - saddr saddrlen dd sizeof.sockaddr_in szIP db "127.0.0.1",0 w_startup db "Initializing winsock...",10,0 w_socket db "Creating new socket...",10,0 w_bind db "Binding on port 7100...",10,0 w_listen db "Going on Listening State...",10,0 w_accept db "Success, Listening on port 7100",10,0 o_msg db "Received request: ",0 send1 db CR,LF,"HEAD / HTTP/1.1",CR,LF send2 db "Host:localhost",CR,LF send3 db "Connection:Close",CR,LF,CR,LF,0 sizeof.send1 = $ - send1 tmpBuff rb 100 mainBuff rb 100 buffend db 0 section '.text' code readable executable proc main cinvoke printf, w_startup invoke WSAStartup, 0202h, wsaData cinvoke printf, w_socket invoke socket, AF_INET, SOCK_STREAM, 0 mov [hSock], eax mov [saddr.sin_family], AF_INET invoke inet_addr,szIP mov [saddr.sin_addr],eax ;mov [saddr.sin_addr], 0 ;invoke htons, 7100 invoke htons,80 mov [saddr.sin_port], ax ;cinvoke printf, w_bind ;invoke bind, [hSock], saddr, sizeof.sockaddr_in ;cinvoke printf, w_listen ;invoke listen, [hSock], 1 ;cinvoke printf, w_accept ;invoke accept, [hSock], saddr, saddrlen ;mov [hSock2], eax invoke connect,[hSock],saddr,sizeof.sockaddr_in invoke send,[hSock],send1,sizeof.send1,0 mov esi,tmpBuff mov edi,mainBuff recv_loop: ;invoke recv, [hSock2], tmpBuff, 100, 0 invoke recv,[hSock],tmpBuff,100,0 cmp eax, 0 jle end_loop ;mov [tmpBuff+eax],0 ;cinvoke printf,tmpBuff ;invoke lstrcat,mainBuff,tmpBuff push eax .load_up: mov ecx,100 rep movsb mov [tmpBuff],0 pop eax jmp recv_loop end_loop: ;invoke closesocket,[hSock2] invoke closesocket,[hSock] invoke WSACleanup cinvoke printf,mainBuff invoke Sleep,-1 invoke ExitProcess,0 endp section '.idata' import data readable library kernel32,'kernel32.dll',\ ws2_32,'ws2_32.dll',\ msvcrt,'msvcrt.dll' include 'API\KERNEL32.INC' import ws2_32, WSAStartup, 'WSAStartup',\ WSACleanup, 'WSACleanup',\ socket, 'socket',\ htons, 'htons',\ bind, 'bind',\ listen, 'listen',\ accept, 'accept',\ recv, 'recv',\ closesocket, 'closesocket',\ inet_addr,'inet_addr',\ connect,'connect',\ send,'send' import msvcrt, printf, 'printf' section '.reloc' fixups data discardable Hope someone will fix and explain why I fail on this code.. I attached it in debugger but I don't understand.. Instead of replacing buffer it just moves data and replacing string and + other strings are left there.. I've tried mov [tmpBuff],0 but no result.. Anyone help please ![]() |
|||
![]() |
|
drobole 13 Nov 2010, 12:06
Yo,
I have been playing with your code again. I haven't got to playing with the string manipulation stuff yet so my example gets by by using only basic instructions. WARNING: I'm just playing around here so this is not how the pro's would do it, and it may contain hazardious constructs ![]() What I wanned to achieve here is to write a server that can receive any amount of ASCII data, increasing the buffer size as needed. (Limited by free available memory of course) server.asm Code: format PE console 4.0 include 'WIN32AX.INC' entry main section '.data' data readable writeable hSock dd ? hSock2 dd ? wsaData WSADATA saddr sockaddr_in sizeof.saddr = $ - saddr saddrlen dd sizeof.sockaddr_in w_dbg_fmt db "Allcating memory - Bytes read so far: %d, Current mainBuff size: %d",10,0 w_startup db "Initializing winsock...",10,0 w_socket db "Creating new socket...",10,0 w_bind db "Binding on port 7100...",10,0 w_listen db "Going on Listening State...",10,0 w_accept db "Success, Listening on port 7100",10,0 tmpBuff rb 32 sizeof.tmpBuff = $ - tmpBuff mainBuff dd 0 ; Varialbe to hold the address of our main buffer. Must be initalized with 0 so that realloc will work as malloc the first time (see documentation of realloc) mainBuffSize dd 0 ; The size of the mainBuff. This will increase as we allocate more memory bytesRead dd 0 ; Counter to keep track of number of bytes read from socket blockSize equ 256 ; Increase mainBuff with this size every time we need more memory section '.text' code readable executable ; Assuming that sizeof.tmpBuff <= blockSize. If not, everything will explode alloc_block: add [mainBuffSize], blockSize cinvoke realloc, [mainBuff], [mainBuffSize] mov [mainBuff], eax cmp [mainBuffSize], blockSize jne alloc_block_exit mov [eax], byte 0 ; If this is the first block allocated, we must add a 0 byte at the start to make it a valid empty string alloc_block_exit: ret proc main cinvoke printf, w_startup invoke WSAStartup, 0202h, wsaData cinvoke printf, w_socket invoke socket, AF_INET, SOCK_STREAM, 0 mov [hSock], eax mov [saddr.sin_family], AF_INET mov [saddr.sin_addr], 0 invoke htons, 7100 mov [saddr.sin_port], ax cinvoke printf, w_bind invoke bind, [hSock], saddr, sizeof.sockaddr_in cinvoke printf, w_listen invoke listen, [hSock], 1 cinvoke printf, w_accept invoke accept, [hSock], saddr, saddrlen mov [hSock2], eax recv_loop: invoke recv, [hSock2], tmpBuff, sizeof.tmpBuff - 1, 0 ; Fill tmpBuff with up to 31 characters cmp eax, 0 jle end_recv_loop mov [tmpBuff + eax], 0 ; Add a 0 as the 32'nd character, so that this is a valid string inc eax ; Adding 1 to eax since we just added a byte to the tmpBuff manually add [bytesRead], eax ; Update bytesRead mov eax, [bytesRead] cmp eax, [mainBuffSize] ; Compare bytesRead and mainBuffSize jle append_block ; If the mainBuff is big enough for another block, skip to lstrcat cinvoke printf, w_dbg_fmt, [bytesRead], [mainBuffSize] ; Print some debug info call alloc_block ; Increase the size of mainBuff append_block: invoke lstrcat, [mainBuff], tmpBuff ; mainBuff holds the address of our buffer (not the buffer itself) so [] is needed jmp recv_loop end_recv_loop: cinvoke printf, [mainBuff] cinvoke free, [mainBuff] ; Since we are allocating memory from the heap we must remeber to free it (Not necessary but good practice) invoke closesocket, [hSock2] invoke closesocket, [hSock] invoke WSACleanup invoke ExitProcess, 0 endp section '.idata' import data readable library kernel32,'kernel32.dll',\ ws2_32,'ws2_32.dll',\ msvcrt,'msvcrt.dll' include 'API\KERNEL32.INC' include 'API\WSOCK32.INC' import ws2_32,\ WSAStartup, 'WSAStartup',\ WSACleanup, 'WSACleanup',\ socket, 'socket',\ htons, 'htons',\ bind, 'bind',\ listen, 'listen',\ accept, 'accept',\ recv, 'recv',\ closesocket, 'closesocket' import msvcrt,\ printf, 'printf',\ realloc, 'realloc',\ free, 'free' section '.reloc' fixups data discardable client.asm Code: include 'WIN32AX.INC' entry main section '.data' data readable writeable request1 db 1000 dup 0x41 request2 db 10 dup 0x42 sizeof.request1 = $ - request1 szIp db "127.0.0.1", 0 sizeof.szIp = $ - szIp hSock dd ? saddr sockaddr_in sizeof.saddr = $ - saddr wsaData WSADATA section '.text' code readable executable proc main invoke WSAStartup, 0202h, wsaData invoke socket, AF_INET, SOCK_STREAM, 0 mov [hSock], eax mov [saddr.sin_family], AF_INET invoke htons, 7100 mov [saddr.sin_port], ax invoke inet_addr, szIp mov [saddr.sin_addr], eax invoke connect, [hSock], saddr, sizeof.saddr invoke send, [hSock], request1, sizeof.request1, 0 invoke closesocket, [hSock] invoke WSACleanup invoke ExitProcess, 0 endp section '.idata' import data readable library user32,'user32.dll',\ kernel32,'kernel32.dll',\ ws2_32,'ws2_32.dll',\ msvcrt,'msvcrt.dll' include 'API\USER32.INC' include 'API\KERNEL32.INC' include 'API\WSOCK32.INC' import ws2_32,\ WSAStartup, 'WSAStartup',\ WSACleanup, 'WSACleanup',\ socket, 'socket',\ htons, 'htons',\ inet_addr, 'inet_addr',\ connect, 'connect',\ send, 'send',\ closesocket, 'closesocket' import msvcrt,printf,'printf' section '.reloc' fixups data discardable We should be able to increase/decrease the number of bytes sent from the client, and the server should handle it[/b] |
|||
![]() |
|
Overflowz 13 Nov 2010, 14:03
Yep it's hard for me.. I should learn about alloc/malloc functions later..
![]() |
|||
![]() |
|
drobole 13 Nov 2010, 14:48
Thats cool.
I'm learning this stuff myself as I go along so I figured I might as well post it. Maybe someone more experienced will come around and point out any mistakes or improvements. You never know ![]() |
|||
![]() |
|
drobole 13 Nov 2010, 15:27
I baked a little example that retrieves a web page.
You can combine it with the other example I posted if you want to store the web page in memory rather than just printing it Code: format PE console 4.0 include 'WIN32AX.INC' entry main CR EQU 0x0D LF EQU 0x0A section '.data' data readable writeable hSock dd ? wsaData WSADATA saddr sockaddr_in sizeof.saddr = $ - saddr saddrlen dd sizeof.sockaddr_in szIP db "128.30.52.37", 0 send_http_request db "GET / HTTP/1.1",CR,LF,"Host: www.w3.org",CR,LF,"Connection: close",CR,LF,CR,LF sizeof.send_http_request = $ - send_http_request cbuf rb 1024 section '.text' code readable executable proc main invoke WSAStartup, 0202h, wsaData invoke socket, AF_INET, SOCK_STREAM, 0 mov [hSock], eax mov [saddr.sin_family], AF_INET invoke inet_addr, szIP mov [saddr.sin_addr], eax invoke htons, 80 mov [saddr.sin_port], ax invoke connect, [hSock], saddr, sizeof.sockaddr_in invoke send, [hSock], send_http_request, sizeof.send_http_request, 0 recv_loop: invoke recv, [hSock], cbuf, 1024-1, 0 cmp eax, 0 jle end_loop mov [cbuf + eax], 0 cinvoke printf, cbuf jmp recv_loop end_loop: invoke closesocket, [hSock] invoke WSACleanup invoke ExitProcess, 0 endp section '.idata' import data readable library kernel32,'kernel32.dll',\ ws2_32,'ws2_32.dll',\ msvcrt,'msvcrt.dll' include 'API\KERNEL32.INC' include 'API\WSOCK32.INC' import ws2_32, WSAStartup, 'WSAStartup',\ WSACleanup, 'WSACleanup',\ socket, 'socket',\ htons, 'htons',\ recv, 'recv',\ closesocket, 'closesocket',\ inet_addr,'inet_addr',\ connect,'connect',\ send,'send' import msvcrt, printf, 'printf' section '.reloc' fixups data discardable |
|||
![]() |
|
Overflowz 13 Nov 2010, 16:38
Nice code. works fine but Is there any way to do without add null-byte to cbuf+eax ? Cause I think it won't work when downloading binary file.. :/
|
|||
![]() |
|
drobole 13 Nov 2010, 17:04
Sure, if you download anything else than ASCII text you can change 2 lines from that code
invoke recv, [hSock], cbuf, 1024-1, 0 becomes invoke recv, [hSock], cbuf, 1024, 0 There is no need to reserve one byte at the end of the buffer anymore since we are not going to add a 0 at the end later and the line mov [cbuf + eax], 0 can just be removed. If you do it like that it should work for anything including binary data but you can not pass it to string functions like printf or lstrcat etc. To do that the buffer must have a 0 byte at the end. How you go about adding that 0 is really up to you. Also, you said its hard for you, but you shouldn't think like that. Its hard for all of us. I have been programming C, C++ and C# for almost 20 years now so its easier for me to avoid problems. But you will get there ![]() |
|||
![]() |
|
Goto page Previous 1, 2, 3, 4 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.