Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added xpc mach inject sample.... check system console to see the Finder ... #4

Open
wants to merge 2 commits into
base: semver-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,7 @@
33B2AC7809285567004B1632 /* Development */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = (
ppc,
i386,
x86_64,
);
ARCHS = "$(NATIVE_ARCH_ACTUAL)";
COPY_PHASE_STRIP = NO;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand Down Expand Up @@ -307,7 +303,7 @@
33B2AC7909285567004B1632 /* Deployment */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
ARCHS = "$(NATIVE_ARCH_ACTUAL)";
COPY_PHASE_STRIP = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -333,6 +329,7 @@
33B2AC7A09285567004B1632 /* Default */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(NATIVE_ARCH_ACTUAL)";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
FRAMEWORK_VERSION = A;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,7 @@
33B2AC4B092854FB004B1632 /* Development */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = (
ppc,
i386,
x86_64,
);
ARCHS = "$(NATIVE_ARCH_ACTUAL)";
COPY_PHASE_STRIP = NO;
DEBUGGING_SYMBOLS = YES;
FRAMEWORK_SEARCH_PATHS = "";
Expand Down Expand Up @@ -248,7 +244,7 @@
33B2AC4C092854FB004B1632 /* Deployment */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
ARCHS = "$(NATIVE_ARCH_ACTUAL)";
COPY_PHASE_STRIP = YES;
FRAMEWORK_SEARCH_PATHS = "";
GCC_ENABLE_TRIGRAPHS = NO;
Expand Down Expand Up @@ -281,10 +277,7 @@
33B2AC4D092854FB004B1632 /* Default */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = (
ppc,
i386,
);
ARCHS = "$(NATIVE_ARCH_ACTUAL)";
FRAMEWORK_SEARCH_PATHS = "";
GCC_ENABLE_TRIGRAPHS = NO;
GCC_GENERATE_DEBUGGING_SYMBOLS = NO;
Expand Down
17 changes: 17 additions & 0 deletions mach_inject_example_xpc/Injector/DKInjector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Injector.h
// Dark
//
// Created by Erwan Barrier on 8/8/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <Foundation/Foundation.h>

extern dispatch_source_t g_timer_source;

@interface DKInjector : NSObject

- (mach_error_t)inject:(pid_t)pid withBundle:(const char *)bundlePackageFileSystemRepresentation;

@end
25 changes: 25 additions & 0 deletions mach_inject_example_xpc/Injector/DKInjector.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Injector.m
// Dark
//
// Created by Erwan Barrier on 8/8/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import "mach_inject_bundle.h"
#import <mach/mach_error.h>

#import "DKInjector.h"

@implementation DKInjector

- (mach_error_t)inject:(pid_t)pid withBundle:(const char *)bundlePackageFileSystemRepresentation
{

mach_error_t error = mach_inject_bundle_pid(bundlePackageFileSystemRepresentation, pid);


return (error);
}

@end
19 changes: 19 additions & 0 deletions mach_inject_example_xpc/Injector/Injector-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.erwanb.MachInjectSample.Injector</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Injector</string>
<key>CFBundleVersion</key>
<string>0.1</string>
<key>SMAuthorizedClients</key>
<array>
<string>identifier com.erwanb.MachInjectSample and certificate leaf[subject.CN] = &quot;Mac Developer: Rudy Aramayo (R2287HQBC8)&quot;</string>
<string>identifier com.erwanb.MachInjectSample and certificate leaf[subject.CN] = &quot;Mac Developer: Rudy Aramayo (R2287HQBC8) Development&quot;</string>
</array>
</dict>
</plist>
13 changes: 13 additions & 0 deletions mach_inject_example_xpc/Injector/Injector-Launchd.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.erwanb.MachInjectSample.Injector</string>
<key>MachServices</key>
<dict>
<key>com.erwanb.MachInjectSample.Injector</key>
<true/>
</dict>
</dict>
</plist>
7 changes: 7 additions & 0 deletions mach_inject_example_xpc/Injector/Injector-Prefix.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'Injector' target in the 'Injector' project
//

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
114 changes: 114 additions & 0 deletions mach_inject_example_xpc/Injector/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// main.m
// Injector
//
// Created by Erwan Barrier on 8/7/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <launch.h>
#import <ServiceManagement/ServiceManagement.h>
#import <mach/mach_error.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "DKInjector.h"

#include <syslog.h>
#include <xpc/xpc.h>


static void __XPC_Peer_Event_Handler(xpc_connection_t connection, xpc_object_t event) {
syslog(LOG_NOTICE, "Received event in helper.");

xpc_type_t type = xpc_get_type(event);

if (type == XPC_TYPE_ERROR) {
if (event == XPC_ERROR_CONNECTION_INVALID) {
// The client process on the other end of the connection has either
// crashed or cancelled the connection. After receiving this error,
// the connection is in an invalid state, and you do not need to
// call xpc_connection_cancel(). Just tear down any associated state
// here.

} else if (event == XPC_ERROR_TERMINATION_IMMINENT) {
// Handle per-connection termination cleanup.
}

} else {
xpc_connection_t remote = xpc_dictionary_get_remote_connection(event);

NSString *payloadPath = @"/Users/h0xff/Library/Developer/Xcode/DerivedData/MachInjectSample-eunqttgqvojfetagfsxvibytdmul/Build/Products/Debug/Payload.bundle";

DKInjector *injector = [DKInjector new];
pid_t pid = [[[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"]
lastObject] processIdentifier];

NSString *bundlePath = payloadPath; //[[NSBundle mainBundle] pathForResource:@"Payload" ofType:@"bundle"];

NSLog(@"Injecting Finder (%@) with %@", [NSNumber numberWithInt:pid], bundlePath);

mach_error_t err = [injector inject:pid withBundle:[bundlePath fileSystemRepresentation]];

if (err == 0) {
NSLog(@"Injected Finder");
//return YES;
} else {
NSLog(@"an error occurred while injecting Finder: %@ (error code: %@)", [NSString stringWithCString:mach_error_string(err) encoding:NSASCIIStringEncoding], [NSNumber numberWithInt:err]);
/*
*error = [[NSError alloc] initWithDomain:DKErrorDomain
code:DKErrInjection
userInfo:@{NSLocalizedDescriptionKey: DKErrInjectionDescription}];
*/
//return NO;
}


xpc_object_t reply = xpc_dictionary_create_reply(event);
xpc_dictionary_set_string(reply, "reply", "Hi there, host application!");
xpc_connection_send_message(remote, reply);
xpc_release(reply);
}
}

static void __XPC_Connection_Handler(xpc_connection_t connection) {
syslog(LOG_NOTICE, "Configuring message event handler for helper.");

xpc_connection_set_event_handler(connection, ^(xpc_object_t event) {
__XPC_Peer_Event_Handler(connection, event);
});

xpc_connection_resume(connection);
}



dispatch_source_t g_timer_source = NULL;




int main(int argc, char *argv[])
{
xpc_connection_t service = xpc_connection_create_mach_service("com.erwanb.MachInjectSample.Injector",
dispatch_get_main_queue(),
XPC_CONNECTION_MACH_SERVICE_LISTENER);

if (!service) {
syslog(LOG_NOTICE, "Failed to create service.");
exit(EXIT_FAILURE);
}

syslog(LOG_NOTICE, "Configuring connection event handler for helper");
xpc_connection_set_event_handler(service, ^(xpc_object_t connection) {
__XPC_Connection_Handler(connection);
});

xpc_connection_resume(service);

dispatch_main();

xpc_release(service);

return EXIT_SUCCESS;

}
21 changes: 21 additions & 0 deletions mach_inject_example_xpc/Installer/DKFrameworkInstaller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DKInstaller.h
// Dark
//
// Created by Erwan Barrier on 8/11/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import <Foundation/Foundation.h>

FOUNDATION_EXPORT NSString *const DKFrameworkDstPath;

extern dispatch_source_t g_timer_source;

@interface DKFrameworkInstaller : NSObject

@property (nonatomic, strong) NSError *error;

- (BOOL)installFramework:(NSString *)frameworkPath;

@end
46 changes: 46 additions & 0 deletions mach_inject_example_xpc/Installer/DKFrameworkInstaller.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// DKInstaller.m
// Dark
//
// Created by Erwan Barrier on 8/11/12.
// Copyright (c) 2012 Erwan Barrier. All rights reserved.
//

#import "DKFrameworkInstaller.h"
#include <syslog.h>


NSString *const DKFrameworkDstPath = @"/Library/Frameworks/mach_inject_bundle.framework";

@implementation DKFrameworkInstaller

@synthesize error = _error;

- (BOOL)installFramework:(NSString *)frameworkPath {

NSError *fileError;
BOOL result = YES;

syslog(LOG_NOTICE, "INSTALLING FRAMEWORK: from helper tool");


if ([[NSFileManager defaultManager] fileExistsAtPath:DKFrameworkDstPath] == YES) {
result = [[NSFileManager defaultManager] removeItemAtPath:DKFrameworkDstPath error:&fileError];

syslog(LOG_NOTICE, "INSTALLING FRAMEWORK: removing original framework");
}

if (result == YES) {
result = [[NSFileManager defaultManager] copyItemAtPath:frameworkPath toPath:DKFrameworkDstPath error:&fileError];
syslog(LOG_NOTICE, "INSTALLING FRAMEWORK: copyItemAtPath");
}

if (result == NO) {
_error = fileError;
}


return result;
}

@end
19 changes: 19 additions & 0 deletions mach_inject_example_xpc/Installer/Installer-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.erwanb.MachInjectSample.Installer</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Installer</string>
<key>CFBundleVersion</key>
<string>0.1</string>
<key>SMAuthorizedClients</key>
<array>
<string>identifier com.erwanb.MachInjectSample and certificate leaf[subject.CN] = &quot;Mac Developer: Rudy Aramayo (R2287HQBC8)&quot;</string>
<string>identifier com.erwanb.MachInjectSample and certificate leaf[subject.CN] = &quot;Mac Developer: Rudy Aramayo (R2287HQBC8) Development&quot;</string>
</array>
</dict>
</plist>
13 changes: 13 additions & 0 deletions mach_inject_example_xpc/Installer/Installer-Launchd.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.erwanb.MachInjectSample.Installer</string>
<key>MachServices</key>
<dict>
<key>com.erwanb.MachInjectSample.Installer</key>
<true/>
</dict>
</dict>
</plist>
7 changes: 7 additions & 0 deletions mach_inject_example_xpc/Installer/Installer-Prefix.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'Installer' target in the 'Installer' project
//

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
Loading