flat assembler
Message board for the users of flat assembler.

Index > Windows > Socket streams in Winsock/ASM?

Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 07 Aug 2007, 03:05
Is it possible?
How to do it?
And how hard it really is?

Thanks
Post 07 Aug 2007, 03:05
View user's profile Send private message Reply with quote
0.1



Joined: 24 Jul 2007
Posts: 474
Location: India
0.1 08 Aug 2007, 08:58
Here is an example by m. Hope it helps somewhat!
http://board.flatassembler.net/topic.php?t=6991&start=20
Post 08 Aug 2007, 08:58
View user's profile Send private message Reply with quote
fumeman



Joined: 16 Sep 2007
Posts: 1
fumeman 17 Sep 2007, 07:48
How to convert this example to PE64 console ????
Post 17 Sep 2007, 07:48
View user's profile Send private message Reply with quote
lehox



Joined: 06 Aug 2007
Posts: 16
lehox 17 Sep 2007, 22:38
I'm trying to port it to a pe from scratch.
I don't want to use the win32a.inc and the other winsock includes etc.

I cant use
mov word [my_addr+sockaddr_in.sin_family], 2; AF_INET
so what can I use instead?
Fasm says Illegal Instruction..

Greetings and Thanks
Post 17 Sep 2007, 22:38
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 17 Sep 2007, 22:52
If you are not using any includes then where is defined the structure?

And what says for you, illegal instruction or undefined symbol? It tells me the latter for me but it is solved by using "include 'win32a.inc'".
Post 17 Sep 2007, 22:52
View user's profile Send private message Reply with quote
lehox



Joined: 06 Aug 2007
Posts: 16
lehox 18 Sep 2007, 17:41
well..
what has gone wrong?
I'm not experienced with manual pe's

Code:


         include 'include/win32a.inc'
     image_base equ 0x400000
     alignment equ 0x4
     stack_reserve equ 0x1000
     stack_commit equ 0x1000
     heap_reserve equ 0x1000
     heap_commit equ 0x1000

     format binary as "exe"
     use32

     dos_header:

      db 'MZ',0,0 ; DOS signature

     pe_header:
      db 'PE',0,0
      dw 0x014C
      dw 1
      dd 0
      dd 0
      dd 0
      dw sizeof.header
      dw 0x010F

     optional_header:
      dw 0x010B ; magic
      dw 0
      dd 0
      dd 0
      dd 0
      dd code_directory
      dd 0
      dd 0
      dd image_base
      dd alignment
      dd alignment
      dw 0
      dw 0
      dw 0
      dw 0
      dw 4
      dw 0
      dd 0
      dd sizeof.image
      dd code_directory
      dd 0
      dw 0x0002
      dw 0
      dd stack_reserve
      dd stack_commit
      dd heap_reserve
      dd heap_commit
      dd 0
      dd 2

     directory_entries:
      dq 0 ; export
      dd import_directory
      dd sizeof.import
      dq 0

     import_header:

      dq '.import'
      dd sizeof.import
      dd code_directory
      dd sizeof.import
      dd code_directory
      dd 0
      dd 0
      dw 0
      dw 0
      dd 0x0E0000020

      align alignment

     code_directory:

 push starting_msg
        call dword [printf + image_base]
        pop eax

        push wsa_data + image_base
        push 0x0101 + image_base
        call dword [WSAStartup + image_base]
        test eax, eax
        jnz .startup_error

        mov word [my_addr+sockaddr_in.sin_family], 2; AF_INET
        mov dword [my_addr+sockaddr_in.sin_addr], 0;INADDR_ANY
        mov ax, 25000
        xchg ah, al
        mov word [my_addr+sockaddr_in.sin_port], ax

        push 0
        push SOCK_STREAM + image_base
        push AF_INET + image_base
        call dword [socket]
        cmp eax, -1 ;INVALID_SOCKET
        je .socket_error
        mov [my_sock], eax

        push sizeof.sockaddr_in + image_base
        push my_addr + image_base
        push dword [my_sock + image_base]
        call dword [bind + image_base]
        test eax, eax
        jnz .bind_error

        push 10
        push dword [my_sock + image_base]
        call dword [listen + image_base]
        test eax, eax
        jnz .listen_error

.forever:
        mov dword [buffer], sizeof.sockaddr_in
        push buffer + image_base
        push client_addr + image_base
        push dword [my_sock + image_base]
        call dword [accept + image_base]
        mov [client_sock], eax

        push thread_id + image_base
        push 0
        push eax + image_base
        push server_thread + image_base
        push 0
        push 0
        call dword [CreateThread + image_base]

        jmp .forever

        push dword [my_sock + image_base]
        call dword [closesocket + image_base]

        call dword [WSACleanup + image_base]

        push 0
        call dword [exit + image_base]


.startup_error:
        mov eax, startup_error_msg
        jmp .error_exit
.socket_error:
        mov eax, socket_error_msg
        jmp .error_exit
.bind_error:
        mov eax, bind_error_msg
        jmp .error_exit
.listen_error:
        mov eax, listen_error_msg
        jmp .error_exit


.error_exit:
        push eax
        call dword [printf + image_base]
        call dword [exit + image_base]


server_thread:
        push dword [client_addr+sockaddr_in.sin_addr]
        call dword [inet_ntoa + image_base]
        push eax
        push ip_msg + image_base
        push buffer + image_base
        call dword [sprintf + image_base]
        add esp, 12

        push buffer + image_base
        call dword [printf + image_base]
        pop ecx

        push 0
        push eax
        push buffer + image_base
        push dword [client_sock + image_base]
        call dword [send + image_base]

        push dword [client_sock + image_base]
        call dword [closesocket + image_base]

        push 0
        call dword [ExitThread + image_base]



thread_id dd 0
my_sock dd 0
wsa_data db sizeof.WSADATA dup 0
my_addr db sizeof.sockaddr_in dup 0

client_sock dd 0
client_addr db sizeof.sockaddr_in dup 0

buffer db 512 dup 0

ip_msg db 'Hello %s!',13,10,13,10

starting_msg db 'Starting TCP server.',13,10,0
startup_error_msg db 'Unable to initialize Windows Sockets library.',13,10,0
socket_error_msg db 'Unable to get a socket.',13,10,0
bind_error_msg db 'Unable to bind Sad',13,10,0
listen_error_msg db 'Unable to listen Sad',13,10,0


     import_directory:

                                                 dd 0,0,0, kernel_name,  kernel_table
                                                 dd 0,0,0, winsock_name, winsock_table
                                                 dd 0,0,0, msvcrt_name,  msvcrt_table
                                                 dd 0,0,0,0,0
                                                 kernel_name  db 'KERNEL32.DLL',0
                                                 winsock_name db 'WS2_32.DLL', 0
                                                 msvcrt_name  db 'MSVCRT.DLL',  0
     kernel_table:
                                                 ExitProcess        dd       _ExitProcess
                                                 CreateThread       dd       _CreateThread
                                                 ExitThread         dd       _ExitThread
                                                 dd 0
     msvcrt_table:
                                                 printf dd       _printf
                                                 sprintf dd       _sprintf
                                                 exit dd       _exit
                                                 dd 0
     winsock_table:
                                                 WSAStartup      dd       _WSAStartup
                                                 socket          dd       _socket
                                                 bind            dd       _bind
                                                 listen          dd       _listen
                                                 accept          dd       _accept
                                                 send            dd       _send
                                                 inet_ntoa       dd       _inet_ntoa
                                                 closesocket     dd       _closesocket
                                                 WSACleanup      dd       _WSACleanup
                                                 dd 0

                                                 _ExitProcess   db 0,0,'ExitProcess',  0
                                                 _CreateThread  db 0,0,'CreateThread', 0
                                                 _ExitThread    db 0,0,'ExitThread',   0
                                                 _printf        db 0,0,'printf',       0
                                                 _sprintf       db 0,0,'sprintf',      0
                                                 _exit          db 0,0,'exit',         0
                                                 _socket        db 0,0,'socket',       0
                                                 _bind          db 0,0,'bind',         0
                                                 _listen        db 0,0,'listen',       0
                                                 _accept        db 0,0,'accept',       0
                                                 _send          db 0,0,'send',         0
                                                 _inet_ntoa     db 0,0,'inet_ntoa',    0
                                                 _closesocket   db 0,0,'closesocket',  0
                                                 _WSACleanup    db 0,0,'WSACleanup',   0
                                                 _WSAStartup    db 0,0,'WSAStartup',   0



     file_end:

     sizeof.import = file_end-import_directory
     sizeof.header = import_header-optional_header
     sizeof.image  = file_end

    
Post 18 Sep 2007, 17:41
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 18 Sep 2007, 17:57
After replacing "include 'include/win32a.inc'" with "include 'win32a.inc'" it compiled perfectly but the PE is invalid and hence it doesn't run. Try comparing with some of the manually created PE files posted on these forums to see if you can find out what is wrong.
Post 18 Sep 2007, 17:57
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.