Jump to content

TCPSEND with SOCK PROXY


Recommended Posts

Hi, i have this code:

HotKeySet("{ESC}", "Terminate")

$ip = "127.0.0.1"

$Hex = "01 "

$Str = StringSplit($Hex," ")


$ReturnTCPStartup = TCPStartup()
$Socket = TCPConnect($IP,7666)


$Hex = ''
For $x = 1 to $Str[0]
    If $Str[$x] Then
        $Hex &= Chr((Dec($Str[$x])))
    EndIf
Next


TCPConnect($IP,7666)
TCPSend($Socket,$Hex)
sleep(3000)

The code works great, it connects to my TCP SERVER and then send the packet.

I would like to know how to connect to a sock4 proxy FIRST and then send the packet to my server but i cant figure out how.

Something like:

LOCALHOST > PROXY(SOCK4) > MY SERVER.

INSTEAD OF:

LOCALHOST > MY SERVER.

 

i saw "HttpSetProxy" but its only for http proxy, i didnt find anything usefull about this in the forum too.

i would appreciate your help, thanks!

 

Link to comment
Share on other sites

i did something like this:

TCPStartup()

$hc = TCPConnect("68.180.105.55",10200)          ; Sock proxy SV

$sReq =   Chr(0x04) _                                    ; Protocol version  4
        & Chr(0x01) _                                    ; Command Code    1 - establish a tcp/ip stream connection
        & Chr(0x4C) & Chr(0x42) _                        ; Port        7666
        & Chr(0x00) & Chr(0x00) & Chr(0x0) & Chr(0xFF) _ ; Ip Adress   Invalid - 0.0.0.255
        & "" & Chr(0x00) _                               ; User Id      Empty
        & "127.0.0.1" & Chr(0x00)                   ; Host Name   localhost

; Send Request to Proxy
ConsoleWrite("! Request: " & Hex(BinaryToString($sReq)) & @CR)
TCPSend($hc,$sReq)


While 1
    Sleep(100)
WEnd

 

but it doesnt connect to my server.

Its not working like MY IP >PROXY > MY SERVER

I need something simple like (TCPSEND EXAMPLE, AUTOITHELP)

Copy to clipboard #include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Start First clicking on "1. Server"
; Then start a second instance of the script selecting "2. Client"

Example()

Func Example()
    TCPStartup() ; Start the TCP service.

    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")

    ; Assign Local variables the loopback IP Address and the Port.
    Local $sIPAddress = "127.0.0.1" ; This IP Address only works for testing on your own computer.
    Local $iPort = 65432 ; Port used for the connection.

    #Region GUI
    Local $sTitle = "TCP Start"
    Local $hGUI = GUICreate($sTitle, 250, 70)

    Local $idBtnServer = GUICtrlCreateButton("1. Server", 65, 10, 130, 22)

    Local $idBtnClient = GUICtrlCreateButton("2. Client", 65, 40, 130, 22)

    GUISetState(@SW_SHOW, $hGUI)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idBtnServer
                WinSetTitle($sTitle, "", "TCP Server started")
                GUICtrlSetState($idBtnClient, $GUI_HIDE)
                GUICtrlSetState($idBtnServer, $GUI_DISABLE)
                If Not MyTCP_Server($sIPAddress, $iPort) Then ExitLoop
            Case $idBtnClient
                WinSetTitle($sTitle, "", "TCP Client started")
                GUICtrlSetState($idBtnServer, $GUI_HIDE)
                GUICtrlSetState($idBtnClient, $GUI_DISABLE)
                If Not MyTCP_Client($sIPAddress, $iPort) Then ExitLoop
        EndSwitch

        Sleep(10)
    WEnd

    #EndRegion GUI
EndFunc   ;==>Example

Func MyTCP_Client($sIPAddress, $iPort)
    ; Assign a Local variable the socket and connect to a listening socket with the IP Address and Port specified.
    Local $iSocket = TCPConnect($sIPAddress, $iPort)
    Local $iError = 0

    ; If an error occurred display the error code and return False.
    If @error Then
        ; The server is probably offline/port is not opened on the server.
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError)
        Return False
    EndIf

    ; Send the string "tata" to the server.
    TCPSend($iSocket, "tata")

    ; If an error occurred display the error code and return False.
    If @error Then
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not send the data, Error code: " & $iError)
        Return False
    EndIf

    ; Close the socket.
    TCPCloseSocket($iSocket)
EndFunc   ;==>MyTCP_Client

Func MyTCP_Server($sIPAddress, $iPort)
    ; Assign a Local variable the socket and bind to the IP Address and Port specified with a maximum of 100 pending connexions.
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0

    If @error Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not listen, Error code: " & $iError)
        Return False
    EndIf

    ; Assign a Local variable to be used by the Client socket.
    Local $iSocket = 0

    Do ; Wait for someone to connect (Unlimited).
        ; Accept incomming connexions if present (Socket to close when finished; one socket per client).
        $iSocket = TCPAccept($iListenSocket)

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Server:" & @CRLF & "Could not accept the incoming connection, Error code: " & $iError)
            Return False
        EndIf

        If GUIGetMsg() = $GUI_EVENT_CLOSE Then Return False
    Until $iSocket <> -1 ;if different from -1 a client is connected.

    ; Close the Listening socket to allow afterward binds.
    TCPCloseSocket($iListenSocket)

    ; Assign a Local variable the data received.
    Local $sReceived = TCPRecv($iSocket, 4) ;we're waiting for the string "tata" OR "toto" (example script TCPRecv): 4 bytes length.

    ; Notes: If you don't know how much length will be the data,
    ; use e.g: 2048 for maxlen parameter and call the function until the it returns nothing/error.

    ; Display the string received.
    MsgBox($MB_SYSTEMMODAL, "", "Server:" & @CRLF & "Received: " & $sReceived)

    ; Close the socket.
    TCPCloseSocket($iSocket)
EndFunc   ;==>MyTCP_Server

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit

But instead of :

LOCALHOST > SERVER

LOCALHOST>PROXY(SOCK4)>SERVER.

cant figure out how to do.

 

Link to comment
Share on other sites

2 hours ago, Guybrush29123 said:

i did something like this:

TCPStartup()

$hc = TCPConnect("68.180.105.55",10200)          ; Sock proxy SV

$sReq =   Chr(0x04) _                                    ; Protocol version  4
        & Chr(0x01) _                                    ; Command Code    1 - establish a tcp/ip stream connection
        & Chr(0x4C) & Chr(0x42) _                        ; Port        7666
        & Chr(0x00) & Chr(0x00) & Chr(0x0) & Chr(0xFF) _ ; Ip Adress   Invalid - 0.0.0.255
        & "" & Chr(0x00) _                               ; User Id      Empty
        & "127.0.0.1" & Chr(0x00)                   ; Host Name   localhost

; Send Request to Proxy
ConsoleWrite("! Request: " & Hex(BinaryToString($sReq)) & @CR)
TCPSend($hc,$sReq)

Did you try something like this or is this exactly what you tried?  If this is exactly what you tried, then of course it doesn't work, the Port (19522) in the request is OBVIOUSLY invalid if your server is server is listening on 7666.  If this is truly a SOCKS4 proxy that you are trying to connect through, then the "127.0.0.1" string after the UserID is appears to be erroneous and not a part of the SOCKS4 request.  This looks like a SOCK4A request with an IP address instead of a domain name.  If you are going to connect to an IP address, then why not pass it in the IP address field?  Even assuming that you had created a valid SOCKS4/A request, you didn't check for a response (TCPRecv) from the server to see if the request was granted or if the server replied with an error.

Have you ever worked with proxy servers before?  Have you ever made a good connection through this particular proxy server (68.18.105.55:10200)?  Are you sure this is a SOCKS4/A proxy and not a SOCKS5 server?

Edited by TheXman
Link to comment
Share on other sites

@TheXman

You are right, i will put the IP addres in THE IP ADDRESS line and not host name.

But i have another question, you said i have "19522" port on my request.  Where is that number declared?

& Chr(0x4C) & Chr(0x42) = 7666 

and above it says: 

"68.180.105.55",10200

 

10200 : is the port that the SOCK4 proxy listen, and 7666 its the port of my server.

im sure you are right but i cant figure out where is that port declared, sorry 

Link to comment
Share on other sites

 

14 hours ago, TheXman said:

Have you ever worked with proxy servers before?  Have you ever made a good connection through this particular proxy server (68.18.105.55:10200)?  Are you sure this is a SOCKS4/A proxy and not a SOCKS5 server?

You did not answer my questions.  It appears to me that you may only have limited, if any, experience with the TCP conversation necessary to establish a proxy server connection.  It also appears that you do not have a firm understanding of binary and hexadecimal numbering systems.  If this is true, then your questions are, and will be, beyond the scope of this forum which is AutoIt General Help & Support.  In other words, you require understanding of the TCP communication, the SOCKS4 protocol, and binary/hexadecimal numbering systems, before you can attempt to implement those technical skills in AutoIt.

One of the easiest to understand descriptions of the SOCKS protocols can be found on Wikipedia.

 

To answer your specific question, if you read the SOCKS4 protocol, you will see that part of the request packet is a 2-byte, network byte order (big-endian), port number in which you want to connect.  I'm not quite sure that you understand the logic that you copied that is building the binary SOCKS4 request.  The part that is defining the port,

Chr(0x4C) & Chr(0x42)

is concatenating 0x4c and 0x42 in order to create the 2-byte port value, 0x4C42.  0x4C42 in hex, interpreted as big-endian, is 19522 in decimal not 7666.  The following 2 lines will give you an example of what I'm saying:

ConsoleWrite("19522 in hex (2 bytes/big-endian) = " & "0x" & Hex(19522,4) & @CRLF)
ConsoleWrite("Big-endian 0x4c42 in decimal      = " & Number("0x4C42") & @CRLF)

Output

19522 in hex (2 bytes/big-endian) = 0x4C42
Big-endian 0x4c42 in decimal      = 19522

 

Link to comment
Share on other sites

@TheXman

You helped me a lot, thank you very much!

This helps me a lot to understand the SOCKS protocol:  https://en.wikipedia.org/wiki/SOCKS#SOCKS4a 

Im going to read it and try to understand it well.

About the PORT THING, you were right too, it is : (0x1D) & 0x2F)  to make 7666.

I will study the sock protocol a lot to understand it well and after that i will try to implement it in autoit.

After that if i get any problems i will post back there but not before, there is no point to implement SOCKS in autoit if i dont understand socks at all.

You gave me a lot of help, thank and have a good day

 

Link to comment
Share on other sites

You're welcome! :thumbsup:  I look forward to providing more assistance once you get a better understanding of the underlying technical subjects.

 

10 minutes ago, Guybrush29123 said:

About the PORT THING, you were right too, it is : (0x1D) & 0x2F)  to make 7666.

Not quite.  In big-endian, 7666 in decimal is 0x1DF2 in hexadecimal, not 0x1D2F.  0x1D2F would be 7471.

 

ConsoleWrite("7666 in hex (2 bytes/big-endian) = " & "0x" & Hex(7666, 4) & @CRLF)
ConsoleWrite("Big-endian 0x1DF2 in decimal      = " & Number("0x1DF2") & @CRLF)
ConsoleWrite("Big-endian 0x1D2F in decimal      = " & Number("0x1D2F") & @CRLF)

 

Edited by TheXman
Link to comment
Share on other sites

There are numerous ways to build the binary SOCKS4 request.  Here's is just one, static way to build it:

socks4_binary_request_example()

Func socks4_binary_request_example()
        Local $xPacket

        ;Build SOCKS4 Request
        $xPacket =  Binary("0x04")        ;Version (SOCKS4)
        $xPacket &= Binary("0x01")        ;Command (Connect)
        $xPacket &= Binary("0x1DF2")      ;Port    (7666)
        $xPacket &= Binary("0x7F000001")  ;IP      (127.0.0.1)
        $xPacket &= Binary("0x00")        ;UserID  ()

        ConsoleWrite("SOCKS4 $xPacket = " & $xPacket & @CRLF)
EndFunc

Output

SOCKS4 $xPacket = 0x04011DF27F00000100

It's just a guide to point you in the right direction once you gain the rest of the knowledge.

Edited by TheXman
Link to comment
Share on other sites

@TheXman I really appreciate your help so MUCH. Thanks to you i made my code fully functional:

I will let the full code bellow in case it serves to someome in a future:

I made it to send packets to the open connection too, tested it and it works.

TCPStartup()
ConsoleWrite("Connecting..." & @CR)

$Hex = "01 "
$Hex1 = "02 0C 00 41 41 41 41 41 41 41 41 42 41 41 41 06 00 61 73 64 61 73 64 00 0D 00 01 01 02 01 00 07 00 61 40 61 2E 63 6F 6D 01 "
$Str = StringSplit($Hex," ")
$Str1 = StringSplit($Hex1," ")
$Hex = ''

For $x = 1 to $Str[0]
    If $Str[$x] Then
        $Hex &= Chr((Dec($Str[$x])))
    EndIf
Next

$Hex1 = ''
For $x = 1 to $Str1[0]
    If $Str1[$x] Then
        $Hex1 &= Chr((Dec($Str1[$x])))
    EndIf
Next


$hc = TCPConnect("181.209.86.170",58682)                  ; SOCK4 PROXY SERVER

If @error Then
    ConsoleWrite("Failed!" & @CR)
    Exit
Else
    ConsoleWrite("Connected!" & @CR)
EndIf

$sReq =   Chr(0x04) _                                   ; Protocol version  4
        & Chr(0x01) _                                   ; Command Code  1 - establish a tcp/ip stream connection
        & Chr(0x1D) & Chr(0xF2) _                       ; Port      7666 (2 bytes)
        & Chr(0x00) & Chr(0x00) & Chr(0x00) & Chr(0x0) _  ; IPv4 Address, 4 bytes (in network byte order)
        & "" & Chr(0x00)                              ; User Id      Empty

; Send Request to Proxy
ConsoleWrite("Sending Request..." & @CR)
ConsoleWrite("! Request: " & Hex(BinaryToString($sReq)) & @CR)
TCPSend($hc,$sReq)

; Wait for the Reply
While 1
    $sBuff = TCPRecv($hc,2048,1)
    If @error Then Exit @ScriptLineNumber
    If StringLen($sBuff) > 0 Then ExitLoop
    Sleep(100)
WEnd

$sBuff &= TCPRecv($hc,8,1)
ConsoleWrite("!   Reply: " & Hex(BinaryToString($sBuff)) & @CR)

; Check for errors
Switch StringMid(Hex(BinaryToString($sBuff)),3,2)
    Case "5A"
        ConsoleWrite("> request granted" & @CR)
    Case "5B"
        ConsoleWrite("> request rejected or failed" & @CR)
        Exit @ScriptLineNumber
    Case "5C"
        ConsoleWrite("> request failed because client is not running identd (or not reachable from the server)" & @CR)
        Exit @ScriptLineNumber
    Case "5D"
        ConsoleWrite("> request failed because client's identd could not confirm the user id string in the request" & @CR)
        Exit @ScriptLineNumber
EndSwitch

; Send raw packet throught the open connection
TCPSend($hc,$Hex)
TCPSend($hc,$Hex1)

; Wait for the Reply
ConsoleWrite("Receiving Data ")
$sRepy = ""

While 1
    $sBuff = TCPRecv($hc,1024*5)
    If @error Then ExitLoop
    If StringLen($sBuff) > 0 Then
        $sRepy &= $sBuff
        ConsoleWrite(".")
    EndIf
    Sleep(100)
WEnd

; Parse Reply
$iHeadEnd = StringInStr($sRepy,@CRLF & @CRLF) + 2
$sRepyHead = StringMid($sRepy,1,$iHeadEnd)
$sRepyBody = StringMid($sRepy,$iHeadEnd)

ConsoleWrite(@CR & "! Reply:" & @CR & $sRepyHead & "---------------------------------" & @CR)

Exit

I got a question, does AUTOIT manages well multi tcp client, to open various sockets? or it will be a little slow?

 

Edited by Guybrush29123
Link to comment
Share on other sites

31 minutes ago, Guybrush29123 said:

I got a question, does AUTOIT manages well multi tcp client, to open various sockets? or it will be a little slow?

"Slow" is a relative term.  How well AutoIt handles it will depend on things like how efficient your code is, how many connections have to be handled, and the amount of traffic.  The only way to know is to test it and see.

FYI, If I were you I would NOT post the public IP address of my SOCKS server to the general public.

 

Edited by TheXman
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...