mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 11:42:30 +00:00
extracted MPModifiedKey, fixed autotype obfuscation to correctly use modifiers
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import "MPModifiedKey.h"
|
||||
@class MPAutotypeContext;
|
||||
|
||||
/**
|
||||
@@ -39,6 +39,7 @@
|
||||
* @param flags modifier flags for the key press event
|
||||
*/
|
||||
- (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags;
|
||||
- (void)sendPressKey:(MPModifiedKey)key;
|
||||
|
||||
/**
|
||||
* Convenience message to be sent for executing a simple paste command
|
||||
|
||||
@@ -261,10 +261,9 @@ typedef struct {
|
||||
* http://keepass.info/help/v2/autotype_obfuscation.html
|
||||
*/
|
||||
|
||||
NSString *paste = @"";
|
||||
NSMutableString *paste = [@"" mutableCopy];
|
||||
|
||||
NSMutableArray *typeKeys = [[NSMutableArray alloc] init];
|
||||
NSMutableArray *modifiers = [[NSMutableArray alloc] init];
|
||||
NSMutableArray<NSValue *> *typeKeys = [[NSMutableArray alloc] init];
|
||||
|
||||
/*
|
||||
* seed the random number generator using the first 4 bytes of the string's SHA1
|
||||
@@ -279,29 +278,21 @@ typedef struct {
|
||||
for (NSUInteger i = 0; i < pasteContent.length; i++) {
|
||||
NSUInteger part = random() % 2;
|
||||
|
||||
unichar key = [pasteContent characterAtIndex:i];
|
||||
MPModifiedKey mKey = [MPKeyMapper modifiedKeyForCharacter:[NSString stringWithFormat:@"%c", key]];
|
||||
NSString *key = [pasteContent substringWithRange:NSMakeRange(i, 1)];
|
||||
MPModifiedKey mKey = [MPKeyMapper modifiedKeyForCharacter:key];
|
||||
/* append unknown keycodes to the paste since we can't type them */
|
||||
if (part == 0 || mKey.keyCode == kMPUnknownKeyCode) {
|
||||
paste = [paste stringByAppendingFormat:@"%c", key];
|
||||
|
||||
[typeKeys addObject:@(kVK_RightArrow)];
|
||||
[modifiers addObject:@0];
|
||||
[paste appendString:key];
|
||||
[typeKeys addObject:[NSValue valueWithModifiedKey:MPMakeModifiedKey(0, kVK_RightArrow)]];
|
||||
}
|
||||
else {
|
||||
[typeKeys addObject:@(mKey.keyCode)];
|
||||
|
||||
if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:key])
|
||||
[modifiers addObject:@(kCGEventFlagMaskShift)];
|
||||
else
|
||||
[modifiers addObject:@0];
|
||||
[typeKeys addObject:[NSValue valueWithModifiedKey:mKey ]];
|
||||
}
|
||||
}
|
||||
|
||||
/* move to the end of the content */
|
||||
for (NSUInteger i = typeKeys.count; i < pasteContent.length; i++) {
|
||||
[typeKeys addObject:@(kVK_RightArrow)];
|
||||
[modifiers addObject:@0];
|
||||
[typeKeys addObject:[NSValue valueWithModifiedKey:MPMakeModifiedKey(0, kVK_RightArrow)]];
|
||||
}
|
||||
|
||||
/* add paste command */
|
||||
@@ -315,7 +306,7 @@ typedef struct {
|
||||
}
|
||||
|
||||
for (NSUInteger i = 0; i < typeKeys.count; i++) {
|
||||
[commands addObject:[[MPAutotypeKeyPress alloc] initWithModifierMask:[modifiers[i] longLongValue] keyCode:[typeKeys[i] unsignedShortValue]]];
|
||||
[commands addObject:[[MPAutotypeKeyPress alloc] initWithModifiedKey:typeKeys[i].modifiedKeyValue]];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -412,6 +403,10 @@ typedef struct {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)sendPressKey:(MPModifiedKey)key {
|
||||
[self sendPressKey:key.keyCode modifierFlags:key.modifier];
|
||||
}
|
||||
|
||||
- (void)sendPressKey:(CGKeyCode)keyCode modifierFlags:(CGEventFlags)flags {
|
||||
|
||||
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
|
||||
|
||||
@@ -7,15 +7,15 @@
|
||||
//
|
||||
|
||||
#import "MPAutotypeCommand.h"
|
||||
|
||||
#import "MPModifiedKey.h"
|
||||
/**
|
||||
* Autotype command to press a single key. Can be used with modifier keys as well
|
||||
*/
|
||||
@interface MPAutotypeKeyPress : MPAutotypeCommand
|
||||
|
||||
@property (assign) CGEventFlags modifierMask;
|
||||
@property (assign) CGKeyCode keyCode;
|
||||
@property (readonly, assign) MPModifiedKey key;
|
||||
|
||||
- (instancetype)initWithModifiedKey:(MPModifiedKey)key;
|
||||
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code;
|
||||
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask character:(NSString *)character;
|
||||
|
||||
|
||||
@@ -22,14 +22,18 @@ static CGEventFlags _updateModifierMaskForCurrentDefaults(CGEventFlags modifiers
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code {
|
||||
- (instancetype)initWithModifiedKey:(MPModifiedKey)key {
|
||||
self = [super init];
|
||||
if(self) {
|
||||
_modifierMask = _updateModifierMaskForCurrentDefaults(modiferMask);
|
||||
_keyCode = code;
|
||||
_key = key;
|
||||
_key.modifier = _updateModifierMaskForCurrentDefaults(_key.modifier);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code {
|
||||
self = [self initWithModifiedKey:MPMakeModifiedKey(modiferMask, code)];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask character:(NSString *)character {
|
||||
MPModifiedKey mappedKey = [MPKeyMapper modifiedKeyForCharacter:character];
|
||||
@@ -40,13 +44,13 @@ static CGEventFlags _updateModifierMaskForCurrentDefaults(CGEventFlags modifiers
|
||||
if(mappedKey.modifier && (modiferMask != mappedKey.modifier)) {
|
||||
NSLog(@"Supplied modifiers for character %@ do not match required modifiers", character);
|
||||
}
|
||||
self = [self initWithModifierMask:modiferMask keyCode:mappedKey.modifier];
|
||||
self = [self initWithModifierMask:modiferMask keyCode:mappedKey.keyCode];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)description {
|
||||
return [[NSString alloc] initWithFormat:@"%@: modifierMaks:%llu keyCode:%d", [self class], self.modifierMask, self.keyCode];
|
||||
return [[NSString alloc] initWithFormat:@"%@: modifierMaks:%llu keyCode:%d", [self class], self.key.modifier, self.key.keyCode];
|
||||
}
|
||||
|
||||
- (void)execute {
|
||||
@@ -54,7 +58,7 @@ static CGEventFlags _updateModifierMaskForCurrentDefaults(CGEventFlags modifiers
|
||||
return; // no valid command. Stop.
|
||||
}
|
||||
//CGKeyCode mappedKey = [self _transformKeyCode];
|
||||
[self sendPressKey:self.keyCode modifierFlags:self.modifierMask];
|
||||
[self sendPressKey:self.key];
|
||||
}
|
||||
|
||||
- (BOOL)isValid {
|
||||
|
||||
@@ -7,21 +7,10 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MPModifiedKey.h"
|
||||
|
||||
FOUNDATION_EXTERN uint16_t const kMPUnknownKeyCode;
|
||||
|
||||
typedef struct {
|
||||
CGEventFlags modifier;
|
||||
CGKeyCode keyCode;
|
||||
} MPModifiedKey;
|
||||
|
||||
NS_INLINE MPModifiedKey MPMakeModifiedKey(CGEventFlags modifier, CGKeyCode keyCode) {
|
||||
MPModifiedKey k;
|
||||
k.keyCode = keyCode;
|
||||
k.modifier = modifier;
|
||||
return k;
|
||||
}
|
||||
|
||||
@interface MPKeyMapper : NSObject
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#import "MPKeyMapper.h"
|
||||
#import <Carbon/Carbon.h>
|
||||
|
||||
#define MPSafeSetPointer(pointer, value) if(pointer != 0) { *pointer = value; }
|
||||
#define MPArrayCount(array) (sizeof(array) / sizeof(array[0]))
|
||||
|
||||
uint16_t const kMPUnknownKeyCode = UINT16_MAX;
|
||||
|
||||
@@ -106,12 +106,11 @@ uint16_t const kMPUnknownKeyCode = UINT16_MAX;
|
||||
/* Loop through every keycode (0 - 127) to find its current mapping. */
|
||||
/* Loop throuhg every control key compbination for every virtual key */
|
||||
for(CGKeyCode keyCode = 0; keyCode < 128; ++keyCode) {
|
||||
for(int modifierIndex = 0; modifierIndex < sizeof(modifierCombinations); modifierIndex++) {
|
||||
for(int modifierIndex = 0; modifierIndex < MPArrayCount(modifierCombinations); modifierIndex++) {
|
||||
MPModifiedKey mKey = MPMakeModifiedKey(modifierCombinations[modifierIndex], keyCode);
|
||||
NSString *string = [self stringForModifiedKey:mKey];
|
||||
if(string != nil && string.length > 0 && nil == tempCharToCodeDict[string]) {
|
||||
NSValue *value = [NSValue valueWithBytes:&mKey objCType:@encode(MPModifiedKey)];
|
||||
tempCharToCodeDict[string] = value;
|
||||
tempCharToCodeDict[string] = [NSValue valueWithModifiedKey:mKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,9 +122,7 @@ uint16_t const kMPUnknownKeyCode = UINT16_MAX;
|
||||
if(!result) {
|
||||
return MPMakeModifiedKey(0, kMPUnknownKeyCode);
|
||||
}
|
||||
MPModifiedKey mKey;
|
||||
[result getValue:&mKey];
|
||||
return mKey;
|
||||
return result.modifiedKeyValue;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
26
MacPass/MPModifiedKey.h
Normal file
26
MacPass/MPModifiedKey.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// MPModifiedKey.h
|
||||
// MacPass
|
||||
//
|
||||
// Created by Michael Starke on 26/01/2017.
|
||||
// Copyright © 2017 HicknHack Software GmbH. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
typedef struct {
|
||||
CGEventFlags modifier;
|
||||
CGKeyCode keyCode;
|
||||
} MPModifiedKey;
|
||||
|
||||
NS_INLINE MPModifiedKey MPMakeModifiedKey(CGEventFlags modifier, CGKeyCode keyCode) {
|
||||
MPModifiedKey k;
|
||||
k.keyCode = keyCode;
|
||||
k.modifier = modifier;
|
||||
return k;
|
||||
}
|
||||
|
||||
@interface NSValue(NSValueMPModifiedKeyExtensions)
|
||||
@property (nonatomic, readonly, assign) MPModifiedKey modifiedKeyValue;
|
||||
+ (instancetype)valueWithModifiedKey:(MPModifiedKey)key;
|
||||
@end
|
||||
24
MacPass/MPModifiedKey.m
Normal file
24
MacPass/MPModifiedKey.m
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// MPModifiedKey.m
|
||||
// MacPass
|
||||
//
|
||||
// Created by Michael Starke on 26/01/2017.
|
||||
// Copyright © 2017 HicknHack Software GmbH. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MPModifiedKey.h"
|
||||
|
||||
@implementation NSValue(NSValueMPModifiedKeyExtensions)
|
||||
|
||||
- (MPModifiedKey)modifiedKeyValue {
|
||||
MPModifiedKey key;
|
||||
[self getValue:&key];
|
||||
return key;
|
||||
}
|
||||
|
||||
+ (instancetype)valueWithModifiedKey:(MPModifiedKey)key {
|
||||
return [NSValue valueWithBytes:&key objCType:@encode(MPModifiedKey)];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user