develogy

Array's Consistantly Not Being Read

8 posts in this topic

#1 ·  Posted (edited)

I am setting up a utility to block applications on computers except where they relate to the "task" at hand. I have run into a curious problem. 

When the application detects a process that is not in the GLobalWhitelist, Whitelist, or BlackList it will prompt for how to handle it in the future, saving the response to text files. However, I have noticed that often it will repeatedly ask for a process, even though the process name has already been added to a list, and the Array's have been rebuilt. This seems to occur more with the $aWhiteList array.

Here is the code: 

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#Include <Timers.au3>
#include <Process.au3>
#include <InetConstants.au3>
#include <GDIPlus.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
$BlackList = @ScriptDir & "/AppBlockerBlackList.txt"
$WhiteList = @ScriptDir & "/AppBlockerWhiteList.txt"
$GlobalList = @ScriptDir & "/AppBlockerGlobalList.txt"
; switch to false to demand command line params
Global $OnFocusTask="Testing"
If $CmdLine[0]<>0 Then
  $OnFocusTask=StringReplace($CmdLine[1],"Task:","")
EndIf
Global $BlackList, $WhiteList, $GlobalList, $aWhiteList, $aBlackList, $aGlobalList
Global $hGUI, $hImage, $hGraphic
RefreshLists()
;; Lock Down
While 1
    ;RefreshLists()
    $ProcessList = ProcessList()
    If IsArray ($ProcessList) Then
        For $I = 1 To $ProcessList[0][0]
            If $ProcessList[$I][0] == "[System Process]" OR $ProcessList[$I][0] ==  "System" Then
                ContinueLoop
            EndIf
            If _AlreadyInArray ($aGlobalList, $ProcessList[$I][0] ) Then
                ; Check Global Whitelist - i.e.. System Processes
                ;ConsoleWrite ($ProcessList[$I][0] & " - Global" & @CRLF)
                ContinueLoop
            Else
                If _AlreadyInArray ($aWhiteList, $ProcessList[$I][0] ) Then
                    ; Check Current Whitelist - Task White List
                    ;ConsoleWrite ($ProcessList[$I][0] & " - Allowed" & @CRLF)
                    ContinueLoop
                Else
                    If _AlreadyInArray ($aBlackList, $ProcessList[$I][0] ) Then
                        ; Check Current BlackList - Task BlackList
                        $PID = ProcessExists ($ProcessList[$I][1])
                        ProcessClose ($PID)
                        ;ConsoleWrite ($ProcessList[$I][0] & " - Denied" & @CRLF)
                        ContinueLoop
                    Else
                        ConsoleWrite ($ProcessList[$I][0] & " - Unknown" & @CRLF)
                        PromptToAllow($ProcessList[$I][0])
                        ExitLoop
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
    Sleep(2000)
WEnd
; #FUNCTION# ====================================================================================================================
; Name ..........: _AlreadyInArray
; Description ...:
; Syntax ........: _AlreadyInArray ($_SearchArray, $_Item)
; Parameters ....: $_SearchArray        - an unknown value.
;                  $_Item               - an unknown value.
; Return values .: None
; Author ........: Unknown
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _AlreadyInArray ( $_SearchArray, $_Item )
    $_Index = _ArraySearch ( $_SearchArray, $_Item )
    If @error Then
        ;ConsoleWrite ("Array Search Failed: " & @error & @CRLF)
        Return False
    Else
        If  $_Index <> 0 Then
            Return True
        Else
            Return False
        EndIf
    EndIf
EndFunc ;==> _AlreadyInArray ( )

; #FUNCTION# ====================================================================================================================
; Name ..........: PromptToAllow
; Description ...:
; Syntax ........: PromptToAllow($process)
; Parameters ....: $process             - a pointer value.
; Return values .: None
; Author ........: Mike Miller
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func PromptToAllow($process)
    ;; Prompt for Allow/Global Allow/Deny
; Create GUI
$st1 = $WS_POPUP
$st2 = BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW,$WS_EX_TOPMOST,$WS_EX_LAYERED)
; Load PNG image
_GDIPlus_StartUp()
$hImage   = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\degree.png")
; Get Image Dimensions Used to Size the GUI
$hImageWidth = _GDIPlus_ImageGetWidth($hImage)
$hImageHeight = _GDIPlus_ImageGetHeight($hImage)
; Create GUI
$hGUI = GUICreate("Show PNG", $hImageWidth, $hImageHeight,-1,-1,$st1,$st2)
_WinAPI_SetLayeredWindowAttributes($hGUI, 0xABCDEF, 255)
GUISetBkColor(0xABCDEF)
GUISetState()
; Draw PNG image
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
$NotifyTitle = GUICtrlCreateLabel("OnFocus - AppBlock", 105, 25, 185, 28, $SS_CENTER)
GUICtrlSetFont(-1, 11, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$TextLineOne = GUICtrlCreateLabel($process, 95, 50, 195, 23, $SS_CENTER)
GUICtrlSetFont(-1, 9, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$TextLineTwo = GUICtrlCreateLabel("Related to " & $OnFocusTask & "?", 105, 70, 185, 23, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
$Button1 = GUICtrlCreateButton("Global Allow", 35, 90, 81, 25, $BS_PUSHBOX)
GUICtrlSetBkColor(-1, 0x3399FF)
$Button2 = GUICtrlCreateButton("Yes", 125, 90, 81, 25, $BS_PUSHBOX)
GUICtrlSetBkColor(-1, 0x3399FF)
$Button3 = GUICtrlCreateButton("No", 215, 90, 81, 25, $BS_PUSHBOX)
GUICtrlSetBkColor(-1, 0x3399FF)
$CloseX = GUICtrlCreateLabel("X", 282, 23, 20, 28, $SS_CENTER)
GUICtrlSetFont(-1, 11, 800, 0, "Arial")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x000000)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $CloseX
            CleanUP()
            RefreshLists()
            ExitLoop
        Case $Button1
            AddGlobal($process)
            CleanUP()
            RefreshLists()
            ExitLoop
        Case $Button2
            AddAllow($process)
            CleanUP()
            RefreshLists()
            ExitLoop
        Case $Button3
            AddDeny($process)
            $PID = ProcessExists ($process)
            ProcessClose ($PID)
            CleanUP()
            RefreshLists()
            ExitLoop
    EndSwitch
WEnd
RefreshLists()
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: CleanUP
; Description ...:
; Syntax ........: CleanUP()
; Parameters ....:
; Return values .: None
; Author ........: Mike Miller
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func CleanUP()
; Clean up resources
        _GDIPlus_GraphicsDispose($hGraphic)
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_ShutDown()
        GUIDelete($hGUI)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: RefreshLists
; Description ...:
; Syntax ........: RefreshLists()
; Parameters ....:
; Return values .: None
; Author ........: Mike Miller
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func RefreshLists()
; Refresh Local Arrays
_FileReadToArray($WhiteList,$aWhiteList, $FRTA_NOCOUNT)
sleep(100)
_FileReadToArray($BlackList,$aBlackList, $FRTA_NOCOUNT)
sleep(100)
_FileReadToArray($GlobalList,$aGlobalList, $FRTA_NOCOUNT)
sleep(100)
;_ArrayDisplay($aWhiteList, "WhiteList")
;sleep(100)
;_ArrayDisplay($aBlackList, "BlackList")
;sleep(100)
;_ArrayDisplay($aGlobalList, "Global Allow")
;sleep(100)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: AddAllow
; Description ...:
; Syntax ........: AddAllow($process)
; Parameters ....: $process - a pointer value.
; Return values .: None
; Author ........: Mike Miller
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func AddAllow($process)
    ; Add to Local List
        FileWriteLine($WhiteList, $process)
    ; Notify API that Process was Allowed

EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: AddDeny
; Description ...:
; Syntax ........: AddDeny($process)
; Parameters ....: $process             - a pointer value.
; Return values .: None
; Author ........: Mike Miller
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func AddDeny($process)
    ; Add to Local List
        FileWriteLine($BlackList, $process)
    ; Notify API that Process was Denied

EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: AddGlobal
; Description ...:
; Syntax ........: AddGlobal($process)
; Parameters ....: $process             - a pointer value.
; Return values .: None
; Author ........: Mike Miller
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func AddGlobal($process)
    ; Add to Local List
        FileWriteLine($GlobalList, $process)
    ; Notify API that Process was Global Allowed

EndFunc

 

I have also attached it as a zip file, with the text files, and the image required to show the prompt.

Any help would be greatly appreciated.

AppBlock.zip

Edited by develogy

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Are you passing an array into the FileWriteLine without specifying the index?  Just wondering about that second parameter ...

 

 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

No, just a text string.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

My mistake - I was confusing it with $processlist.  

Edit: Noticed that the search does not appear to be working correctly.  I added one process to the global list then exited the program.  I see t in the file.  When I open it back up it still hits the else statement in the loop so that it shows in the console as -unknown .  That may be part of the problem.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

Yes, I think your right, but do not know why.....

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Maybe has to do with the @error flag being set in the loop.  It may not be cleared when a successful search returns a positive value ... ? 

EDIT I think this is the problem:

Func _AlreadyInArray ( $_SearchArray, $_Item )
    $_Index = _ArraySearch ( $_SearchArray, $_Item )
    If @error Then ; will be true when first if in the else condition is met so will never get to that second test will return false instead
        ;ConsoleWrite ("Array Search Failed: " & @error & @CRLF)
        Return False
    Else
        If  $_Index <> 0 Then ; this is the sasme as If @error - this code won't be reached (and would mean it was not found so maybe shoudld return false)
            Return True
        Else
            Return False
        EndIf
    EndIf
EndFunc ;==> _AlreadyInArray ( )

 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Would it be worth it time/resource wise to loop through each item and do an if equal test? Or is ArraySearch better, provided I can figure out to get to stop "false" erroring?

EDIT:

Would this work?
 

Func _AlreadyInArray ( $_SearchArray, $_Item )
    $_Index = _ArraySearch ( $_SearchArray, $_Item )
    If NOT @error AND $_Index >= 0 Then
        Return True
    Else
        Return False
    EndIf
EndFunc ;==> _AlreadyInArray ( )

 

Edited by develogy
new Idea

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

That search finds it but there is another problem.  The For loop at the top won't loop through all the items in the array.  I still get the -unknown just after I find my process and then it exits the loop 4 items into a 129 long array.   I think this is the issue:

Else ; did not find the item on any list
    ConsoleWrite ($ProcessList[$I][0] & " - Unknown" & @CRLF) #cs write that it is unknown (this was the item after the one it found for me not the item on my list as originally reported - they have similar names but it make sense since I only had one item on one list).
    #ce
    PromptToAllow($ProcessList[$I][0]) ; this will not stop the next thing from happening (exitloop) - also there is an exit on each button
    ExitLoop; - loop exits
EndIf

In my case I have the count at [0][0], then some system stuff, then the one item I added.  After it gets to my added item it exits on the 4th index.  This is what would happen whenever it does not find a process on one of the lists (I think).  That may be why it appears to work for some cases.  On a related note, I am not sure you want to exit the loop when you press any button?  On a broader and still related note  ... it looks like the script treats the GUI prompt as if it were a message box that halts the script while waiting for user input - but it doesn't.  A message box would stop the script until it received an answer.  The GUI will just appear and listen for input but the script will continue.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
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