Guest User

Port Knocking APP

a guest
Jul 29th, 2010
399
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ButtonConstants.au3>
  2. #include <ComboConstants.au3>
  3. #include <EditConstants.au3>
  4. #include <GUIConstantsEx.au3>
  5. #include <GUIListBox.au3>
  6. #include <StaticConstants.au3>
  7. #include <WindowsConstants.au3>
  8. #include <file.au3>
  9. #include <Array.au3>
  10.  
  11. ;knock it like it's hot
  12. $g_IP = ""
  13. $g_port = ""
  14. $ListLocation = 1
  15.  
  16. ;check config file
  17. if not FileExists(@ScriptDir & "\config.txt") Then
  18.     ;create it with dummy info
  19.     $file = FileOpen(@ScriptDir & "\config.txt", 1)
  20.     If $file = -1 Then
  21.         MsgBox(0, "Error", "Unable to open file.")
  22.         Exit
  23.     EndIf
  24.  
  25.     FileWriteLine($file, "Description;IPAddress;UDP;Port;GregRocks")
  26.  
  27.     FileClose($file)
  28.  
  29. EndIf
  30.  
  31. ReadConfig ()
  32.  
  33. ; Start The TCP Services
  34. ;==============================================
  35. TCPStartUp()
  36. UDPStartup()
  37.  
  38. ;###########################################
  39.  
  40. #Region ### START Koda GUI section ### Form=C:\Documents and Settings\greg\Desktop\autoit\portknock\Form1.kxf
  41. $Form1_1 = GUICreate("GregSowell.com Port Knock", 441, 434, 192, 114)
  42. $List1 = GUICtrlCreateList("", 24, 16, 393, 201)
  43. $CBO1 = GUICtrlCreateCombo("None", 33, 280, 65, 25)
  44. GUICtrlSetData(-1, "TCP|UDP")
  45. $In1Port = GUICtrlCreateInput("", 121, 280, 105, 21)
  46. $In1Text = GUICtrlCreateInput("", 233, 280, 177, 21)
  47. $Label1 = GUICtrlCreateLabel("1", 9, 280, 10, 17)
  48. $BTNKnock = GUICtrlCreateButton("Knock", 96, 400, 73, 25, $WS_GROUP)
  49. $BTNAdd = GUICtrlCreateButton("Add/Update", 182, 400, 73, 25, $WS_GROUP)
  50. $BTNDelete = GUICtrlCreateButton("Delete", 272, 400, 73, 25, $WS_GROUP)
  51. $InIP = GUICtrlCreateInput("", 40, 234, 161, 21)
  52. $Label2 = GUICtrlCreateLabel("IP", 8, 234, 14, 17)
  53. $Label3 = GUICtrlCreateLabel("Type", 44, 259, 28, 17)
  54. $Label4 = GUICtrlCreateLabel("Port", 124, 259, 23, 17)
  55. $Label5 = GUICtrlCreateLabel("Text", 238, 259, 25, 17)
  56. $CBO2 = GUICtrlCreateCombo("None", 33, 309, 65, 25)
  57. GUICtrlSetData(-1, "TCP|UDP")
  58. $In2Port = GUICtrlCreateInput("", 121, 309, 105, 21)
  59. $In2Text = GUICtrlCreateInput("", 233, 309, 177, 21)
  60. $Label6 = GUICtrlCreateLabel("2", 9, 309, 10, 17)
  61. $CBO3 = GUICtrlCreateCombo("None", 33, 341, 65, 25)
  62. GUICtrlSetData(-1, "TCP|UDP")
  63. $In3Port = GUICtrlCreateInput("", 121, 341, 105, 21)
  64. $In3Text = GUICtrlCreateInput("", 233, 341, 177, 21)
  65. $Label7 = GUICtrlCreateLabel("3", 9, 341, 10, 17)
  66. $CBO4 = GUICtrlCreateCombo("None", 33, 367, 65, 25)
  67. GUICtrlSetData(-1, "TCP|UDP")
  68. $In4Port = GUICtrlCreateInput("", 121, 367, 105, 21)
  69. $In4Text = GUICtrlCreateInput("", 233, 367, 177, 21)
  70. $Label8 = GUICtrlCreateLabel("4", 9, 367, 10, 17)
  71. $InDesc = GUICtrlCreateInput("", 253, 234, 161, 21)
  72. $Label9 = GUICtrlCreateLabel("Desc", 221, 234, 29, 17)
  73. GUISetState(@SW_SHOW)
  74. #EndRegion ### END Koda GUI section ###
  75.  
  76. PopList()
  77.  
  78.  
  79. While 1
  80.     $nMsg = GUIGetMsg()
  81.     Switch $nMsg
  82.         Case $BTNAdd
  83.             ;
  84.             $foundOne = 0
  85.             for $y = 1 to $aConfig
  86.                 ;loop through until we find the correct line
  87.                 if StringLeft($aConfig[$y], StringInStr($aConfig[$y], ";") - 1) == GUICtrlRead($InDesc) Then
  88.                     ; we have our match, update
  89.                     $foundOne = $y
  90.                 EndIf
  91.             Next
  92.             if $foundOne == 0 Then
  93.                 ;we didn't find a match above, so write it to file, then reload listbox
  94.                 $tempNewEntry = GUICtrlRead($InDesc) & ";" & GUICtrlRead($InIP) & ";" & GUICtrlRead($CBO1) & ";" & GUICtrlRead($In1Port) & ";" & GUICtrlRead($In1Text)
  95.                 if GUICtrlRead($CBO2) <> "None" Then
  96.                     ;add 2
  97.                     $tempNewEntry = $tempNewEntry & ";" & GUICtrlRead($CBO2) & ";" & GUICtrlRead($In2Port) & ";" & GUICtrlRead($In2Text)
  98.                     if GUICtrlRead($CBO3) <> "None" Then
  99.                         ;add 3
  100.                         $tempNewEntry = $tempNewEntry & ";" & GUICtrlRead($CBO3) & ";" & GUICtrlRead($In3Port) & ";" & GUICtrlRead($In3Text)
  101.                         if GUICtrlRead($CBO4) <> "None" Then
  102.                             ;add 4
  103.                             $tempNewEntry = $tempNewEntry & ";" & GUICtrlRead($CBO4) & ";" & GUICtrlRead($In4Port) & ";" & GUICtrlRead($In4Text)
  104.                         EndIf
  105.                     EndIf
  106.                 EndIf
  107.                 _ArrayAdd($aConfig, $tempNewEntry)
  108.             Else
  109.                 ;does exist, and write over line $y
  110.                 $aConfig[$foundOne] = GUICtrlRead($InDesc) & ";" & GUICtrlRead($InIP) & ";" & GUICtrlRead($CBO1) & ";" & GUICtrlRead($In1Port) & ";" & GUICtrlRead($In1Text)
  111.                 if GUICtrlRead($CBO2) <> "None" Then
  112.                     ;add 2
  113.                     $aConfig[$foundOne] = $aConfig[$foundOne] & ";" & GUICtrlRead($CBO2) & ";" & GUICtrlRead($In2Port) & ";" & GUICtrlRead($In2Text)
  114.                     if GUICtrlRead($CBO3) <> "None" Then
  115.                         ;add 3
  116.                         $aConfig[$foundOne] = $aConfig[$foundOne] & ";" & GUICtrlRead($CBO3) & ";" & GUICtrlRead($In3Port) & ";" & GUICtrlRead($In3Text)
  117.                         if GUICtrlRead($CBO4) <> "None" Then
  118.                             ;add 4
  119.                             $aConfig[$foundOne] = $aConfig[$foundOne] & ";" & GUICtrlRead($CBO4) & ";" & GUICtrlRead($In4Port) & ";" & GUICtrlRead($In4Text)
  120.                         EndIf
  121.                     EndIf
  122.                 EndIf
  123.             EndIf
  124.             ;write array to file
  125.             _FileWriteFromArray(@ScriptDir & "\config.txt", $aConfig,1)
  126.             ReadConfig()
  127.             PopList()
  128.  
  129.         Case $BTNDelete
  130.             ;
  131.             for $y = 1 to $aConfig[0]
  132.                 ;loop through until we find the correct line
  133.                 if StringLeft($aConfig[$y], StringInStr($aConfig[$y], ";") - 1) == GUICtrlRead($InDesc) Then
  134.                     ; we have our match, update
  135.                     _ArrayDelete($aConfig, $y)
  136.                     _FileWriteFromArray(@ScriptDir & "\config.txt", $aConfig,1)
  137.                     ReadConfig()
  138.                     PopList()
  139.                 EndIf
  140.             Next
  141.  
  142.         Case $BTNKnock
  143.             ;
  144.             $g_IP = GUICtrlRead($InIP)
  145.             $g_port = GUICtrlRead($In1Port)
  146.             $g_text = ""
  147.             $g_text = GUICtrlRead($In1Text)
  148.             if GUICtrlRead($CBO1) == "TCP" Then
  149.                 TCPKnock()
  150.             Else
  151.                 UDPKnock()
  152.             EndIf
  153.             sleep(300)
  154.             if GUICtrlRead($CBO2) <> "None" Then
  155.                 $g_port = GUICtrlRead($In2Port)
  156.                 $g_text = ""
  157.                 $g_text = GUICtrlRead($In2Text)
  158.             EndIf
  159.             if GUICtrlRead($CBO2) == "TCP" Then
  160.                 TCPKnock()
  161.             Elseif GUICtrlRead($CBO2) == "UDP" Then
  162.                 UDPKnock()
  163.             EndIf
  164.             sleep(300)
  165.             if GUICtrlRead($CBO3) <> "None" Then
  166.                 $g_port = GUICtrlRead($In3Port)
  167.                 $g_text = ""
  168.                 $g_text = GUICtrlRead($In3Text)
  169.             EndIf
  170.             if GUICtrlRead($CBO3) == "TCP" Then
  171.                 TCPKnock()
  172.             Elseif GUICtrlRead($CBO3) == "UDP" Then
  173.                 UDPKnock()
  174.             EndIf
  175.             sleep(300)
  176.             if GUICtrlRead($CBO4) <> "None" Then
  177.                 $g_port = GUICtrlRead($In4Port)
  178.                 $g_text = ""
  179.                 $g_text = GUICtrlRead($In4Text)
  180.             EndIf
  181.             if GUICtrlRead($CBO4) == "TCP" Then
  182.                 TCPKnock()
  183.             Elseif GUICtrlRead($CBO4) == "UDP" Then
  184.                 UDPKnock()
  185.             EndIf
  186.             ToolTip("knock complete")
  187.             sleep(5000)
  188.             ToolTip("")
  189.  
  190.         case $GUI_EVENT_PRIMARYUP
  191.             ;mouse was pressed, lets check to see if they choose a new item in list
  192.             ;check which list item is highlighted
  193.             $tempList = GUICtrlRead($List1)
  194.             ;see if this is new item chosen or just a click somewhere on the prog
  195.             if $tempList <> $ListLocation and $tempList <> "" Then
  196.                 ;change, update everything
  197.                 ;set list location to the temp value
  198.                 $ListLocation = $tempList
  199.  
  200.                 ;clear the entries
  201.                 GUICtrlSetData($InDesc,"")
  202.                 GUICtrlSetData($InIP,"")
  203.                 GUICtrlSetData($CBO1,"None")
  204.                 GUICtrlSetData($In1Port,"")
  205.                 GUICtrlSetData($In1Text,"")
  206.                 GUICtrlSetData($CBO2,"None")
  207.                 GUICtrlSetData($In2Port,"")
  208.                 GUICtrlSetData($In2Text,"")
  209.                 GUICtrlSetData($CBO3,"None")
  210.                 GUICtrlSetData($In3Port,"")
  211.                 GUICtrlSetData($In3Text,"")
  212.                 GUICtrlSetData($CBO4,"None")
  213.                 GUICtrlSetData($In4Port,"")
  214.                 GUICtrlSetData($In4Text,"")
  215.                 ;set the entries
  216.                 for $y = 1 to $aConfig[0]
  217.                     ;loop through until we find the correct line
  218.                     if StringLeft($aConfig[$y], StringInStr($aConfig[$y], ";") - 1) == $ListLocation Then
  219.                         ; we have our match
  220.                         $ConfigLine = $aConfig[$y]
  221.                     EndIf
  222.                 Next
  223.                 ;fill in all the boxes
  224.                 GUICtrlSetData($InDesc, $ListLocation)
  225.                 GUICtrlSetData($InIP, StringMid($ConfigLine,StringInStr($ConfigLine,";") + 1, StringInStr($ConfigLine,";",0,2) - StringInStr($ConfigLine,";") - 1))
  226.                 GUICtrlSetData($CBO1, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,2) + 1, StringInStr($ConfigLine,";",0,3) - StringInStr($ConfigLine,";",0,2) - 1))
  227.                 GUICtrlSetData($In1Port, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,3) + 1, StringInStr($ConfigLine,";",0,4) - StringInStr($ConfigLine,";",0,3) - 1))
  228.                 GUICtrlSetData($In1Text, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,4) + 1, StringInStr($ConfigLine,";",0,5) - StringInStr($ConfigLine,";",0,4) - 1))
  229.                 $tempstring = StringReplace($ConfigLine, ";", ";")
  230.                 $tempCount = @extended
  231.                 if $tempCount > 4 Then
  232.                     ;we have a second set
  233.                     GUICtrlSetData($CBO2,  StringMid($ConfigLine,StringInStr($ConfigLine,";",0,5) + 1, StringInStr($ConfigLine,";",0,6) - StringInStr($ConfigLine,";",0,5) - 1))
  234.                     GUICtrlSetData($In2Port, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,6) + 1, StringInStr($ConfigLine,";",0,7) - StringInStr($ConfigLine,";",0,6) - 1))
  235.                     GUICtrlSetData($In2Text, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,7) + 1, StringInStr($ConfigLine,";",0,8) - StringInStr($ConfigLine,";",0,7) - 1))
  236.                 EndIf
  237.                 if $tempCount > 7 Then
  238.                     ;we have a third set
  239.                     GUICtrlSetData($CBO3, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,8) + 1, StringInStr($ConfigLine,";",0,9) - StringInStr($ConfigLine,";",0,8) - 1))
  240.                     GUICtrlSetData($In3Port, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,9) + 1, StringInStr($ConfigLine,";",0,10) - StringInStr($ConfigLine,";",0,9) - 1))
  241.                     GUICtrlSetData($In3Text, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,10) + 1, StringInStr($ConfigLine,";",0,11) - StringInStr($ConfigLine,";",0,10) - 1))
  242.                 EndIf
  243.                 if $tempCount > 10 Then
  244.                     ;we have a fourth set
  245.                     GUICtrlSetData($CBO4, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,11) + 1, StringInStr($ConfigLine,";",0,12) - StringInStr($ConfigLine,";",0,11) - 1))
  246.                     GUICtrlSetData($In4Port, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,12) + 1, StringInStr($ConfigLine,";",0,13) - StringInStr($ConfigLine,";",0,12) - 1))
  247.                     GUICtrlSetData($In4Text, StringMid($ConfigLine,StringInStr($ConfigLine,";",0,4) + 1))
  248.                 EndIf
  249.  
  250.             EndIf
  251.  
  252.         Case $GUI_EVENT_CLOSE
  253.             TCPShutdown()
  254.             UDPShutdown()
  255.             Exit
  256.  
  257.     EndSwitch
  258. WEnd
  259. ;###########################################
  260.  
  261. Func PopList ()
  262.     ;populate list box
  263.     GUICtrlSetData($List1, "")
  264.     for $x = 1 to $aConfig[0]
  265.         GUICtrlSetData($List1, StringLeft($aConfig[$x], StringInStr($aConfig[$x], ";") - 1))
  266.     Next
  267. EndFunc
  268.  
  269. Func ReadConfig ()
  270.     Global $aConfig
  271.     If Not _FileReadToArray(@ScriptDir & "\config.txt",$aConfig) Then
  272.         MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
  273.         Exit
  274.     EndIf
  275. EndFunc
  276.  
  277. ;knock functions
  278. Func TCPKnock ()
  279.     ToolTip("knocking " & $g_IP & " " & $g_port)
  280.     $socket = TCPConnect( $g_IP, $g_port )
  281.     TCPCloseSocket($socket)
  282.     ToolTip("")
  283. EndFunc
  284.  
  285. Func UDPKnock ()
  286.     ToolTip("knocking " & $g_IP & " " & $g_port)
  287.     $socket = UDPOpen($g_IP, $g_port)
  288.     if $g_text <> "" Then
  289.         $status = UDPSend($socket, $g_text)
  290.     EndIf
  291.     UDPCloseSocket($socket)
  292.     ToolTip("")
  293. EndFunc
RAW Paste Data