Skip to content

Commit

Permalink
darwin: add core_graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Mar 20, 2022
1 parent 51afef9 commit 6b62b6b
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rpcclient/rpcclient/darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rpcclient.darwin import objective_c_class
from rpcclient.darwin.bluetooth import Bluetooth
from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType, CFStringEncoding, kCFAllocatorDefault
from rpcclient.darwin.core_graphics import CoreGraphics
from rpcclient.darwin.darwin_lief import DarwinLief
from rpcclient.darwin.fs import DarwinFs
from rpcclient.darwin.hid import Hid
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self, sock, sysname: str, hostname: str, port: int = None):
self.hid = Hid(self)
self.lief = DarwinLief(self)
self.bluetooth = Bluetooth(self)
self.core_graphics = CoreGraphics(self)
self.type_decoders = {
self.symbols.CFNullGetTypeID(): self._decode_cfnull,
self.symbols.CFStringGetTypeID(): self._decode_cfstr,
Expand Down
86 changes: 86 additions & 0 deletions src/rpcclient/rpcclient/darwin/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,89 @@ class IOHIDEventFieldDigitizer(Enum):
kIOHIDEventFieldDigitizerWillUpdateMask = auto()
kIOHIDEventFieldDigitizerDidUpdateMask = auto()
kIOHIDEventFieldDigitizerEstimatedMask = auto()


kCGHIDEventTap = 0
kCGWindowListOptionAll = 0
kCGNullWindowID = 0

CG_KEY_A = 0
CG_KEY_S = 1
CG_KEY_D = 2
CG_KEY_F = 3
CG_KEY_H = 4
CG_KEY_G = 5
CG_KEY_Z = 6
CG_KEY_X = 7
CG_KEY_C = 8
CG_KEY_V = 9
CG_KEY_B = 11
CG_KEY_Q = 12
CG_KEY_W = 13
CG_KEY_E = 14
CG_KEY_R = 15
CG_KEY_Y = 16
CG_KEY_T = 17
CG_KEY_1 = 18
CG_KEY_2 = 19
CG_KEY_3 = 20
CG_KEY_4 = 21
CG_KEY_6 = 22
CG_KEY_5 = 23
CG_KEY_EQUAL = 24
CG_KEY_9 = 25
CG_KEY_7 = 26
CG_KEY_DASH = 27
CG_KEY_8 = 28
CG_KEY_0 = 29
CG_KEY_CLOSE_SQUARE_BRACKETS = 30
CG_KEY_O = 31
CG_KEY_U = 32
CG_KEY_OPEN_SQUARE_BRACKETS = 33
CG_KEY_I = 34
CG_KEY_P = 35
CG_KEY_RETURN = 36
CG_KEY_L = 37
CG_KEY_J = 38
CG_KEY_COLON = 39
CG_KEY_K = 40
CG_KEY_SEMICOLON = 41
CG_KEY_BACKSLASH = 42
CG_KEY_COMMA = 43
CG_KEY_SLASH = 44
CG_KEY_N = 45
CG_KEY_M = 46
CG_KEY_DOT = 47
CG_KEY_TAB = 48
CG_KEY_SPACE = 49
CG_KEY_TILDE = 50
CG_KEY_DELETE = 51
CG_KEY_ENTER = 52
CG_KEY_ESCAPE = 53
CG_KEY_ASTERISK = 67
CG_KEY_PLUS = 69
CG_KEY_CLEAR = 71
CG_KEY_F5 = 96
CG_KEY_F6 = 97
CG_KEY_F7 = 98
CG_KEY_F3 = 99
CG_KEY_F8 = 100
CG_KEY_F9 = 101
CG_KEY_F11 = 103
CG_KEY_F13 = 105
CG_KEY_F14 = 107
CG_KEY_F10 = 109
CG_KEY_F12 = 111
CG_KEY_F15 = 113
CG_KEY_HELP = 114
CG_KEY_HOME = 115
CG_KEY_PGUP = 116
CG_KEY_F4 = 118
CG_KEY_END = 119
CG_KEY_F2 = 120
CG_KEY_PGDN = 121
CG_KEY_F1 = 122
CG_KEY_LEFT = 123
CG_KEY_RIGHT = 124
CG_KEY_DOWN = 125
CG_KEY_UP = 126
41 changes: 41 additions & 0 deletions src/rpcclient/rpcclient/darwin/core_graphics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import time
from typing import Union

from rpcclient.darwin.consts import kCGHIDEventTap, kCGWindowListOptionAll, kCGNullWindowID
from rpcclient.exceptions import BadReturnValueError


class CoreGraphics:
""" Manage Core Graphics events. """

def __init__(self, client):
self._client = client

@property
def window_list(self):
"""
get a list of all opened windows
https://developer.apple.com/documentation/coregraphics/1455137-cgwindowlistcopywindowinfo?language=objc
"""
return self._client.symbols.CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID).py

def send_key_press(self, key_code: int, interval: Union[float, int] = 0):
"""
Send a key-press event.
Accessibility features must be allowed.
"""
self.send_keyboard_event(key_code, True)
if interval:
time.sleep(interval)
self.send_keyboard_event(key_code, False)

def send_keyboard_event(self, key_code: int, down: bool):
"""
send a CG keyboard event
https://developer.apple.com/documentation/coregraphics/1456564-cgeventcreatekeyboardevent
"""
event = self._client.symbols.CGEventCreateKeyboardEvent(0, key_code, down)
if not event:
raise BadReturnValueError('CGEventCreateKeyboardEvent() failed')

self._client.symbols.CGEventPost(kCGHIDEventTap, event)

0 comments on commit 6b62b6b

Please sign in to comment.