Jump to content

block specific extensions within the folder and subfolders in the firewall


Go to solution Solved by Andreik,

Recommended Posts

Hello everyone, I have a .bat file that perfectly performs the function of searching within the folder where the script is located and in all the subfolders, all the files with the .exe extension to be blocked on my firewall, but it has actually been years since I don't use autoit since I always did it as a hobby and I'm not a programmer, but I've always gotten around very well with autoit, but lack of practice doesn't allow me to transfer the few lines of my .bat file to a script in autoit, I guess For someone with enough knowledge it should be something very simple, but please do not underestimate my ability to try to achieve it.

I would appreciate it if you could give me some help on how to transcribe the code to autoit. thank you so much.

Batch File:

 

@ setlocal enableextensions
@ cd /d "%~dp0"

for /R %%f in (*.exe) do (
netsh advfirewall firewall add rule name="Bloqueados: %%f" dir=out program="%%f" action=block
netsh advfirewall firewall add rule name="Bloqueados: %%f" dir=in program="%%f" action=block
)

pause

Link to comment
Share on other sites

I tried to use the following line and it worked for a specific file name but I need it to do it with all files with a specific extension regardless of the name, as I already said the .bat works perfectly.

 

Run(@ComSpec & "/k " & 'netsh advfirewall firewall add rule name=' & '"New"' & ' dir=in action=allow program="' & NoSlash(@ScriptDir) & '\MyApp.exe"' & ' enable=yes')
Link to comment
Share on other sites

Use _FileListToArray() with a filter for exe files, then use a For loop through the returned array and Run() the command above for each exe file found.

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory

$aEXE = _FileListToArray($sPath, '*.exe', 1, False)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $aEXE[$Index] & '" dir=in program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $aEXE[$Index] & '" dir=out program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

22 minutes ago, Andreik said:

Use _FileListToArray() with a filter for exe files, then use a For loop through the returned array and Run() the command above for each exe file found.

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory

$aEXE = _FileListToArray($sPath, '*.exe', 1, False)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $aEXE[$Index] & '" dir=in program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $aEXE[$Index] & '" dir=out program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

 

Your response was incredibly fast and extremely efficient. it works perfectly. Thank you very much for your help, I spent days testing and I couldn't understand how to make the for work for this specific case.

Link to comment
Share on other sites

21 hours ago, Andreik said:

Use _FileListToArray() with a filter for exe files, then use a For loop through the returned array and Run() the command above for each exe file found.

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory

$aEXE = _FileListToArray($sPath, '*.exe', 1, False)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $aEXE[$Index] & '" dir=in program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $aEXE[$Index] & '" dir=out program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

 

@Andreik When I tried the solution I did not realize that it did not search for files with said extension in the SUBDIRECTORIES, it only searches and adds those that are in the same directory as the script but does not search for all the files in the subfolders as well. Could you help me with that? I really appreciate it.

Link to comment
Share on other sites

  • Solution
Posted (edited)

Use _FileListToArrayRec() for recursive search in all directories. Something like this:

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory

$aEXE = _FileListToArrayRec($sPath, '*.exe', 1, 1, Default, 2)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & StringReplace($aEXE[$Index], $sPath & '\', '') & '" dir=in program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & StringReplace($aEXE[$Index], $sPath & '\', '') & '" dir=out program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

or for a better rule naming:

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory
Global $sDrive, $sDir, $sFilename, $sExt

$aEXE = _FileListToArrayRec($sPath, '*.exe', $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        _PathSplit($aEXE[$Index], $sDrive, $sDir, $sFilename, $sExt)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $sFilename & '" dir=in program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $sFilename & '" dir=out program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

18 minutes ago, Andreik said:

Use _FileListToArrayRec() for recursive search in all directories. Something like this:

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory

$aEXE = _FileListToArrayRec($sPath, '*.exe', 1, 1, Default, 2)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & StringReplace($aEXE[$Index], $sPath & '\', '') & '" dir=in program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & StringReplace($aEXE[$Index], $sPath & '\', '') & '" dir=out program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

or for a better rule naming:

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory
Global $sDrive, $sDir, $sFilename, $sExt

$aEXE = _FileListToArrayRec($sPath, '*.exe', $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        _PathSplit($aEXE[$Index], $sDrive, $sDir, $sFilename, $sExt)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $sFilename & '" dir=in program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados: ' & $sFilename & '" dir=out program="' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf

 

 

 

Perfect i was trying this and works until you give me the final solution.

 

#RequireAdmin
#include <File.au3>

Global $sPath = @ScriptDir  ; Replace @ScriptDir with your directory

;~ $aEXE = _FileListToArrayRec($sPath, '*.exe', 1, False)
$aEXE = _FileListToArrayRec($sPath, "*.exe||i*", $FLTAR_FILES+$FLTAR_NOSYSTEM+$FLTAR_NOHIDDEN , $FLTAR_RECUR)

If IsArray($aEXE) Then
    For $Index = 1 To $aEXE[0]
        Run('netsh advfirewall firewall add rule name="Bloqueados2: ' & $aEXE[$Index] & '" dir=in program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
        Run('netsh advfirewall firewall add rule name="Bloqueados2: ' & $aEXE[$Index] & '" dir=out program="' & $sPath & '\' & $aEXE[$Index]  & '" action=block', $sPath, @SW_HIDE)
    Next
EndIf


; A test for all folders and .exe files only throughout the folder tree, omitting folders beginning with I (Icons and Include)
;~  $aArray = _FileListToArrayRec($sPath, "*.exe||i*", $FLTAR_FILES+$FLTAR_NOSYSTEM+$FLTAR_NOHIDDEN , $FLTAR_RECUR)
;~  _ArrayDisplay($aArray, "Recur with filter")

 

Link to comment
Share on other sites

You didn't said in your first post that you want to get exe files from subdirectories, so you got what you asked for. It's good that you have a solution now.

When the words fail... music speaks.

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