Jump to content

Packet Forwarding


Recommended Posts

Hi,

Some time ago I made a script to forward TCP packets.

The idea is something like:

You ---> ForwardingServer ---> real Server ---> ForwardingServer ---> You

instead of:

You ---> real Server ---> You

Here is Version 2.0:

Changes:

- now with GUI

- UDP support

- sending the data as it receives it

Could be a bit buggy, but it works.

to Do:

- make use of the replace function (not very hard, just too lazy now)

#NoTrayIcon
#include <INet.au3>

$Gui1 = GUICreate('', 300, 200)
$g1Binary = GUICtrlCreateInput('Binary', 20, 20, 260, 20)
$g1String = GUICtrlCreateEdit('String', 20, 50, 260, 100)
$g1ToBinary = GUICtrlCreateButton('StringToBinary', 20, 160, 125, 20)
$g1ToString = GUICtrlCreateButton('BinaryToString', 150, 160, 125, 20)
GUISetState(@SW_SHOW, $Gui1)

AdlibRegister('Title1', 500)

TCPStartup()
UDPStartup()

Global $Pipe = -1, $Client = -1, $Server = -1, $PipeStarted = False
Global $PipeIPAddress = @IPAddress1, $PipeIsTCP = True, $PipeProtocol = 'TCP', $PipePort = 80
Global $ServerIPAddress = @IPAddress1, $ServerIsTcp = True, $ServerProtocol = 'TCP', $ServerPort = 80
Global $i = 0, $Title1 = 'Binary To String', $i1 = 0

$Gui2 = GUICreate('', 300, 300)
$g2PipeIPAddress = GUICtrlCreateInput($PipeIPAddress, 20, 20, 120, 20)
GUICtrlSetTip($g2PipeIPAddress, 'The IP Address of the Pipe.', 'Help', 1, 1)
$g2PipeProtocol = GUICtrlCreateCombo('TCP', 150, 20, 60, 20, 0x0003)
GUICtrlSetData($g2PipeProtocol, 'UDP')
GUICtrlSetTip($g2PipeProtocol, 'The Protocol of the Pipe.', 'Help', 1, 1)
$g2PipePort = GUICtrlCreateInput($PipePort, 220, 20, 60, 20, 0x2000)
GUICtrlSetTip($g2PipePort, 'The Port of the Pipe.', 'Help', 1, 1)
$g2ServerIPAddress = GUICtrlCreateInput($ServerIPAddress, 20, 50, 120, 20)
GUICtrlSetTip($g2ServerIPAddress, 'The IP Address of the Server.', 'Help', 1, 1)
$g2ServerProtocol = GUICtrlCreateCombo('TCP', 150, 50, 60, 20, 0x0003)
GUICtrlSetData($g2ServerProtocol, 'UDP')
GUICtrlSetTip($g2ServerProtocol, 'The Protocol of the Server.', 'Help', 1, 1)
$g2ServerPort = GUICtrlCreateInput($ServerPort, 220, 50, 60, 20, 0x2000)
GUICtrlSetTip($g2ServerPort, 'The Port of the Server.', 'Help', 1, 1)
$g2ReplaceRules = GUICtrlCreateEdit('', 20, 80, 260, 60, 20)
GUICtrlSetTip($g2ReplaceRules, 'The Edit Control where you can define how the Packets should be modified.' & @CRLF _
    & '[The Binary Data you want to replace];[The Binary Data you want to replace it with]' & @CRLF _
    & '[The Binary Data you want to replace];[The Binary Data you want to replace it with]' & @CRLF _
    & '...', 'Help', 1, 1)
$g2StartStop = GUICtrlCreateButton('Start Pipe', 20, 150, 125, 20)
$g2Exit = GUICtrlCreateButton('Exit', 155, 150, 125, 20)
$g2Log = GUICtrlCreateEdit('', 20, 180, 260, 100)
GUICtrlSetTip($g2Log, 'The Edit Control where all the Packets are being logged.', 'Help', 1, 1)
GUISetState(@SW_SHOW, $Gui2)

AdlibRegister('Title', 500)

Do
    If $PipeStarted Then
        If $PipeIsTCP And $Client > 0 Then
            If $Server > 0 Then
                $clientip = SocketToIP($Client)
                $clientname = _TCPIpToName($clientip)
                $clientping = Ping($clientip)
                $received = TCPRecv($Client, 1024)
                If $received <> '' Then
                    _AddToLog(_Time(2) & ' ' & $clientip & ' (Client) > ' & $ServerIPAddress & ' (Server):' & @CRLF & StringToBinary($received) & @CRLF)
                    If $ServerIsTCP Then
                        TCPSend($Server, $received)
                        If @error Then
                            $Server = -1
                        Else
                            $received = TCPRecv($Server, 1024)
                        EndIf
                    Else
                        UDPSend($Server, $received)
                        If @error Then
                            $Server = -1
                        Else
                            $received = UDPRecv($Server, 1024)
                        EndIf
                    EndIf
                    TCPSend($Client, $received)
                    If @error Then
                        $Client = -1
                        TCPCloseSocket($Server)
                        $Server = -1
                    Else
                        _AddToLog(_Time(2) & ' ' & $ServerIPAddress & ' (Server) > ' & $clientip & ' (Client):' & @CRLF & StringToBinary($received) & @CRLF)
                    EndIf
                EndIf
            Else
                If $ServerIsTCP Then
                    $Server = TCPConnect($ServerIPAddress, $ServerPort)
                Else
                    $Server = UDPOpen($ServerIPAddress, $ServerPort)
                EndIf
                If @error Then
                    $Server = -1
                EndIf
            EndIf
        ElseIf $PipeIsTCP = False Then
            If $Client > 0 Then
                $received = UDPRecv($Pipe, 1024)
                If $received <> '' Then
                    _AddToLog(_Time(2) & ' ???.???.???.??? (Client) > ' & $ServerIPAddress & ' (Server):' & @CRLF & StringToBinary($received) & @CRLF)
                    If $ServerIsTCP Then
                        TCPSend($Server, $received)
                        If @error Then
                            $Client = -1
                        Else
                            $received = TCPRecv($Server, 1024)
                        EndIf
                    Else
                        UDPSend($Server, $received)
                        If @error Then
                            $Client = -1
                        Else
                            $received = UDPRecv($Server, 1024)
                        EndIf
                    EndIf
                    UDPSend($Pipe, $received)
                    _AddToLog(_Time(2) & ' ' & $ServerIPAddress & ' (Server) > ???.???.???.??? (Client):' & @CRLF & StringToBinary($received) & @CRLF)
                EndIf
            Else
                If $ServerIsTCP Then
                    $Server = TCPConnect($ServerIPAddress, $ServerPort)
                Else
                    $Server = UDPOpen($ServerIPAddress, $ServerPort)
                EndIf
                If @error Then
                    $Server = -1
                Else
                    $Client = 1
                EndIf
            EndIf
        Else
            If $PipeIsTCP Then
                $Client = TCPAccept($Pipe)
            EndIf
        EndIf
    EndIf
    $Msg = GUIGetMsg()
    If $Msg = $g1ToBinary Then
        GUICtrlSetData($g1Binary, StringToBinary(GUICtrlRead($g1String)))
    EndIf
    If $Msg = $g1ToString Then
        GUICtrlSetData($g1String, BinaryToString(GUICtrlRead($g1Binary)))
    EndIf
    If $Msg = $g2StartStop Then
        If $PipeStarted Then
            If $PipeIsTCP Then
                TCPCloseSocket($Client)
                $Client = -1
                TCPCloseSocket($Pipe)
                $Pipe = -1
            Else
                UDPCloseSocket($Pipe)
            EndIf
            If $ServerIsTCP Then
                TCPCloseSocket($Server)
                $Server = -1
            Else
                UDPCloseSocket($Server)
                $Server = -1
            EndIf
            $PipeStarted = False
            GUICtrlSetData($g2StartStop, 'Start Pipe')
        Else
            $PipeIPAddress = GUICtrlRead($g2PipeIPAddress)
            $PipeProtocol = GUICtrlRead($g2PipeProtocol)
            If $PipeProtocol = 'TCP' Then
                $PipeIsTCP = True
            ElseIf $PipeProtocol = 'UDP' Then
                $PipeIsTCP = False
            Else
                ; LOL... Easter Egg... This part should never be run. Let's use TCP.
                $PipeIsTCP = True
            EndIf
            $ServerPort = GUICtrlRead($g2ServerPort)
            $ServerIPAddress = GUICtrlRead($g2ServerIPAddress)
            $ServerProtocol = GUICtrlRead($g2ServerProtocol)
            If $ServerProtocol = 'TCP' Then
                $ServerIsTCP = True
            ElseIf $ServerProtocol = 'UDP' Then
                $ServerIsTCP = False
            Else
                ; LOL... Easter Egg... This part should never be run. Let's use TCP.
                $ServerIsTCP = True
            EndIf
            $ServerPort = GUICtrlRead($g2ServerPort)
            If $PipeIsTCP Then
                $Pipe = TCPListen($PipeIPAddress, $PipePort, 1)
            Else
                $Pipe = UDPBind($PipeIPAddress, $PipePort)
            EndIf
            If @error Then
                MsgBox(16, 'Error', 'Couldn''t start Pipe on IP Address "' & $PipeIPAddress & '" and Port ' & $PipePort & '.', 0, $Gui2)
                $PipeStarted = False
            Else
                $PipeStarted = True
                GUICtrlSetData($g2StartStop, 'Stop Pipe')
            EndIf
        EndIf
    EndIf
Until $Msg = -3 Or $Msg = $g2Exit
AdlibUnRegister('Title1')
AdlibUnRegister('Title')
If $PipeStarted Then
    If $PipeIsTCP Then
        TCPCloseSocket($Pipe)
    Else
        UDPCloseSocket($Pipe)
    EndIf
EndIf
TCPShutdown()
UDPShutdown()
Exit

Func Title1()
    $Title = $Title1
    $Len = StringLen($Title)
    $Title = StringLeft($Title, $i1)
    WinSetTitle($Gui1, '', $Title)
    $i1 += 1
    If $i1 = ($Len + 1) Then
        $i1 = 0
        If $Title1 = 'Binary To String' Then
            $Title1 = 'String To Binary'
        Else
            $Title1 = 'Binary To String'
        EndIf
    EndIf
    Return True
EndFunc

Func Title()
    $Title = 'ChristophX86 Packet Forwarding'
    $Len = StringLen($Title)
    $Title = StringLeft($Title, $i)
    WinSetTitle($Gui2, '', $Title)
    $i += 1
    If $i = ($Len + 1) Then
        $i = 0
    EndIf
    Return True
EndFunc

Func _AddToLog($Message)
    Global $g2Log
    Return GUICtrlSetData($g2Log, $Message & @CRLF, 'append')
EndFunc

; Function Name: _Time
; Function Description: simple function to get the number of seconds since the start of the unix-epoch
; Function Author: ChristophX86
Func _Time($Mode=1)
    If $Mode = 1 Then
        Return ((@YEAR - 1970) * 86400*360) + (@YDAY * 86400) + (@HOUR * 3600) + (@MIN * 60) + @SEC
    ElseIf $Mode = 2 Then
        Return @MON & '/' & @MDAY & '/' & @YEAR & ' ' & @HOUR & ':' & @MIN & ':' & @SEC
    EndIf
EndFunc

; 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 ChristophX086
Link to comment
Share on other sites

Is this a stream based Pacfket Forwarder?

As in: Is it forwarding all the packets in one go? or is it sending them as it recieves them?

I would think that this is what you want...(Stream Based Server). In case you didnt realise, it is one way.

#NoTrayIcon
#include <ChristophX86Binary.au3>

Global $thisServer = -1, $Client = -1, $remoteServer = -1
Global $receivedData = '', $receivedPacket = ''

TCPStartup()

$thisIP = '127.0.0.1'
$thisPort = 80
$remoteIP = TCPNameToIP('www.facebook.com')
$remotePort = 80

$thisServer = TCPListen($thisIP, $thisPort)
If @error Then
    ConsoleWrite('Couldn''t start Server on ' & $thisIP & ':' & $thisPort & ' !' & @CRLF)
    TCPShutdown()
    Exit
EndIf

Do
    If $Client > 0 Then
        $ClientIP = SocketToIP($Client)
        $receivedPacket = TCPRecv($Client, 1024)
;~      If $receivedPacket = '' Then
;~          ConsoleWrite('Received Nothing from Client ' & $ClientIP & ' !' & @CRLF)
;~      Else
            $receivedData = $receivedPacket
            Do
                $receivedPacket = TCPRecv($Client, 1024)
                If $receivedPacket = '' Then
                    ExitLoop
                EndIf
                $receivedData &= $receivedPacket
            Until 0=1
            $remoteServer = TCPConnect($remoteIP, $remotePort)
            If @error Then
                ConsoleWrite('Couldn''t connect to Server ' & $remoteIP & ':' & $remotePort & ' !' & @CRLF)
            Else
                TCPSend($remoteServer, $receivedData)
                If @error Then
                    ConsoleWrite('Couldn''t send Data to Server ' & $remoteIP & ':' & $remotePort & ' !' & @CRLF)
                Else
                    If $receivedData = '' Then
                        ConsoleWrite('Received Nothing from Server ' & $remoteIP & ' !' & @CRLF)
                    Else
                        ConsoleWrite(_Time(2) & '  ' & $ClientIP & ' (Client) > ' & $remoteIP & ' (Server):' & @CRLF & StringToBinary($receivedData) & @CRLF)
                    EndIf
                    $receivedPacket = TCPRecv($remoteServer, 1024)
                    $receivedData = $receivedPacket
;~                  If $receivedData <> '' Then
                        Do
                            $receivedPacket = TCPRecv($remoteServer, 1024)
                            If $receivedPacket = '' Then
                                ContinueLoop;CHANGE HERE
                            EndIf
                            $receivedData = $receivedPacket;CHANGE HERE
                    TCPSend($Client, $receivedData);WE ARE SENDING THE DATA EVERY LOOP, NOT AT THE END OF THE CONNECTION
                    If @error Then
                        ConsoleWrite('Couldn''t send Data to Client ' & $ClientIP & ' !' & @CRLF)
                    Else
                        ConsoleWrite(_Time(2) & '  ' & $remoteIP & ' (Server)  > ' & $ClientIP & ' (Client):' & @CRLF & StringToBinary($receivedData) & @CRLF)
                    EndIf

                        Until 0=1
;~                  EndIf
                EndIf
            EndIf
;~      EndIf
        TCPCloseSocket($Client)
        $Client = -1
    Else
        $Client = TCPAccept($thisServer)
    EndIf
Until 0=1
Exit

Func OnAutoItExit()
    TCPCloseSocket($thisServer)
    TCPShutdown()
EndFunc
Edited by hyperzap

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Link to comment
Share on other sites

Thanks for your help, hyperzap.

I really wanted it to send the packets as it recives them.

But can you tell me what the problem with my version was (it should be working, too)?

Just imagine you want to download a 1GB file with your proxy. Then your script stores all the information in memory and then sends the data all at once. In the meantime the connection could have timed out, so nothing would be sent or you get an out-of-memory error. Direct streaming avoids these problems since you neither have to store large amounts of data nor have long idle times.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

you script stores it all and then sends it in one go, like an ajaculation. my script sends the data as it recieves it, preventing timeout.

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

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