result.append([1,matches['main'][0]['rule']]) and got messages TypeError: list indices must be integers, not str

result.append([1,matches['main'][0]['rule']]) and got messages TypeError: list indices must be integers, not str

我在下面使用此代码,但它不起作用.. filepath 的内容可在此处 peid.yara. full code here integrated_feature_extraction.py

def __init__(self,source,output,label):
        self.source = source
        self.output = output
        self.type = label
    #Need PEiD rules compile with yara
        self.rules= yara.compile(filepath='/home/osboxes/honeymalware/scripts/peid.yara')  
        
def check_packer(self,filepath):
        result=[]
        matches = self.rules.match(filepath)
        if matches == []:
               result.append([0,"NoPacker"])
        else:
               result.append([1,matches['main'][0]['rule']])
        return result
    
def main():    
        source_path= raw_input("Enter the path of samples (ending with /) >>  ")
        output_file= raw_input("Give file name of output file. (.csv) >>")
        label = raw_input("Enter type of sample( malware(1)|benign(0))>>")

当我 运行 程序出现错误时

Traceback (most recent call last):
  File "integrated_features_extraction.py", line 375, in <module>
    main()
  File "integrated_features_extraction.py", line 372, in main
    features.create_dataset()
  File "integrated_features_extraction.py", line 356, in create_dataset
    data = self.extract_all(filepath)
  File "integrated_features_extraction.py", line 330, in extract_all
    packer = self.check_packer(filepath)
  File "integrated_features_extraction.py", line 239, in check_packer
    result.append([1,matches['main'][0]['rule']])
TypeError: list indices must be integers, not str

我认为执行 result.append([1,matches['main'][0]['rule']]) 时出现问题。上面的代码有什么问题??。我应该怎么办 ?? 输出应该是“no packer”或文件路径中的规则名称。

可以使用索引访问列表,例如 matches[0]、matches[1]、matches[2] .. 等,在您的程序中,您使用字符串 'main' 和 'rule', matches['main'][0]['rule'] 引发 TypeError.

异常

问题在于 Yara 模块的 match() 方法发生了变化。早期的字典是 return,因此它是使用键访问的,但现在它是 return 的列表,因此需要更改代码。

我已经编写了脚本,所以我在 GitHub 项目页面上更新了脚本。

 else:
        #result.append([1,matches['main'][0]['rule']])

        result.append([1,matches[0]])

感谢大家发现并解决问题。