Package modules :: Package auxiliary :: Module human
[hide private]
[frames] | no frames]

Source Code for Module modules.auxiliary.human

  1  #!/usr/bin/env python 
  2  # Copyright (C) 2010-2014 Cuckoo Foundation. 
  3  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org 
  4  # See the file 'docs/LICENSE' for copying permission. 
  5   
  6  import random 
  7  import logging 
  8  from threading import Thread 
  9  from ctypes import WINFUNCTYPE, POINTER 
 10  from ctypes import c_bool, c_int, create_unicode_buffer 
 11   
 12  from lib.common.abstracts import Auxiliary 
 13  from lib.common.defines import KERNEL32, USER32 
 14  from lib.common.defines import WM_GETTEXT, WM_GETTEXTLENGTH, BM_CLICK 
 15   
 16  log = logging.getLogger(__name__) 
 17   
 18  EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int)) 
 19  EnumChildProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int)) 
 20   
 21  RESOLUTION = { 
 22      "x": USER32.GetSystemMetrics(0), 
 23      "y": USER32.GetSystemMetrics(1) 
 24  } 
 25   
26 -def foreach_child(hwnd, lparam):
27 buttons = [ 28 "yes", 29 "ok", 30 "accept", 31 "next", 32 "install", 33 "run", 34 "agree", 35 "enable", 36 "don't send", 37 "continue", 38 ] 39 40 classname = create_unicode_buffer(50) 41 USER32.GetClassNameW(hwnd, classname, 50) 42 43 # Check if the class of the child is button. 44 if classname.value == "Button": 45 # Get the text of the button. 46 length = USER32.SendMessageW(hwnd, WM_GETTEXTLENGTH, 0, 0) 47 text = create_unicode_buffer(length + 1) 48 USER32.SendMessageW(hwnd, WM_GETTEXT, length + 1, text) 49 50 # Check if the button is "positive". 51 for button in buttons: 52 if button in text.value.lower(): 53 log.info("Found button \"%s\", clicking it" % text.value) 54 USER32.SetForegroundWindow(hwnd) 55 KERNEL32.Sleep(1000) 56 USER32.SendMessageW(hwnd, BM_CLICK, 0, 0) 57 # Don't search for childs (USER32.EnumChildWindows). 58 return False 59 else: 60 # Recursively search for childs (USER32.EnumChildWindows). 61 return True
62 63 64 # Callback procedure invoked for every enumerated window.
65 -def foreach_window(hwnd, lparam):
66 # If the window is visible, enumerate its child objects, looking 67 # for buttons. 68 if USER32.IsWindowVisible(hwnd): 69 USER32.EnumChildWindows(hwnd, EnumChildProc(foreach_child), 0) 70 return True
71
72 -def move_mouse():
73 x = random.randint(0, RESOLUTION["x"]) 74 y = random.randint(0, RESOLUTION["y"]) 75 76 # Originally was: 77 #USER32.mouse_event(0x8000, x, y, 0, None) 78 # Changed to SetCurorPos, since using GetCursorPos would not detect 79 # the mouse events. This actually moves the cursor around which might 80 # cause some unintended activity on the desktop. We might want to make 81 # this featur optional. 82 USER32.SetCursorPos(x, y)
83
84 -def click_mouse():
85 # Move mouse to top-middle position. 86 USER32.SetCursorPos(RESOLUTION["x"] / 2, 0) 87 # Mouse down. 88 USER32.mouse_event(2, 0, 0, 0, None) 89 KERNEL32.Sleep(50) 90 # Mouse up. 91 USER32.mouse_event(4, 0, 0, 0, None)
92
93 -class Human(Auxiliary, Thread):
94 """Human after all""" 95
96 - def __init__(self):
97 Thread.__init__(self) 98 self.do_run = True
99
100 - def stop(self):
101 self.do_run = False
102
103 - def run(self):
104 while self.do_run: 105 move_mouse() 106 click_mouse() 107 USER32.EnumWindows(EnumWindowsProc(foreach_window), 0) 108 KERNEL32.Sleep(1000)
109