2017-04-15 37 views
0

我正在使用winappdbg調試,創建一個事件處理程序,該程序在連接到調試器的進程上掛鉤了createfile函數。我不能使用「循環」功能,因爲這種方式調試器是無法停用的,所以我使用其他功能。在看看有多少createfile調用發生後,我試圖獲得cpu precent的進程,並且它們自動進行到0.我認爲它是因爲調試器,但不知道爲什麼winappdbg debug + api hooking + cpu precent

while True: hooked = [ ]

eventhandelerobject = hooking4.EventReciever() 
    debugger = Debug(eventhandelerobject,False) 


    """hooking""" 
    for procid in psutil.pids(): 
     if not whitelist5.inlist(procid): 
      print procid 
      try: 
       debugger.attach(procid) 
       hooked.append(procid) 
       print "Initialized!"+str(procid) 

       errorfile.write("Initialized!"+str(procid)) 

      except Exception as ex: 
       print ex 
       errorfile.write(str(ex)+str(procid)) 
    run_time = 60 

    start = time.clock() 

    time_passed = 10 
    print "start" 
    while time.clock()-start<60: 
     ############################# 
     if time.clock()-start>time_passed: 
      for procid in psutil.pids(): 
       if not whitelist5.inlist(procid) and procid not in hooked: 
        try: 
         debugger.attach(procid) 
         hooked.append(procid) 
         print "Initialized!"+str(procid) 
        except: 
         continue 
       ###################################### 

      time_passed+=10 

     try: 
      #debugger.next() 
      event = debugger.wait(1000) 
      debugger.dispatch(event) 
      debugger.cont(event) 
     except: 
      continue 
    print "stop" 



    print "part2" 

    over_cpu_mem_id = cpu_mem4.dangerous_cpu_and_mem(0,0) 
    counter = hooking_count.Count.counterdict 
    print "counter:" 
    print counter 
    sorted_counter = sorted(counter.items(), key=operator.itemgetter(1)) 
    if over_cpu_mem_id >= 1: 
     if registry4.manage() and not whitelist5.inlist(over_cpu_mem_id): 
      print "cpu+registry , not in whitelist" 
      pid_being_suspected = over_cpu_mem_id 
      if file4.mannge(filesnumber, timeinseconds): 
       print "files are changing..." 


       if len(sorted_counter)>0: 
        most_createfiles = sorted_counter[0][1] 
        pid = sorted_counter[0][0] 
        ############################# 
        exe = psutil.Process(pid).exe() 
        print exe 
        cpuexe = psutil.Process(over_cpu_mem_id).exe() 
        print cpuexe 
        ###################### 

        if most_createfiles >= 100 and pid == over_cpu_mem_id: 
         process_control5.process_dealing(pid) 
     elif pid_being_suspected >0 and len(sorted_counter)>0: 
      most_createfiles = sorted_counter[0][1] 
      pid = sorted_counter[0][0] 
      if most_createfiles >= 100 and pid == pid_being_suspected: 
       process_control5.process_dealing(pid) 
      pid_being_suspected = -1 



    print "no danger" 


    hooking_count.Count.reset_dict() 


    time.sleep(10) 
    debugger.stop() 

回答

0

我不太清楚,因爲我從來沒有試圖讓調試的進程的CPU使用率,但我懷疑這是因爲當你嘗試和衡量,已調試的進程暫停。通常當你的調試器接受控制時,被調試的過程就會被凍結,因此你總會看到0%的使用率。

如果是這樣的話,解決方案相當複雜。你必須寫兩個腳本(或者一個腳本,但是要運行兩次)。一個調試目標,收集所有進程ID和正在打開的文件。另一個並行測量CPU使用率。