我正在编写一个Python脚本,该脚本主要是一个Windows主机上实时取证数据收集的GUI框架。 到目前为止,我还没有遇到很多无法解决的问题。 但是这个让我难过。 这可能是一个涉及变量范围不正确的问题,但是在我的生命中,我找不到问题。 该脚本本身有两千多行,因此,除非另有说明,否则我将复制相关部分。

我对破坏功能的代码进行了更改。 它曾经可以按预期完美地工作,现在我不知道为什么不这样做。 毫无疑问,这肯定会导致我将来在项目中使用版本控制软件。

奇怪的是根本没有抛出任何异常。

我想要做的是让用户选择与他们想要执行的可选功能相对应的Tkinter Checkbutton小部件。 我已经为每个Checkbutton小部件设置了Tkinter IntVars,并将这些IntVars放入一个数组中以便于枚举。 GUI有一个“开始”按钮,按下该按钮可启动start()函数。 如果IntVar设置为1(指示选择),则此函数应枚举数组中的Checkbutton IntVars并执行相关功能。

我的代码的所有其他部分均按预期运行。 我在填充数组上度过了恐怖的时光。 似乎我不能修改数组,即使在函数内声明为全局。 如果我手动设置数组中的值,它将按预期运行。 我无法遍历IntVars并将“全局”数组设置为应为其正确的值。 看来这两个数组永远都不会被修改,就像所有Checkbutton IntVars都不被选中一样。

#this is an array of integers corresponding to the checked
#state of desired filetypes for collection
filetypes = [0]*34

#this is an array of integers corresponding to optional collection items
optionalitems = [0]*10

#... snipped ...

#This function performs just fine if the filetypes array is set manually...
#extensions[] is an array containing file extension strings corresponding
# to the Checkbuttons on the fileSelector Toplevel widget.
#outputdirs[] is an array containing strings with a desired output directory.
def collectFiles():
    global filetypes

    n = 0
    print("Parsing checkboxes for file collection...\r\n")
    for item in filetypes:
        if item:
           print(extensions[n] + " selected...")
           #make output dir corresponding to the desired extension
           outputdest = cwd + "/output/" + computername + outputdirs[n]
           if not os.path.exists(outputdest):
               print(outputdest + " does not exist, creating...")
               os.makedirs(outputdest)
               print("Collection for " + outputdirs[n] + " beginning...")
               for drive in drives:
                   print("Searching drive " + drive + "...")
                   for filename in search(drive, extensions[n]):
                       try:
                           i = 2
                           #outpath = cwd + "/output/" + username + outputdirs[n]
                           tempbasename = os.path.basename(filename)
                           testpath = os.path.join(outputdest, tempbasename)
                           tempbasename2 = ""
                           while os.path.exists(testpath):
                               print(testpath + " exists in output directory.")
                               tempbasename2 = str(i) + "_" + tempbasename
                               i += 1
                               testpath = os.path.join(outputdest, tempbasename2)
                           shutil.copy2(filename,testpath)
                           print("Copied:\n\t" + filename + "\n to:\n\t" + testpath + "\n")
                           logfile.write(str(datetime.datetime.now()) + "- Copied:\r\n\t" + filename
                                     + "\r\n to:\r\n\t" + testpath + "\r\n\r\n")
                       except:
                           print("****Problem copying: " + filename + "\n")
                           logfile.write(str(datetime.datetime.now()) + "****Problem copying: " + filename
                                     + "\r\n\r\n")
                           pass
        n += 1

#... snipped ...

#here's where the oddness begins.
#The optionalitems array SHOULD be set outside of this
# function, before it is called.
def doOptionalCollection():
    x = 0
    for item in optionalitems:
        if item:
            optionfunctions[x]()
        x = x + 1

    collectFiles()

#this is the routine called when the "Start" button is pressed.
# The goal here is to enumerate the checkboxes on the GUI, and
# fill filetypes[] and optionalitems[] with values corresponding to
# whether or not the Checkbuttons are selected.
def start():
    global filetypes
    global optionalitems

    #... snipped ...
    #code here performs baseline forensic data collection,
    # and performs exactly as intended.
    #... snipped ...

    #get status of checkboxes and update optionalitems array.
    # optionArray is the Array of IntVars associated with Tkinter Checkbuttons
    # obj.get() --> 0 = unchecked, 1 = checked
    i = 0
    for obj in optionArray:
        optionalitems[i] = obj.get()
        i = i + 1

    doOptionalCollection()

#this is for a dialog-style window that pops up when a button is pressed.
# The dialog contains 34 checkboxes, each with a variable to hold the state.
def showFileSelector():
    global filetypes
    fs = Toplevel(master=root)
    fs.title("Collect files by extension...")
    #set up a grid based on the extensions dictionary keys.
    # have the grid wrap on the 6th element.

    #... snipped, setup of Checkbutton tkinter windgets ...

    buttonArray = [txtButton, pdfButton, logButton, docButton, docxButton,
               rarButton, zipButton, isoButton, jarButton, jpgButton,
               jpegButton, bmpButton, pngButton, gifButton, exeButton,
               pptButton, pptxButton, aviButton, mp4Button, movButton,
               flvButton, mpgButton, wmvButton, mp3Button, flacButton,
               wmaButton, m4aButton, wavButton, psdButton, rawButton,
               apkButton, szipButton, indexdatButton, thumbsdbButton]

    #using filetypes array, set the status of the checkbox.
    #this is helpful if the dialog is re-opened, it will
    # re-populate the dialog with previous selections.
    x = 0
    for item in buttonArray:
        if filetypes[x]:
            item.select()
        x = x + 1

    varArray = [txtvar, pdfvar, logvar, docvar, docxvar,
            rarvar, zipvar, isovar, jarvar, jpgvar,
            jpegvar, bmpvar, pngvar, gifvar, exevar,
            pptvar, pptxvar, avivar, mp4var, movvar,
            flvvar, mpgvar, wmvvar, mp3var, flacvar,
            wmavar, m4avar, wavvar, psdvar, rawvar,
            apkvar, szipvar, indexdatvar, thumbsdbvar]

    def accept():
        global filetypes
        #user has possibly chosen files to collect by extension.
        # iterate varArray to determine what files to collect,
        # and store the result in filetypes[].
        #This assignment also does not work.
        x = 0
        for var in varArray:
            #var.get() to get the values of checkboxes
            # 0 = unchecked
            # 1 = checked
            filetypes[x] = var.get()
            x = x + 1
        fs.destroy()


    def cancel():
        #user has decided to discard selections and close
        # the window.
        fs.destroy()

    #... snipped, GUI placement ...

#back to the base-level indentation (no indents... not inside any functions)

#... snipped, optional item GUI Checkbutton setup ....

optionArray = [productkeyvar, outlookvar,
               recyclebinvar, skypevar,
               prefetchvar, installlogvar,
               allmediavar, win7jumplistvar,
               win7thumbcachevar, recentfilesvar]

optionfunctions = [collectProductKeys, collectOutlookAttachments,
                   collectRecycleBin, collectSkypeHistory,
                   collectPrefetchFolder, collectDeviceInstallationLogs,
                   collectMediaFiles, collectWin7JumpList,
                   collectWin7ThumbnailCache, collectShortcutRecentFiles]

#... snipped, more GUI setup ...

#and then, the obligatory...
root.mainloop()

尝试简化流程:

  1. 用户可以在GUI上设置选项,也可以选择要收集的文件类型。 如果用户选择了要收集的任何文件类型,则应该已经用所需的选择填充了filetypes []。 似乎没有发生修改此数组。

  2. 用户按下启动按钮,启动按钮命令就是start()函数。 定义了全局变量-即filetypes []和optionalitems []。

  3. 在start()内部,发生了基线取证收集,并按预期运行。

  4. 仍在start()内,获取可选集合的复选框状态,并填充optionalitems []。 似乎没有发生修改此数组。

  5. start()然后调用doOptionalCollection()。

  6. doOptionalCollection()还定义了全局变量-filetypes []和optionalitems []。 枚举数组时,它们将被读取为其初始值-全部为零。 这意味着没有任何所需的收集功能正在执行。

  7. doOptionalCollection()调用collectFiles()。

  8. collectFiles()有一个全局定义-这主要是我在函数本身中解析filetypes []时的工件。 由于filetypes数组始终为零,因此除非在代码中手动设置,否则不会收集任何信息。

这是篇很长的文章...第一次在这里发表,所以我对文本的巨大空白表示歉意。 我不想从我的任何代码中获利,所以如果您希望看到完整的预期功能,可以共享我的代码库。 我可以将整个软件包提供给有兴趣的人。 提前致谢!

  ask by user2292628 translate from so

本文未有回复,本站智能推荐: