Jump to content

white list of windows


Recommended Posts

I have a script that will detect a window and close it. Currently it is designed to look for notepad for proof of concept. What I like to do is have a white list of windows to allow, and if any window is opened that doesn't match the list, it gets closed.

Items I want in the whitelist are Program manager and Firefox. Any help would be welcomed.

CODE
#include <Array.au3>
dim $var, $i, $array
HotKeySet("asd", "_exit")
Opt("WinTitleMatchMode", 2)

while 1
sleep(100)
_getwinlist()
For $x = 0 to UBound($array) -1
    $result = StringInStr($array[$x], "Notepad")
    if $result > 0 then 
        WinClose("Notepad")
        _Talk("Access Denied!")
        MsgBox(0, "Access Denied!", "Please contact Volly if you need access to this application.")
        
    endif           
next        

WEnd    
   
Func _Talk($sText)
    $talk = ObjCreate("SAPI.SpVoice")
    $talk.Speak($sText)
EndFunc

func _exit()
    Exit
EndFunc 

func _getwinlist()
$array = _ArrayCreate("")
$var = WinList()
For $i = 1 to $var[0][0]
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
      $adj = _ArrayAdd($array, $var[$i][0])
  EndIf
Next
_ArrayDelete($array,0)
EndFunc

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then 
    Return 1
  Else
    Return 0
  EndIf
EndFunc
Link to comment
Share on other sites

You could try using a .txt file to do it.

CODE
#include <Array.au3>
#include <File.au3>
dim $var, $i, $array, $location="C:\Documents and Settings\"&@UserName&"\Desktop\Whitelist.txt"
Dim $lines = _FileCountLines($Location)
Dim $whitelist[$lines]
_FileReadToArray($Location,$Whitelist)
HotKeySet("asd", "_exit")
Opt("WinTitleMatchMode", 2)

while 1
sleep(100)
_getwinlist()
For $x = 0 to UBound($array) -1
    For $z = 1 to $whitelist[0]
        $result = StringInStr($array[$x], $Whitelist[$z])
        if $result > 0 then
            WinClose($array[$x])
            _Talk("Access Denied!")
            MsgBox(0, "Access Denied!", "Access to '"&$array[$x]&"' is denied!!"&@CRLF&"Please contact Volly if you need access to this application.")
        endif         
    next       
Next
WEnd   
   
Func _Talk($sText)
    $talk = ObjCreate("SAPI.SpVoice")
    $talk.Speak($sText)
EndFunc

func _exit()
    Exit
EndFunc 

func _getwinlist()
$array = _ArrayCreate("")
$var = WinList()
For $i = 1 to $var[0][0]
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
      $adj = _ArrayAdd($array, $var[$i][0])
  EndIf
Next
_ArrayDelete($array,0)
EndFunc

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then
    Return 1
  Else
    Return 0
  EndIf
EndFunc
Edited by Paulie
Link to comment
Share on other sites

I came up with this, and it works for the most part. I'm having trouble with the hotkeyset. My script keeps wanting to restart.

CODE
;asdfghjkl;
#include <Array.au3>
#include <Misc.au3>
dim $var, $i, $array
HotKeySet("^d", "_exit")
Opt("WinTitleMatchMode", 2)
if _Singleton ("asdfghjkl",1) = 0 then exit
while 1
sleep(10)
_getwinlist()
For $x = 0 to UBound($array) -1
    $result1 = StringInStr($array[$x], "SciTE")
    $result2 = StringInStr($array[$x], "Firefox")
    $result3 = StringInStr($array[$x], "Program Manager")
    $result4 = StringInStr($array[$x], "Help")
    if $result1 = 0 and $result2 = 0 and $result3 = 0 and $result4 = 0 then
        WinClose("")
        MsgBox(16 + 262144, "Access Denied!", "Please contact Volly if you need access to this application.",6)     
    endif           
next        

WEnd    
   
Func _Talk($sText)
    $talk = ObjCreate("SAPI.SpVoice")
    $talk.Speak($sText)
EndFunc

func _exit()
    Exit
    sleep(1000)
EndFunc 

func _getwinlist()
$array = _ArrayCreate("")
$var = WinList()
For $i = 1 to $var[0][0]
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
      $adj = _ArrayAdd($array, $var[$i][0])
  EndIf
Next
_ArrayDelete($array,0)
EndFunc

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then 
    Return 1
  Else
    Return 0
  EndIf
EndFunc
Edited by Volly
Link to comment
Share on other sites

I was messing with this for fun and came up with...

HotKeySet('{esc}', 'quit')
Opt('WinTitleMatchMode', 2)

Global $filter[4] = [3, 'Notepad', 'cmd.exe', 'SciTE']

While 1
    Sleep(1000)
    _enforcefilter()
WEnd

Func _enforcefilter()
    Local $winarray = _getwinlist()
    For $i = 1 To UBound($winarray) - 1
        For $x = 1 To UBound($filter) - 1
            If StringInStr($winarray[$i], $filter[$x]) Then $winarray[$i] = 'Allowed'
        Next
    Next
    For $i = 1 To UBound($winarray) - 1
        If $winarray[$i] = 'Allowed' Then ContinueLoop
        WinClose($winarray[$i])
        MsgBox(16, 'Error', 'You may not use: ' & $winarray[$i])
    Next
EndFunc

Func _getwinlist()
    Local $wins = WinList(), $ret[1], $num = 0
    For $i = 1 To $wins[0][0]
        If $wins[$i][0] <> '' And BitAND(WinGetState($wins[$i][1]), 2) Then
            ReDim $ret[$num + 1]
            $ret[$num] = $wins[$i][0]
            $num += 1
        EndIf
    Next
    $ret[0] = UBound($ret) - 1
    Return $ret
EndFunc

Func quit()
    Exit
EndFunc

edit - left in debugging stuff :)

Edited by xcal
Link to comment
Share on other sites

tried it. doesn't work. :)

It keeps popping up the program manager as if I'm trying to shutdown. I add it to the list of allowed by changing the following line:

Global $filter[6] = [3, 'Notepad', 'cmd.exe', 'SciTE', 'Firefox', 'Program manager']

Now it won't stop anything. Not sure why.

Link to comment
Share on other sites

Give it a try....

#include <Misc.au3>
#include <Array.au3>

HotKeySet("^d", "_exit")

If _Singleton("asdfghjkl", 1) = 0 Then Exit

While 1
    Sleep(10)
    _CloseThemUp("Firefox|Help|SciTe|White|Internet Explorer")
WEnd

Func _CloseThemUp($SLEAVEOPEN = "Program Manager")
    $EXCLUDE = StringSplit("Program Manager|Start|Start Menu", "|")
    If Not IsArray($SLEAVEOPEN) Then $SLEAVEOPEN = StringSplit($SLEAVEOPEN, '|')
    $SWINDOWS = WinList()
    
    For $I = 1 To $SWINDOWS[0][0]
        $CLOSE = True
        If $SWINDOWS[$I][0] <> "" And BitAND(WinGetState($SWINDOWS[$I][1]), 2) And _ArraySearch($EXCLUDE, $SWINDOWS[$I][0], 1) = -1 Then
            For $X = 1 To $SLEAVEOPEN[0]
                If StringInStr($SWINDOWS[$I][0], $SLEAVEOPEN[$X]) <> 0 Then $CLOSE = False
            Next
            If $CLOSE Then
                WinClose($SWINDOWS[$I][0])
                MsgBox(16 + 262144, "Access Denied!", "Please contact Volly if you need access to " & $SWINDOWS[$I][0], 6)
            EndIf           
        EndIf
    Next
EndFunc   ;==>_CloseThemUp

Func _exit()
    Exit
    Sleep(1000)
EndFunc   ;==>_exit
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

Hmm I can't see what's wrong with my function. I'm sure you know the logic of it, but here it is spelled out for the heck of it...

Func _enforcefilter()
    ; gets a 1d array of window titles
    Local $winarray = _getwinlist ()
    ; run through all titles
    For $i = 1 To UBound($winarray) - 1
        ; compare against the filter
        For $x = 1 To UBound($filter) - 1
            ; if a filter item is found in the array of windows, change the array entry to 'Allowed'
            If StringInStr($winarray[$i], $filter[$x]) Then $winarray[$i] = 'Allowed'
        Next
    Next
    ; run through the array of windows again, this time allowed entries are renamed to 'Allowed'
    For $i = 1 To UBound($winarray) - 1
        ; if an entry is 'Allowed' skip it
        If $winarray[$i] = 'Allowed' Then ContinueLoop
        ; if an entry isn't named 'Allowed', we get to here, and it gets closed
        WinClose($winarray[$i])
        MsgBox(16, 'Error', 'You may not use: ' & $winarray[$i])
    Next
EndFunc

I did actually remove Opt('WinTitleMatchMode', 2) from my script, as I couldn't see the point of it. I tested it all again, and it worked perfectly on my setup. :)

Link to comment
Share on other sites

I tested Danny's flavor of the script. His works well, but I'm going to make a small change where it looks at a ini file so I can update it more easily. Thanks everyone!

:D

Edit: Danny, the CPU hunger problem still exist. I've been thinking of how to address that, but the only way I know how to is to put in a sleep. That would impair performance as far as response time to close the window. :)

Edited by Volly
Link to comment
Share on other sites

I tested Danny's flavor of the script. His works well, but I'm going to make a small change where it looks at a ini file so I can update it more easily. Thanks everyone!

:D

Edit: Danny, the CPU hunger problem still exist. I've been thinking of how to address that, but the only way I know how to is to put in a sleep. That would impair performance as far as response time to close the window. :)

Personally, I don't think it really matters.

What could someone do on a computer with 1000 miliseconds or something. if the window is only open for a few seconds, i don't think it would reduce the effectiveness of the protection.

Link to comment
Share on other sites

well, I did this, and it got the CPU problem down to a manageable level (around 6), though I like to have it down to 0

Func _CloseThemUp($SLEAVEOPEN = "Program Manager")
    $EXCLUDE = StringSplit("Program Manager|Start|Start Menu|Windows Task Manager", "|")
    If Not IsArray($SLEAVEOPEN) Then $SLEAVEOPEN = StringSplit($SLEAVEOPEN, '|')
    $SWINDOWS = WinList()
    For $I = 1 To $SWINDOWS[0][0]
        $CLOSE = True
        If $SWINDOWS[$I][0] <> "" And BitAND(WinGetState($SWINDOWS[$I][1]), 2) And _ArraySearch($EXCLUDE, $SWINDOWS[$I][0], 1) = -1 Then            
            For $X = 1 To $SLEAVEOPEN[0]
                sleep(1)
                If StringInStr($SWINDOWS[$I][0], $SLEAVEOPEN[$X]) <> 0 Then 
                    $CLOSE = False
                endif   
            Next
            If $CLOSE Then
                WinClose($SWINDOWS[$I][0])
                MsgBox(16 + 262144, "Access Denied!", "Please contact Your system administrator if you need access to " & $SWINDOWS[$I][0], 6)
            EndIf         
        EndIf
    Next
EndFunc   ;==>_CloseThemUp
Link to comment
Share on other sites

The CPU hunger problem still exist.

My computer it is using 1% of CPU, but I have 1GB of RAM and 2.3 Ghz CPU. The only thing I can suggest is to use ReduceMemory() UDF by wOuter.

; Reduce memory usage
; Author wOuter ( mostly )

Func _ReduceMemory($i_PID = -1)    
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    
    Return $ai_Return[0]
EndFunc;==> _ReduceMemory()
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
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...