result.append([1,匹配['main'][0]['rule']])并获取消息类型错误:列表索引必须是整数,而不是str

2024-04-28 04:40:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用下面的代码,但它不起作用。。filepath的内容可在此处获取peid.yara。完整代码在这里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']])时出现了问题。上面的代码有什么问题??。我该怎么办?? 文件路径中的输出应为“无打包器”或rulename


Tags: inpyselfsourceoutputmainlineresult
2条回答

问题在于Yara模块的match()方法的更改。早期的字典是返回的,因此使用键访问,但现在它返回一个列表,因此需要更改代码

我已经编写了脚本,因此在GitHub项目页面上更新了相同的脚本

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

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

谢谢大家发现并解决了这个问题

可以使用索引访问列表,例如匹配项[0]、匹配项[1]、匹配项[2]。。等等,在您的程序中,您使用字符串“main”和“rule”访问列表,匹配['main'][0]['rule'],这会引发TypeError异常

相关问题 更多 >