AHKWall - Windows Firewall Mangement

Post your working scripts, libraries and tools
Xatmo97
Posts: 15
Joined: 27 May 2014, 12:50

AHKWall - Windows Firewall Mangement

22 Mar 2018, 15:44

AHKWall is a program were you can whitelist and blacklist remote ip addresses via the windows firewall.

Image
Image Image

Features:
1. Detects remote ip connections
2. Asks for whitelist or No for blocking in windows firewall
3. Log only mode if you dont want to be bugged by the message box and do no action
4. Rules menu item displays a list of all rules that were blacklist or autoblacklisted as well as ip addresses,ports or programs
that were allowed or blocked via the Allow or Deny Menu items. double clicking rule gives ability to delete said rule.
5. Allow Menu item gives you 3 options to allow. you can Allow a program by its window, Allow by an ip or multiple ips
Or browse to allow a program.
6. Deny Menu item also gives 3 option same as above but to deny said things.
7. Ports Menu item gives you the option to allow or block up to 14 ports or port ranges.

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%
Menu, MainTheMenu, Add, &Rules, Rules
Menu, MainAllowMenu, Add, Allow by window,  WindowAllowFW
Menu, MainAllowMenu, Add, Allow by IP,  IPAllowFW
Menu, MainAllowMenu, Add, Browse,  BrowseAllowFW
Menu, MainDenyMenu, Add, Deny by Window, WindowDenyFW
Menu, MainDenyMenu, Add, Deny by IP, IPDenyFW
Menu, MainDenyMenu, Add, Browse, BrowseDenyFW
Menu, MainTheMenu, Add, &Ports, Ports
Menu, MainTheMenu, Add, &Allow, :MainAllowMenu
Menu, MainTheMenu, Add, &Deny, :MainDenyMenu
Menu, MainOptionMenu, Add, Yez, MsgBox
Menu, MainTheMenu, Add, &Log Only, :MainOptionMenu
Gui, Menu, MainTheMenu

Gui Add, Edit, +ReadOnly x16 y22 w210 h290 vTheLog
Gui Add, Button, gDeleteLog x16 y315 w210 h30, Clear Log
Gui Show, w247 h350,AHK Wall v2
Goto, Check
Return

GuiEscape:
GuiClose:
    ExitApp

; ===============================================================================================================================
Check:
UpDateLog()
Num := RuleIndex()
IfNotExist, %A_WorkingDir%\WallLists.ini
{
 for i, v in GetTcpTable()
 Append := Append v.RemoteIP ","
 IniWrite, %Append%, %A_WorkingDir%\WallLists.ini, Whitelist, IP
 goto, tcp
}
 else
 {
  goto, tcp
 }
return

tcp:
loop
{
 Sleep, 125
 ip :=
 for i, v in GetTcpTable()
 ip := ip v.RemoteIP ","
 IniRead, File1, %A_WorkingDir%\WallLists.ini, Whitelist, IP
 IniRead, File2, %A_WorkingDir%\WallLists.ini, Blacklist, IP
 File3 := File1 . File2
 Sleep, 125

 Loop, parse, ip, `, ,%A_Space%%A_Tab%
 {
  ;Check := CheckIP(Mon)
  Mon = %A_LoopField%
   If A_LoopField =
   {
    continue
   }
     if Mon not in %File3%
     {
        if Mon in %TempIpVar%
        {
         continue
        }
        FormatTime, Date,, MM/dd hh:mm tt
        If NoMsg = 1
         {
            FileAppend,IP: %Mon%`nDetected on %Date%`nAction: Timedout-Log Only`n`n, %A_WorkingDir%\AHKWallLog.txt
            TempIpVar := TempIpVar . Mon . ","
            UpDateLog()
            continue
         }
          MsgBox,262148,Notice, IP: %Mon% Detected on %Date%. Add to Whitelist?, 5
          IfMsgBox Timeout
          {
             Sleep, 500
             FormatTime, Date,, MM/dd hh:mm tt
             IniRead, OldWallList, %A_WorkingDir%\WallLists.ini, Blacklist, IP
             NewWallList := OldWallList . Mon . ","
             IniWrite, %NewWallList%, %A_WorkingDir%\WallLists.ini, Blacklist, IP
             FileAppend,IP: %Mon%`nDetected on %Date%`nAction: Timedout-AutoBlackListed`n`n, %A_WorkingDir%\AHKWallLog.txt
             Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=in action=block enable=yes remoteip=%Mon% profile=any",, HIDE
             Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=out action=block enable=yes remoteip=%Mon% profile=any",, HIDE
             FileAppend,%Num%AHKWall: dir=in and out action=block enable=yes remoteip=%Mon% profile=any`n, %A_WorkingDir%\Rules.txt
             UpDateLog()
             Num := RuleIndex()
            
          }
          IfMsgBox No
            {
             Sleep, 500
             FormatTime, Date,, MM/dd hh:mm tt
             IniRead, OldWallList2, %A_WorkingDir%\WallLists.ini, Blacklist, IP
             NewWallList2 := OldWallList2 . Mon . ","
             IniWrite, %NewWallList2%, %A_WorkingDir%\WallLists.ini, Blacklist, IP
             FileAppend,IP: %Mon%`nDetected on %Date%`nAction: No-BlackListed`n`n, %A_WorkingDir%\AHKWallLog.txt
             Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=in action=block enable=yes remoteip=%Mon% profile=any",, HIDE
             Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=out action=block enable=yes remoteip=%Mon% profile=any",, HIDE
             FileAppend,%Num%AHKWall: dir=in and out action=block enable=yes remoteip=%Mon% profile=any`n, %A_WorkingDir%\Rules.txt
             UpDateLog()
             Num := RuleIndex()
            }
          IfMsgBox Yes
            {
             Sleep, 500
             FormatTime, Date,, MM/dd hh:mm tt
             IniRead, OldWallList3, %A_WorkingDir%\WallLists.ini, Whitelist, IP
             NewWallList3 := OldWallList3 . Mon . ","
             IniWrite, %NewWallList3%, %A_WorkingDir%\WallLists.ini, Whitelist, IP
             FileAppend,IP: %Mon%`nDetected on %Date%`nAction: Yes-WhiteListed`n`n, %A_WorkingDir%\AHKWallLog.txt
             UpDateLog()
            }
     }
}
}
return


Rules:
Menu, MainExportMenu, Add, &Export Rules, ExportRules
Gui, Rules:Menu, MainExportMenu

FileRead, TheRules, %A_WorkingDir%\Rules.txt
Gui Rules:Add, ListBox, gClicked x16 y22 w510 h400 vRules
Gui Rules:Show, w547 h420, Rules
UpdateRules()
return

ExportRules:
MsgBox, Not functional yet sorry. =(
return

Ports:
PortGuiOpen = 1
Gui Port:Add, GroupBox, x8 y0 w228 h466, Port Control
Gui Port:Add, Edit, vERad1A x16 y24 w84 h27
Gui Port:Add, Radio, gRad1A x105 y24 w50 h25, Allow
Gui Port:Add, Radio, gRad1D x155 y24 w50 h25, Deny
Gui Port:Add, Edit, vERad2A x16 y56 w84 h27
Gui Port:Add, Radio, gRad2A x105 y56 w50 h25, Allow
Gui Port:Add, Radio, gRad2D x155 y56 w50 h25, Deny
Gui Port:Add, Edit, vERad3A x16 y88 w84 h27
Gui Port:Add, Radio, gRad3A x105 y88 w50 h25, Allow
Gui Port:Add, Radio, gRad3D x155 y88 w50 h25, Deny
Gui Port:Add, Edit, vERad4A x16 y120 w84 h27
Gui Port:Add, Radio, gRad4A x105 y120 w50 h25, Allow
Gui Port:Add, Radio, gRad4D x155 y120 w50 h25, Deny
Gui Port:Add, Edit, vERad5A x16 y152 w84 h27
Gui Port:Add, Radio, gRad5A x105 y152 w50 h25, Allow
Gui Port:Add, Radio, gRad5D x155 y152 w50 h25, Deny
Gui Port:Add, Edit, vERad6A x16 y182 w84 h27
Gui Port:Add, Radio, gRad6A x105 y182 w50 h25, Allow
Gui Port:Add, Radio, gRad6D x155 y182 w50 h25, Deny
Gui Port:Add, Edit, vERad7A x16 y212 w84 h27
Gui Port:Add, Radio, gRad7A x105 y212 w50 h25, Allow
Gui Port:Add, Radio, gRad7D x155 y212 w50 h25, Deny
Gui Port:Add, Edit, vERad8A x16 y242 w84 h27
Gui Port:Add, Radio, gRad8A x105 y242 w50 h25, Allow
Gui Port:Add, Radio, gRad8D x155 y242 w50 h25, Deny
Gui Port:Add, Edit, vERad9A x16 y272 w84 h27
Gui Port:Add, Radio, gRad9A x105 y272 w50 h25, Allow
Gui Port:Add, Radio, gRad9D x155 y272 w50 h25, Deny
Gui Port:Add, Edit, vERad10A x16 y302 w84 h27
Gui Port:Add, Radio, gRad10A x105 y302 w50 h25, Allow
Gui Port:Add, Radio, gRad10D x155 y302 w50 h25, Deny
Gui Port:Add, Edit, vERad11A x16 y332 w84 h27
Gui Port:Add, Radio, gRad11A x105 y332 w50 h25, Allow
Gui Port:Add, Radio, gRad11D x155 y332 w50 h25, Deny
Gui Port:Add, Edit, vERad12A x16 y362 w84 h27
Gui Port:Add, Radio, gRad12A x105 y362 w50 h25, Allow
Gui Port:Add, Radio, gRad12D x155 y362 w50 h25, Deny
Gui Port:Add, Edit, vERad13A x16 y392 w84 h27
Gui Port:Add, Radio, gRad13A x105 y392 w50 h25, Allow
Gui Port:Add, Radio, gRad13D x155 y392 w50 h25, Deny
Gui Port:Add, Edit, vERad14A x16 y422 w84 h27
Gui Port:Add, Radio, gRad14A x105 y422 w50 h25, Allow
Gui Port:Add, Radio, gRad14D x155 y422 w50 h25, Deny
Gui Port:Show, w252 h480, Ports
GetSavedPort()
return

PortGuiClose:
Loop, 14
{
 GuiControlGet, SavePort, Port:, ERad%A_Index%A
 IniWrite, %SavePort%, %A_WorkingDir%\WallLists.ini, SavePort%A_Index%, Port 
}
Gui, Port:Destroy
return

Rad1A:
GuiControlGet, GRad1A, Port:, ERad1A
A := "Allow"
SavePorts := "SavePort1"
Port := GRad1A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad1D:
GuiControlGet, GRad1D, Port:, ERad1A
A := "Deny"
SavePorts := "SavePort1"
Port := GRad1D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad2A:
GuiControlGet, GRad2A, Port:, ERad2A
A := "Allow"
SavePorts := "SavePort2"
Port := GRad2A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad2D:
GuiControlGet, GRad2D, Port:, ERad2A
A := "Deny"
SavePorts := "SavePort2"
Port := GRad2D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad3A:
GuiControlGet, GRad3A, Port:, ERad3A
A := "Allow"
SavePorts := "SavePort3"
Port := GRad3A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad3D:
GuiControlGet, GRad3D, Port:, ERad3A
A := "Deny"
SavePorts := "SavePort3"
Port := GRad3D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad4A:
GuiControlGet, GRad4A, Port:, ERad4A
A := "Allow"
SavePorts := "SavePort4"
Port := GRad4A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad4D:
GuiControlGet, GRad4D, Port:, ERad4A
A := "Deny"
SavePorts := "SavePort4"
Port := GRad4D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad5A:
GuiControlGet, GRad5A, Port:, ERad5A
A := "Allow"
SavePorts := "SavePort5"
Port := GRad5A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad5D:
GuiControlGet, GRad5D, Port:, ERad5A
A := "Deny"
SavePorts := "SavePort5"
Port := GRad5D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad6A:
GuiControlGet, GRad6A, Port:, ERad6A
A := "Allow"
SavePorts := "SavePort6"
Port := GRad6A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad6D:
GuiControlGet, GRad6D, Port:, ERad6A
A := "Deny"
SavePorts := "SavePort6"
Port := GRad6D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad7A:
GuiControlGet, GRad7A, Port:, ERad7A
A := "Allow"
SavePorts := "SavePort7"
Port := GRad7A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad7D:
GuiControlGet, GRad7D, Port:, ERad7A
A := "Deny"
SavePorts := "SavePort7"
Port := GRad7D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad8A:
GuiControlGet, GRad8A, Port:, ERad8A
A := "Allow"
SavePorts := "SavePort8"
Port := GRad8A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad8D:
GuiControlGet, GRad8D, Port:, ERad8A
A := "Deny"
SavePorts := "SavePort8"
Port := GRad8D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad9A:
GuiControlGet, GRad9A, Port:, ERad9A
A := "Allow"
SavePorts := "SavePort9"
Port := GRad9A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad9D:
GuiControlGet, GRad9D, Port:, ERad9A
A := "Deny"
SavePorts := "SavePort9"
Port := GRad9D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad10A:
GuiControlGet, GRad10A, Port:, ERad10A
A := "Allow"
SavePorts := "SavePort10"
Port := GRad10A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad10D:
GuiControlGet, GRad10D, Port:, ERad10A
A := "Deny"
SavePorts := "SavePort10"
Port := GRad10D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad11A:
GuiControlGet, GRad11A, Port:, ERad11A
A := "Allow"
SavePorts := "SavePort11"
Port := GRad11A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad11D:
GuiControlGet, GRad11D, Port:, ERad11A
A := "Deny"
SavePorts := "SavePort11"
Port := GRad11D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad12A:
GuiControlGet, GRad12A, Port:, ERad12A
A := "Allow"
SavePorts := "SavePort12"
Port := GRad12A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad12D:
GuiControlGet, GRad12D, Port:, ERad12A
A := "Deny"
SavePorts := "SavePort12"
Port := GRad12D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad13A:
GuiControlGet, GRad13A, Port:, ERad13A
A := "Allow"
SavePorts := "SavePort13"
Port := GRad13A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad13D:
GuiControlGet, GRad13D, Port:, ERad13A
A := "Deny"
SavePorts := "SavePort13"
Port := GRad13D
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad14A:
GuiControlGet, GRad14A, Port:, ERad14A
A := "Allow"
SavePorts := "SavePort14"
Port := GRad14A
PortHandler(Port)
SaveButton(SavePorts, A)
return

Rad14D:
GuiControlGet, GRad14D, Port:, ERad14A
A := "Deny"
SavePorts := "SavePort14"
Port := GRad14D
PortHandler(Port)
SaveButton(SavePorts, A)
return

PortHandler(Port)
{
 if !(Port)
 {
  return
 }
  else
  {
    if A_GuiControl = Allow
    {
      if !(PortGuiOpen = 1)
      {
       Run %comspec% /c "netsh advfirewall firewall delete rule name="AHKWallBlock%Port%" dir=in",, HIDE
       Run %comspec% /c "netsh advfirewall firewall delete rule name="AHKWallBlock%Port%" dir=out",, HIDE
       Sleep,100
       Run %comspec% /c "netsh advfirewall firewall add rule name="AHKWallOpen%Port%" dir=in action=allow protocol=TCP localport=%Port%",, HIDE
       Run %comspec% /c "netsh advfirewall firewall add rule name="AHKWallOpen%Port%" dir=out action=allow protocol=TCP localport=%Port%",, HIDE
       FileAppend,AHKWallOpen%Port%: dir=in and out action=allow localport="%Port%" enable=yes`n, %A_WorkingDir%\Rules.txt
      }
     UpDateLog()
    }
     else
     {
       if !(PortGuiOpen = 1)
       {
        Run %comspec% /c "netsh advfirewall firewall delete rule name="AHKWallOpen%Port%" dir=in",, HIDE
        Run %comspec% /c "netsh advfirewall firewall delete rule name="AHKWallOpen%Port%" dir=out",, HIDE
        Sleep, 100
        Run %comspec% /c "netsh advfirewall firewall add rule name="AHKWallBlock%Port%" dir=in action=block protocol=TCP localport=%Port%",, HIDE
        Run %comspec% /c "netsh advfirewall firewall add rule name="AHKWallBlock%Port%" dir=out action=block protocol=TCP localport=%Port%",, HIDE
        FileAppend,AHKWallBlock%Port%: dir=in and out action=block localport="%Port%" enable=yes`n, %A_WorkingDir%\Rules.txt
       }
      UpDateLog()
     }
  }
}


GetSavedPort()
{
  Loop, 14
  {
   IniRead, GetPort, %A_WorkingDir%\WallLists.ini, SavePort%A_Index%, Port
   GuiControl, Port:, ERad%A_Index%A, %GetPort%
  }
Radiohandler()
}

Radiohandler()
{
b=2
 loop, 14
 {
  BNum := A_Index
    IniRead, RadioState, %A_WorkingDir%\WallLists.ini, SavePort%BNum%, Button%b%
     if RadioState = 1
     {
      GuiControl, Port:, Button%b%, 1
      b := b+2
     }
      else
      {
       b++
       GuiControl, Port:, Button%b%, 1
       b++
      }
  }
PortGuiOpen = 0
}

SaveButton(SavePorts, A)
{
b=2
bb=3
 loop, 14
 {
  if (SavePorts = ("SavePort" . A_Index) and A = "Allow")
  {
   IniWrite, 1, %A_WorkingDir%\WallLists.ini, %SavePorts%, Button%b%
    b := b+1
     IniWrite, 0, %A_WorkingDir%\WallLists.ini, %SavePorts%, Button%b%
   A := ""
   SavePorts := ""
   return
  }
   else if (SavePorts = ("SavePort" . A_Index) and A = "Deny")
   {
    IniWrite, 1, %A_WorkingDir%\WallLists.ini, %SavePorts%, Button%bb%
     bb := bb-1
      IniWrite, 0, %A_WorkingDir%\WallLists.ini, %SavePorts%, Button%bb%
    A := ""
    SavePorts := ""
    return
   } 
    else
    {
     b := b+2
     bb := bb+2
    }
 }
b=
bb=
}

Clicked:
If A_GuiEvent = DoubleClick
{
 GuiControlGet, Rules, Rules:
 StringGetPos, ThePos, Rules, :
 StringMid, TheName, Rules, 1, %ThePos%
 MsgBox,262148, Delete,Delete`n %Rules%?
  IfMsgBox Yes
  {
   Run %comspec% /c "netsh advfirewall firewall delete rule name="%TheName%" dir=in",, HIDE
   Run %comspec% /c "netsh advfirewall firewall delete rule name="%TheName%" dir=out",, HIDE
   FileRead, RuleList, %A_WorkingDir%\Rules.txt
   StringReplace, NewRuleList, RuleList, %Rules%, %A_BACKSPACE%
   FileDelete, %A_WorkingDir%\Rules.txt
   FileAppend, %NewRuleList%, %A_WorkingDir%\Rules.txt
   UpdateRules()
  }
}
Return

DeleteLog:
FileDelete, %A_WorkingDir%\AHKWallLog.txt
UpDateLog()
return

RulesGuiClose:
Gui, Rules:Destroy
return

MsgBox:
 If NoMsg = 1
 {
  NoMsg = 0
  Menu, MainOptionMenu, ToggleCheck, Yez
  TempIpVar :=
 }
  else
  {
   NoMsg = 1
   Menu, MainOptionMenu, ToggleCheck, Yez
  }
return


BrowseAllowFW:
if !(WinMode = 1)
{
FileSelectFile, File, 3, ,Allow in firewall, .exe,.ahk (*.exe;*.ahk)
}
Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=in action=allow program="%File%" enable=yes remoteip=any profile=any",, HIDE
Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=out action=allow program="%File%" enable=yes remoteip=any profile=any",, HIDE
FileAppend,%Num%AHKWall: dir=in and out action=allow program="%File%" enable=yes`n, %A_WorkingDir%\Rules.txt
UpDateLog()
Num := RuleIndex()
WinMode = 0
return

IPAllowFW:
InputBox, AllowIPs, Allow IP, Enter one ip or multiple ips seperated by a `,
if (!AllowIPs)
{
 MsgBox, Please enter some ips.
}
 else
 {
  Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=in action=allow enable=yes remoteip="%AllowIPs%" profile=any",, HIDE
  Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=out action=allow enable=yes remoteip="%AllowIPs%" profile=any",, HIDE
  Run %comspec% /c "netsh advfirewall firewall set rule name = "%Num%AHKWall" new remoteip = "%AllowIPs%",, HIDE
  FileAppend,%Num%AHKWall: dir=in and out action=allow enable=yes remoteip=%AllowIPs% profile=any`n, %A_WorkingDir%\Rules.txt
  IniRead, OldAllowList, %A_WorkingDir%\WallLists.ini, Whitelist, IP
  NewAllowList := OldAllowList . AllowIPs . ","
  IniWrite, %NewAllowList%, %A_WorkingDir%\WallLists.ini, Whitelist, IP
  Num := RuleIndex()
 }
return

IPDenyFW:
InputBox, DenyIPs, Deny IP, Enter one ip or multiple ips seperated by a `,
if (!DenyIPs)
{
 MsgBox, Please enter some ips.
}
 else
 {
  Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=in action=block enable=yes remoteip="%DenyIPs%" profile=any",, HIDE
  Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=out action=block enable=yes remoteip="%DenyIPs%" profile=any",, HIDE
  Run %comspec% /c "netsh advfirewall firewall set rule name = "%Num%AHKWall" new remoteip = "%DenyIPs%",, HIDE
  FileAppend,%Num%AHKWall: dir=in and out action=block enable=yes remoteip=%DenyIPs% profile=any`n, %A_WorkingDir%\Rules.txt
  IniRead, OldDenyList, %A_WorkingDir%\WallLists.ini, Blacklist, IP
  NewDenyList := OldDenyList . DenyIPs . ","
  IniWrite, %NewDenyList%, %A_WorkingDir%\WallLists.ini, Blacklist, IP
  Num := RuleIndex()
 }
return

BrowseDenyFW:
if !(WinMode2 = 1)
{
FileSelectFile, FileD, 3, ,Deny in firewall, .exe,.ahk (*.exe;*.ahk)
}
Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=in action=block program="%FileD%" enable=yes remoteip=any profile=any",, HIDE
Run %comspec% /c "netsh advfirewall firewall add rule name="%Num%AHKWall" dir=out action=block program="%FileD%" enable=yes remoteip=any profile=any",, HIDE
FileAppend,%Num%AHKWall: dir=in and out action=deny program="%FileD%" enable=yes`n, %A_WorkingDir%\Rules.txt
UpDateLog()
Num := RuleIndex()
WinMode2 = 0
return

WindowDenyFW:
Hotkey, B, WindowDeny, On
MsgBox, Activate a window then press B.
return

WindowDeny:
WinMode2 = 1
WinGet, FileD ,ProcessPath, A
Hotkey, B, WindowAllow, Off
MsgBox, Program %FileD% Denied.
Goto, BrowseDenyFW
return

WindowAllowFW:
Hotkey, A, WindowAllow, On
MsgBox, Activate a window then press A.
return

WindowAllow:
WinMode = 1
WinGet, File,ProcessPath, A
Hotkey, A, WindowAllow, Off
MsgBox, Program %File% Allowed.
Goto, BrowseAllowFW
return

RuleIndex()
{
 ifNotExist, %A_WorkingDir%\Rules.txt
 {
  Num = 1
  IniWrite, %Num%, %A_WorkingDir%\WallLists.ini, RuleNumber, Index
  return Num
 }
  else
  {
   IniRead, OldNum, %A_WorkingDir%\WallLists.ini, RuleNumber, Index
    if OldNum = ERROR
     {
      Num := LstRuleNum()
      IniWrite, %Num%, %A_WorkingDir%\WallLists.ini, RuleNumber, Index
      return Num
     }
      else
      {
       Num := OldNum+1
       IniWrite, %Num%, %A_WorkingDir%\WallLists.ini, RuleNumber, Index
        return Num
      }
  }
}

LstRuleNum()
{
 ifNotExist, %A_WorkingDir%\Rules.txt
 {
  Num = 1
 }
  else
  {
     Loop, read, %A_WorkingDir%\Rules.txt
     {
      Loop, parse, A_LoopReadLine, %A_Tab%
      {
       Num++
      }
    }
      Return Num
   }
}

UpDateLog()
{
 FileRead, Log, %A_WorkingDir%\AHKWallLog.txt
 GuiControl,, TheLog, %Log%
}
return

UpdateRules()
{
GuiControl, Rules:, Rules, |
 Loop
 {
  FileReadLine, Rules, %A_WorkingDir%\Rules.txt, %A_Index%
   if ErrorLevel
     break

 GuiControl, Rules:, Rules, %Rules%
 }
}

;==========Function by jNizM below=============
GetTcpTable()
{
    static hIPHLPAPI := DllCall("LoadLibrary", "str", "iphlpapi.dll", "ptr"), table := []
    VarSetCapacity(TBL, 4 + (s := (20 * 32)), 0)
    while (DllCall("iphlpapi\GetTcpTable", "ptr", &TBL, "uint*", s, "uint", 1) = 122)
        VarSetCapacity(TBL, 4 + s, 0)

    table := {}, index := 1
    loop % NumGet(TBL, 0, "uint") {
        o := 4 + ((index - 1) * 20)
        table[index, "LocalIP"]    := (( ROW := NumGet(TBL, o+4,  "uint"))&0xff) "." ((ROW&0xff00)>>8) "." ((ROW&0xff0000)>>16) "." ((ROW&0xff000000)>>24)
        table[index, "LocalPort"]  := (((ROW := NumGet(TBL, o+8,  "uint"))&0xff00)>>8) | ((ROW&0xff)<<8)
        table[index, "RemoteIP"]   := (( ROW := NumGet(TBL, o+12, "uint"))&0xff) "." ((ROW&0xff00)>>8) "." ((ROW&0xff0000)>>16) "." ((ROW&0xff000000)>>24)
        table[index, "RemotePort"] := (((ROW := NumGet(TBL, o+16, "uint"))&0xff00)>>8) | ((ROW&0xff)<<8)
        table[index, "State"]      := NumGet(TBL, o, "uint"), index++
    }
    return table, DllCall("FreeLibrary", "ptr", hIPHLPAPI)
}
SquirrelHugger
Posts: 17
Joined: 23 Feb 2018, 04:17

Re: AHKWall - Windows Firewall Mangement

22 Mar 2018, 18:31

I have NetLimiter installed that gives me a popout whenever anything wants to connect to the internet, I can then either allow or deny with a remember option. If I understood correctly, this script would offer that exact functionality? Or did I understood it completely wrong?
:morebeard:
Xatmo97
Posts: 15
Joined: 27 May 2014, 12:50

Re: AHKWall - Windows Firewall Mangement

22 Mar 2018, 18:46

Sorta kinda except it gives you all the other options of allowing and denying ip addresses multiple ip address and ports or port ranges

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Wicked and 29 guests