Skrip Posted May 7, 2009 Posted May 7, 2009 (edited) This script is designed to monitor a port for incoming information. Give 'er a shot. expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <ListBoxConstants.au3> #include <StaticConstants.au3> #include <StatusBarConstants.au3> #include <TabConstants.au3> #include <WindowsConstants.au3> #include <GuiStatusBar.au3> #include <GuiListView.au3> #include <String.au3> Global $Listening = False, $Active = False, $LSocket, $LSocketA = -1, $PNum = 0, $MaxLength = 2048, $Buffer, $SBuffer, $SIBuffer = 8, $BReturn, $IBuffer, $SettingP = @ScriptDir & "\PSettings.ini", $Server_Set = 1, $Server Global $ListData[1] = [0] Global $LSocketsA[1] = [0] $LSocketsA[0] = 0 $Form1 = GUICreate("Port Monitor", 625, 610, -1, -1) $Label1 = GUICtrlCreateLabel("Port (1 - 65535):", 390, 10, -1, 17) $Input1 = GUICtrlCreateInput("", 472, 8, 57, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_NUMBER)) GUICtrlSetLimit(-1, 5) $Button1 = GUICtrlCreateButton("Listen", 536, 6, 81, 25, 0) ;Tabs $Tab1 = GUICtrlCreateTab(8, 146, 609, 409, -1, BitOR($TCS_EX_FLATSEPARATORS, $WS_EX_STATICEDGE)) $TabSheet1 = GUICtrlCreateTabItem("RAW") $Edit1 = GUICtrlCreateEdit("", 13, 175, 593, 369, $ES_READONLY) $TabSheet2 = GUICtrlCreateTabItem("Hex") $Edit2 = GUICtrlCreateEdit("", 13, 175, 593, 369, $ES_READONLY) ;~ $TabSheet3 = GUICtrlCreateTabItem("Asc") ;~ $Edit3 = GUICtrlCreateEdit("", 13, 175, 593, 369, $ES_READONLY) $TabSheet4 = GUICtrlCreateTabItem("Binary") $Edit4 = GUICtrlCreateEdit("", 13, 175, 593, 369, $ES_READONLY) GUICtrlCreateTabItem("") ;Menu $MMenu = GUICtrlCreateMenu("File") $MSaveA = GUICtrlCreateMenuItem("Save Raw Packets", $MMenu) GUICtrlCreateMenuItem("", $MMenu) $MExit = GUICtrlCreateMenuItem("Exit", $MMenu) ; $MSettings = GUICtrlCreateMenu("Settings") $MLClear = GUICtrlCreateMenuItem("Clear List", $MSettings) $MIP1 = GUICtrlCreateMenuItem("@IPAddress1", $MSettings, -1, 1) $MIP2 = GUICtrlCreateMenuItem("@IPAddress2", $MSettings, -1, 1) $MIP3 = GUICtrlCreateMenuItem("@IPAddress3", $MSettings, -1, 1) $MIP4 = GUICtrlCreateMenuItem("@IPAddress4", $MSettings, -1, 1) $MIP5 = GUICtrlCreateMenuItem("Custom IP", $MSettings, -1, 1) $MClear = GUICtrlCreateMenuItem("Reset Settings File", $MSettings) ;Status $StatusBar1 = _GUICtrlStatusBar_Create($Form1) _GUICtrlStatusBar_SetMinHeight($StatusBar1, 25) _GUICtrlStatusBar_SetParts($StatusBar1, 75, 325) ;Listview $List1 = GUICtrlCreateListView("", 8, 40, 609, 100, $LVS_SINGLESEL) _GUICtrlListView_InsertColumn($List1, 0, "Packet Number", 100) _GUICtrlListView_InsertColumn($List1, 1, "RAW View", 400) _GUICtrlListView_InsertColumn($List1, 2, "IP", 110) GUICtrlSetBkColor($List1, 0xFFFECC) $Label2 = GUICtrlCreateLabel("View Packet:", 8, 16, 67, 17) GUISetState(@SW_SHOW) _SetStatus("Loading settings...") If FileExists($SettingP) Then _Settings(1) Else _SetStatus("Settings not found. Creating...", 2, 0, 1000) GUICtrlSetState($MIP1, $GUI_CHECKED) $Server = @IPAddress1 $Server_Set = 1 _Settings(0) EndIf _SetStatus("Waiting...") _SetStatus("No packets found.", 0, 1) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _SetStatus("Cleaning up and shutting down.") _Settings(0) TCPShutdown() _SetStatus("Goodbye!", 0, 1) _Fade(0); Fade-Out Exit Case $MExit _SetStatus("Cleaning up and shutting down.") _Settings(0) TCPShutdown() _SetStatus("Goodbye!", 0, 1) _Fade(0); Fade-Out Exit Case $MIP1 $Server = @IPAddress1 $Server_Set = 1 Case $MIP2 $Server = @IPAddress2 $Server_Set = 2 Case $MIP3 $Server = @IPAddress3 $Server_Set = 3 Case $MIP4 $Server = @IPAddress4 $Server_Set = 4 Case $MIP5 $Server = InputBox("Please enter IP to host server on.", "Please enter IP to host server on.", "192.168.1.", "", 500, 130) $Server_Set = $Server Case $MClear FileDelete($SettingP) _SetStatus("Settings cleared...", 0, 0, 1500) Case $MLClear _GUICtrlListView_DeleteAllItems($List1) $PNum = 0 Case $MSaveA _SetStatus("Beginning save process...", 0, 0, 400) $File = FileSaveDialog("Save packet to end of file", @MyDocumentsDir, "Packet File (*.pmp)") $File = FileOpen($File, 1) _SetStatus("Saving Packets...", 0, 0, 200) For $j = 1 To UBound($ListData) - 2 ;~ Sleep(500) FileWrite($File, $ListData[$j] & @CRLF & "==============================" & @CRLF) _SetStatus("Saving Packets..." & $j & " of " & UBound($ListData) - 2) Next FileClose($File) _SetStatus("Completed...") Case $Button1 _TListen(); Toggle Listening EndSwitch If $Listening Then _FConnection() If $Listening And $Active Then _Monitor();Listen for packets _Buffer(1) WEnd Func _Buffer($IO) If $IO Then If _GUICtrlListView_GetSelectedIndices($List1) <> $Buffer Then $Buffer = _GUICtrlListView_GetSelectedIndices($List1) $IBuffer = $ListData[$Buffer + 1] GUICtrlSetData($Edit1, $IBuffer) GUICtrlSetData($Edit2, _StringToHex($IBuffer)) ;~ GUICtrlSetData($Edit3, Asc($IBuffer)) GUICtrlSetData($Edit4, StringToBinary($IBuffer)) EndIf ;~ EndIf EndFunc ;==>_Buffer Func _TListen() Global $Port = GUICtrlRead($Input1) If $Port = "" Or $Port > 65535 Then _SetStatus("Enter a port number betweeen 1 and 65535", 2, 0, 1500) Else $Listening = Not $Listening If $Listening Then GUICtrlSetData($Button1, "Stop"); Now Listening ControlDisable($Form1, "", $Input1) _SetStatus("Initiating Server on port " & $Port & "...") Sleep(200) _InitServ($Port) Else TCPShutdown() $Active = False GUICtrlSetData($Button1, "Listen"); Not Listening _SetStatus("Waiting...") ControlEnable($Form1, "", $Input1) EndIf EndIf EndFunc ;==>_TListen Func _InitServ($Port) TCPStartup() $LSocket = TCPListen($Server, $Port) If $LSocket = -1 Then $Active = False _SetStatus("Unable to intialize socket - Error: " & @error, 2) Else ;~ $Active = True _SetStatus("Waiting for connection..", 1) EndIf EndFunc ;==>_InitServ Func _FConnection() $LSocketA = TCPAccept($LSocket) If $LSocketA <> -1 Then _SetStatus("Connection Found. Total: " & $LSocketsA[0] + 1, 1, 0, 750) $Active = True; Someone connected ReDim $LSocketsA[UBound($LSocketsA) + 2] $LSocketsA[0] += 1 $LSocketsA[$LSocketsA[0]] = $LSocketA Sleep(300) _SetStatus("Listening...", 1) EndIf EndFunc ;==>_FConnection Func _Monitor() For $i = 1 To $LSocketsA[0] $Data = TCPRecv($LSocketsA[$i], $MaxLength) If $Data <> "" Then $PNum = $PNum + 1 ReDim $ListData[UBound($ListData) + 2] $ListData[$PNum] = $Data GUICtrlCreateListViewItem($PNum & "|" & $ListData[$PNum] & "|" & _Socket2IP($LSocketsA[$i]), $List1) $ListData[$PNum] = "From: " & _Socket2IP($LSocketsA[$i]) & @CRLF & $Data _SetStatus("Found " & $PNum & " Packets.", 0, 1) EndIf Next EndFunc ;==>_Monitor Func _Socket2IP($hSocket) Local $pSocketAddress, $aReturn $pSocketAddress = DllStructCreate("short;ushort;uint;char[8]") $aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $hSocket, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress)) If @error Or $aReturn[0] <> 0 Then Return 0 $aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3)) If @error Then Return 0 $pSocketAddress = 0 Return $aReturn[0] EndFunc ;==>_Socket2IP Func _Settings($IO) If $IO Then; Load settings GUICtrlSetData($Input1, IniRead($SettingP, "Settings", "Port", "")) $Server_Set = IniRead($SettingP, "Settings", "IP", "1") If $Server_Set = 1 Then $Server = @IPAddress1 GUICtrlSetState($MIP1, $GUI_CHECKED) ElseIf $Server_Set = 2 Then $Server = @IPAddress2 GUICtrlSetState($MIP2, $GUI_CHECKED) ElseIf $Server_Set = 3 Then $Server = @IPAddress3 GUICtrlSetState($MIP3, $GUI_CHECKED) ElseIf $Server_Set = 4 Then $Server = @IPAddress4 GUICtrlSetState($MIP4, $GUI_CHECKED) Else GUICtrlSetState($MIP5, $GUI_CHECKED) EndIf Else; Save Settings IniWrite($SettingP, "Settings", "Port", GUICtrlRead($Input1)) IniWrite($SettingP, "Settings", "IP", $Server_Set) EndIf EndFunc ;==>_Settings Func _SetStatus($SText, $SIcon = 0, $SPart = 0, $SReturn = 0) If $SIcon = 0 Then; Default $SIcon = 8 ElseIf $SIcon = 1 Then; Listening $SIcon = 164 ElseIf $SIcon = 2 Then; Error $SIcon = 131 $SText = "ERROR: " & $SText Else; Default $SIcon = 8 EndIf If $SReturn = 0 And $SPart = 0 Then $SBuffer = $SText $SIBuffer = $SIcon EndIf If $SReturn > 0 Then _GUICtrlStatusBar_SetText($StatusBar1, $SText, $SPart) _GUICtrlStatusBar_SetIcon($StatusBar1, $SPart, _WinAPI_LoadShell32Icon($SIcon)) $Timer = TimerInit() Do $Time = TimerDiff($Timer) Sleep(50) Until $Time >= $SReturn _GUICtrlStatusBar_SetText($StatusBar1, $SBuffer, $SPart) _GUICtrlStatusBar_SetIcon($StatusBar1, $SPart, _WinAPI_LoadShell32Icon($SIBuffer)) Else _GUICtrlStatusBar_SetText($StatusBar1, $SText, $SPart) _GUICtrlStatusBar_SetIcon($StatusBar1, $SPart, _WinAPI_LoadShell32Icon($SIcon)) EndIf EndFunc ;==>_SetStatus Func _Fade($IO) If $IO Then Local $Trans = 0 Do $Trans = $Trans + 5 WinSetTrans($Form1, "", $Trans) Sleep(10) Until $Trans = 255 Else Local $Trans = 255 Do $Trans = $Trans - 5 WinSetTrans($Form1, "", $Trans) Sleep(10) Until $Trans = 0 EndIf EndFunc ;==>_Fade Edited May 9, 2009 by Firestorm [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
FinalVersion Posted May 7, 2009 Posted May 7, 2009 (edited) If this is half as good as I think it is good job. Edited May 7, 2009 by Godly [center][+] Steam GUI [+][+] Clipboard Tool [+][+] System :: Uptime [+][+] StarCraft II Mouse Trap [+][/center]
enaiman Posted May 7, 2009 Posted May 7, 2009 I tried to listen on port 80 - nothing showed up and my IE was working SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
Skrip Posted May 7, 2009 Author Posted May 7, 2009 (edited) I tried to listen on port 80 - nothing showed up and my IE was working XXX.XXX.XXX.XXX:80/Your IP:80Here's a screenshot. It's a newer version, but will return the same thing as posted version above.EDIT: Released new version.Added: -Saving Packets-Source IP-Removed Asc (useless)-Improved networking-Fixed multi connections Edited May 8, 2009 by Firestorm [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
MerkurAlex Posted May 8, 2009 Posted May 8, 2009 Looking great glad it worked out lol [quote name='PsaltyDS' post='635433' date='Jan 27 2009, 07:04 AM']Larry is a mass murderer?! It's always the quiet, clean cut, bald guys... [/quote]
enaiman Posted May 8, 2009 Posted May 8, 2009 XXX.XXX.XXX.XXX:80/ Your IP:80 I guess I'm "blind" and I can't see what you mean. Better have a look at the code you have posted and see if anything is wrong. Your input accepts only port numbers and the only refference to an IP address is in: Local $Server = @IPAddress1 The code doesn't work for me. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
TomCat Posted May 8, 2009 Posted May 8, 2009 Nice Some Question: Can you add support for UDP Packets too ?
MerkurAlex Posted May 8, 2009 Posted May 8, 2009 I guess I'm "blind" and I can't see what you mean. Better have a look at the code you have posted and see if anything is wrong. Your input accepts only port numbers and the only refference to an IP address is in: Local $Server = @IPAddress1 The code doesn't work for me.I believe he was telling you how to connect to the packet viewer with a browser... so run packet viewer on port 80 and then open firefox or IE and enter "http://192.168.1.10:80/" that is of course assuming your local ip is 192.168.1.10... note: i have found its best if you use the 192.168 ip and not 127.0.0.1 as it will not connect(for me and firestorm at least) [quote name='PsaltyDS' post='635433' date='Jan 27 2009, 07:04 AM']Larry is a mass murderer?! It's always the quiet, clean cut, bald guys... [/quote]
Skrip Posted May 8, 2009 Author Posted May 8, 2009 Also, that port may need to be forwarded. [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
MerkurAlex Posted May 8, 2009 Posted May 8, 2009 Also, that port may need to be forwarded.Only if your not local in other words if you want someone outside your house/network to connect it must be forwarded and they must use the outside ip(from whatismyip.com) [quote name='PsaltyDS' post='635433' date='Jan 27 2009, 07:04 AM']Larry is a mass murderer?! It's always the quiet, clean cut, bald guys... [/quote]
FranckGr Posted May 8, 2009 Posted May 8, 2009 If you have 2 NICs on your computer, your problem can also come from the NIC order (Socket used = @IPAddress1+Port). You can change it in Network connections properties / Menu=Advanced / Advanced parameters (My XP is French but I think it is the good translation). Here you can change the NICs order. You can verify with IPConfig in a DOS box.
Skrip Posted May 8, 2009 Author Posted May 8, 2009 I'm not sure what you mean. I only have one NIC, so I can't really test. Should I add an option to select @IPAddress1-4? Is that what you meant? [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
FranckGr Posted May 9, 2009 Posted May 9, 2009 I'm not sure what you mean. I only have one NIC, so I can't really test. Should I add an option to select @IPAddress1-4? Is that what you meant?I have one NIC (hardware) and a second (Virtual NIC from Sun VirtualBox) and only the first one displayed by IPCONFIG (IPAddress1:PortNb) is seen by PortMonitor. I'm using Telnet IPAddress1 PortNb and Telnet IPAddress2 PortNb to test. My XP (for security) is not routing at all between NICs, so the TCP Stack is not routing IPAddress2:PortNB to IPAddress1:PortNb.So yes, adding an option with @IPAddress1-4 would be nice, with a default set to @IPAddress1 (And may be a _GUICtrlListView_DeleteAllItems($List1) )Nice program anyway ...
Skrip Posted May 9, 2009 Author Posted May 9, 2009 Released an update with some IP settings, and I added the feature to clear the packet list. [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
enaiman Posted May 10, 2009 Posted May 10, 2009 XXX.XXX.XXX.XXX:80/Your IP:80run packet viewer on port 80 and then open firefox or IE and enter "http://192.168.1.10:80/"Nice translation MerkurAlex I'm glad you understood that language because it was beyond my understanding.Thank you for making that clear SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
Skizmata Posted May 11, 2009 Posted May 11, 2009 That is amazing I have learned allot from reading your code (not just this code). Thank you firestorm for sharing. Just for fun at the office I added the following 3 lines Dim $oSp = ObjCreate("SAPI.SpVoice") $aData = StringSplit($data,@CRLF) $oSp.speak(StringReplace(StringTrimRight(StringTrimLeft($aData[1],5),9),"%20"," ")) to _Monitor() so I could make my system talk to people while locked down from the other room. Fun stuff and very informative. Thanks again! Everyone got a good laugh in the office. AutoIt changed my life.
Skrip Posted May 11, 2009 Author Posted May 11, 2009 Haha, awesome! Never would have thought to do that. Glad you like it. [left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now