Sign in to follow this  
Followers 0
pbw

problem using TCPSend in "raw" mode with OpenVPN

3 posts in this topic

#1 ·  Posted (edited)

Hi,

This is my first post but I have spent a lot of time here learning Autoit. What an awesome resource. Thanks!

What I am trying to do is access the management interface for OpenVPN using TCPSend in "raw" mode. 

Using the simple client from the helpfile I can TCPConnect() to the management console and hold a socket but I cant get any commands to make it to the management console using TCPSend(). I can send commands to the helpfile server with the helpfile. I have accessed the console using putty in raw mode succesfully. And I have used putty to succesfully send tothe Autoit server.

So I can putty to the autoit server, and I can autoit client to the autoit server. And I can putty to the OpenVPN management console but I havent been able to TCPSend() to the OpenVPN management console.

TCPSend always returns a success value identical to the number of characters in the message. I have tried TCPSend using all 4 modes of StringToBinary and as a string.

I suspect this has to do with a character break or line feed that needs to be at the end of the message but I am pulling my hair out on this one.

Here is the OpenVPN:

http://openvpn.net/index.php/open-source/documentation/miscellaneous/79-management-interface.html

The helpfile client script:

; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
    ;   Local $szServerPC = @ComputerName
    ;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = "127.0.0.1"
    Local $nPORT = 7505

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        ;Loop forever asking for data to send to the SERVER
        While 1
            ; InputBox for data to transmit
            $szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")

            ; If they cancel the InputBox or leave it blank we exit our forever loop
            If @error Or $szData = "" Then ExitLoop

            ; We should have data in $szData... lets attempt to send it through our connected socket.
            ; convert AutoIt native UTF-16 to UTF-8
            $b = TCPSend($ConnectedSocket, StringToBinary($szData, 4))
ConsoleWrite($b & @CRLF)
            ; If the send failed with @error then the socket has disconnected
            ;----------------------------------------------------------------
            If @error Then ExitLoop
        WEnd
    EndIf
EndFunc   ;==>Example

 

And the helpfile server script:
 
; Create a GUI for messages
    ;==============================================
    GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200, 100, 100)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)
    GUISetState()


    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    ; GUI Message Loop
    ;==============================================
    While 1
        $msg = GUIGetMsg()

        ; GUI Closed
        ;--------------------
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop

        ; Try to receive (up to) 2048 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048,1)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then ExitLoop

        ; convert from UTF-8 to AutoIt native UTF-16
        $recv = BinaryToString($recv, 4)

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then GUICtrlSetData($edit, _
                $szIP_Accepted & " > " & $recv & @CRLF & GUICtrlRead($edit))
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP
Edited by pbw

Share this post


Link to post
Share on other sites



Hi,

Upon further investigation it appears that the putty client does append a carriage return or line feed. I realized this after saving the commands to binary file. The putty commands have an extra line that is not present with the tcpsend commands.

So, Does anyone know what characters I need to append to the tcpsend  commands to make them fly?

ty

Share this post


Link to post
Share on other sites

Hi,

I solved this by examining the hex code of the messages.

Apending "0D 0A" (binary carraiage return) to the messages after completing stringtobinary causes the commands to be recognized in telnet "raw mode".

So this

$b = TCPSend($ConnectedSocket, StringToBinary($szData, 4))

becomes this:

$b = TCPSend($ConnectedSocket, StringToBinary($szData, 4) & "0D0A")
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0