簡體   English   中英

result.append([1,matches['main'][0]['rule']]) 並收到消息 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此處提供的文件路徑內容。 完整的代碼在這里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']])時出現問題。上面的代碼有什么問題?? 我應該怎么辦?? output 應該是文件路徑中的“no packer”或規則名。

可以使用索引訪問列表,例如matches[0]、matches[1]、matches[2]..等,在您的程序中,您使用字符串'main'和'rule'訪問了一個列表,matches['main '][0]['rule'] 引發 TypeError 異常。

問題在於 Yara 模塊的 match() 方法的更改。 早些時候返回了一個字典,因此使用鍵進行訪問,但現在它返回一個列表,因此需要更改代碼。

我已經編寫了腳本,所以我在 GitHub 項目頁面上更新了相同的內容。

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

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

謝謝大家發現並解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM