tell me more: Virtual Network Computing (VNC)

Started by Arnold Archibald, May 26, 2010, 02:56:49 AM

Previous topic - Next topic

Arnold Archibald

[Please treat this thread as though it could be a worthwhile reference for future generations of VNCers]

I'd love to know about VNC, it's component functions, perhaps which APIs it might use, some basic (not BASIC) source code in assembler would also be super.
I've run a couple of different companies VNCs (UltraVNC free, Idleart's Veo Remote) and wouldn't mind trying to make one myself.

For those that are interested:
http://www.idlearts.com/

joemc

Well basically learn sockets programing. Then learn how to capture screen :) than learn SendMessage() to click on stuff.  it's really not all that hard.  Close to Trojan territory though. And most people are going to expect encryption, and no holes for people to come trash your computer.

What i would honestly like to see is being able to log onto remote machine, and being able to capture a remote window... i suppose you could just steal a picture of it, but i don't feel like looking at a whole desktop, just the application i want, in its own window. Preferable logged on in background of whats on the machine locally.  My other box is connected to my TV and mainly playing video files. currently i am making Client's for my laptop that control server processes running in background.   Would be nice if some magical VNC software handled all this for me.

edit: As a side note i believe this is where WPF is headed.  A xml document describing what the UI looks like is alot easier to send over a wire than a bunch of random CreateWindow calls :)  I am fairly torn on the subject because i like local code... It's a shame they make you pick between outdated techniques or what is basically a horrible virtual machine.  By being on a MASM forum i generally agree with outdated techniques :)

edit again: sorry i stole your thread and ran off with it... VNC is good :)

gwapo

Quote from: joemc on May 26, 2010, 04:50:52 AM
As a side note i believe this is where WPF is headed.  A xml document describing what the UI looks like is alot easier to send over a wire than a bunch of random CreateWindow calls :)  I am fairly torn on the subject because i like local code...

One of the needed parameter of CreateWindow is the class name. I'm not sure whether it's easy to transfer WNDCLASSEX (for RegisterClassEx) to local machine if class in not yet locally registered.

Edit: Sorry, I've misread your post. You were referring to WPF, not CreateWindow  :bg

Cheers,

-chris

joemc

i started writing a basic control for computer hookedup to my tv. for now the client is telnet.
it does not have a buffer or any "packet" handling set up. but should give you a basic idea of winsock.  It is really drawn out to be obvious.  Most of the init code could all be in one function.
It also has an invisible window due to the fact i wanted a console mode application and still wanted to use WSAAsyncSelect.  Hope you appreciate my "clever" MessageLoop. lol. although it really doesnt need a TranslateMessage in this application, i just copy and pasted it from my other application.

edit : oh yeah and "s" being sent to server would require you have a file named "matrix.scr" in same folder. The version i use is available from Catch22.net , which is an awesome reference for win32.
edit again: as a funny side note i am using RealVNC to upload the server.exe and run / terminate it.

include \masm32\include\masm32rt.inc
include \masm32\include\ws2_32.inc
includelib \masm32\lib\ws2_32.lib

WM_SOCKET equ WM_USER+100

.data
hInstance HINSTANCE 0
hWindow   HWND      0
sock      dd        0
szComplete db "COMPLETE",13,10,0
szFail     db "FAIL",13,10,0
szWelcome  db "You are connected to the TV.",13,10  ; // 27

.code
start:
  xor eax,eax
  invoke GetModuleHandle,eax
  mov hInstance,eax
  call main
  call ExitProcess

  main proc
    cls
   
    call ReadConfig
    test eax,eax
    jnz MAIN_EXIT
   
    call StartWinsock
    test eax,eax
    jnz MAIN_EXIT
   
    call StartSocket
    test eax,eax
    jnz MAIN_WINSOCK_EXIT

    call StartWindow
    test eax,eax
    jnz MAIN_WINSOCK_EXIT
   
    call StartAsync
    test eax,eax
    jnz MAIN_WINSOCK_EXIT

    call StartBind
    test eax,eax
    jnz MAIN_WINSOCK_EXIT

    call StartListen
    test eax,eax
    jnz MAIN_WINSOCK_EXIT
   
    call MessageLoop
   
    MAIN_WINSOCK_EXIT:
      call StopWinsock
    MAIN_EXIT:
      inkey
      ret
  main endp

  MessageLoop proc uses ebx esi
    LOCAL msg:MSG
   
    lea ebx,msg
    xor esi,esi
    @@:
      invoke GetMessage,ebx,esi,esi,esi
      test eax,eax
      jz @f
      invoke TranslateMessage, ebx
      invoke DispatchMessage , ebx
      jmp @b
    @@:
    ret
  MessageLoop endp
 
  WinsockProc proc hwnd:DWORD,msg:DWORD,wparam:DWORD,lparam:DWORD
    LOCAL buff:byte
    cmp msg, WM_SOCKET
    je @f
      invoke DefWindowProc, hwnd, msg, wparam, lparam
      ret
    @@:
    mov eax,lparam
    .if ax==FD_ACCEPT
      shr eax,16
      .if ax==NULL
        print "CONNECTED",13,10
        invoke accept,wparam,0,0
        invoke send,eax,ADDR szWelcome,sizeof szWelcome,NULL
      .else
        print "CONNECTED ERROR",13,10
      .endif
   
    .elseif ax==FD_READ
      shr eax,16
      .if ax==NULL
       
        invoke recv,wparam,addr buff,1,0
        print "READ "
        print str$(buff),13,10
        .if buff=='s'
          jmp @f
          Matrix db "matrix.scr",0
          @@:
          invoke ShellExecute,NULL,NULL,addr Matrix,NULL,NULL,SW_SHOW
        .endif
      .else
        print "READ ERROR"
      .endif
    .elseif ax==FD_CLOSE
      shr eax,16
      .if ax==NULL
        print "CLOSED",13,10
      .else
        print "CLOSED ERROR",13,10
      .endif
    .endif
    xor eax,eax
    ret
  WinsockProc endp
 
  ReadConfig proc
    print " Reading Configuartion Files   "
   
    print OFFSET szComplete
    xor eax,eax
    ret
  ReadConfig endp

  StartWinsock proc
  LOCAL ws:WSADATA
    print " Starting Winsock              "
    invoke WSAStartup, 101h,addr ws
    test eax,eax
    jnz @f
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StartWinsock endp

  StartSocket proc
    print " Creating Socket               "
    invoke socket,AF_INET,SOCK_STREAM,0
    cmp eax,INVALID_SOCKET
    je @f
      mov sock,eax
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StartSocket endp
 
  StartWindow proc
    LOCAL wc:WNDCLASSEX
    STRING class_name,"WinsockWindow"
   
    print " Creating Window               "
   
    invoke RtlZeroMemory, addr wc, sizeof WNDCLASSEX;
    mov wc.cbSize,sizeof WNDCLASSEX;
    lea eax, class_name
    mov wc.lpszClassName,eax;
    mov wc.lpfnWndProc, OFFSET WinsockProc;
    invoke RegisterClassEx, ADDR wc
    xor ecx,ecx
    lea eax, class_name
    invoke CreateWindowEx, ecx, eax, ecx, ecx, ecx, ecx, ecx, ecx, ecx, ecx, hInstance, ecx
    test eax,eax
    jz @f
      mov hWindow,eax
      invoke ShowWindow,eax, SW_HIDE
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StartWindow endp
 
  StopWinsock proc
    print " Stopping Winsock              "
    invoke WSACleanup
    cmp eax,SOCKET_ERROR
    je @F
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StopWinsock endp
 
  StartAsync proc
    print " Starting Async                "
    invoke WSAAsyncSelect, sock, hWindow,WM_SOCKET, FD_ACCEPT+FD_READ+FD_CLOSE
    cmp eax,SOCKET_ERROR
    je @F
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StartAsync endp
 
  StartBind proc
    LOCAL sin:sockaddr_in
    print " Starting Bind                 "
    mov sin.sin_family, AF_INET
    invoke htons,8888
    mov sin.sin_port,ax
    mov sin.sin_addr.S_un,INADDR_ANY
   
    invoke bind, sock, ADDR sin, SIZEOF sockaddr_in
    test eax,eax
    jnz @f
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StartBind endp
 
  StartListen proc
  print " Starting Listen               "
  invoke listen,sock,1
  cmp eax,SOCKET_ERROR
  je @F
      print OFFSET szComplete
      xor eax,eax
      ret
    @@:
      print OFFSET szFail
      xor eax,eax
      add eax,1
      ret
  StartListen endp
 
end start