Esempio n. 1
0
	mach_error_t
mach_inject_bundle_pid(
		const char	*bundlePackageFileSystemRepresentation,
		pid_t		pid )
{
	assert( bundlePackageFileSystemRepresentation );
	assert( pid > 0 );
	
	mach_error_t	err = err_none;
	
    /*
	//	Get the framework's bundle.
	CFBundleRef frameworkBundle = NULL;
	if( !err ) {
		frameworkBundle = CFBundleGetBundleWithIdentifier(
			CFSTR("com.flyingmeat.JSTalk"));
		if( frameworkBundle == NULL )
			err = err_mach_inject_bundle_couldnt_load_framework_bundle;
	}
	
	//	Find the injection bundle by name.
	CFURLRef injectionURL = NULL;
	if( !err ) {
		injectionURL = CFBundleCopyResourceURL( frameworkBundle,
			CFSTR("mach_inject_bundle_stub.bundle"), NULL, NULL );
		if( !injectionURL )
			err = err_mach_inject_bundle_couldnt_find_injection_bundle;
	}
	*/
    
    CFStringRef filePath = CFStringCreateWithCString(nil, bundlePackageFileSystemRepresentation, kCFStringEncodingUTF8);
    CFURLRef injectionURL = CFURLCreateWithFileSystemPath(nil, filePath, kCFURLPOSIXPathStyle, true);
    
    //CFRelease(filePath);
    
	//	Create injection bundle instance.
	CFBundleRef injectionBundle = NULL;
	if( !err ) {
		injectionBundle = CFBundleCreate( kCFAllocatorDefault, injectionURL );
		if( !injectionBundle )
			err = err_mach_inject_bundle_couldnt_load_injection_bundle;
	}
	
	//	Load the thread code injection.
	void *injectionCode = injectEntry;
    /*
	if( !err ) {
		injectionCode = CFBundleGetFunctionPointerForName( injectionBundle,
			CFSTR( INJECT_ENTRY_SYMBOL ));
		if( injectionCode == NULL )
			err = err_mach_inject_bundle_couldnt_find_inject_entry_symbol;
	}
	*/
    
	//	Allocate and populate the parameter block.
	mach_inject_bundle_stub_param *param = NULL;
	size_t paramSize;
	if( !err ) {
		size_t bundlePathSize = strlen( bundlePackageFileSystemRepresentation )
			+ 1;
		paramSize = sizeof( ptrdiff_t ) + bundlePathSize;
		param = malloc( paramSize );
		bcopy( bundlePackageFileSystemRepresentation,
			   param->bundlePackageFileSystemRepresentation,
			   bundlePathSize );
	}
	
	//	Inject the code.
	if( !err ) {
		err = mach_inject( injectionCode, param, paramSize, pid, 0 );
	}
	
	//	Clean up.
	if( param )
		free( param );
	if( injectionBundle )
		CFRelease( injectionBundle );
	if( injectionURL )
		CFRelease( injectionURL );
	
	return err;
}
OSErr injectCode() {
    mach_error_t err = err_none;
    
    //printf("Attempting to install Dock patch...\n");
    
    // Get the main bundle for the app.
    CFBundleRef mainBundle = NULL;
    if(!err) {
        mainBundle = CFBundleGetMainBundle();
    if( !mainBundle )
                    err = err_couldnt_load_main_bundle;
    }
    
    // Find our injection bundle by name.
    CFURLRef injectionURL = NULL;
    if( !err ) {
        injectionURL = CFBundleCopyResourceURL( mainBundle,
            CFSTR("DockExtension.bundle"), NULL, NULL );
        if( !injectionURL )
            err = err_couldnt_find_injection_bundle;
    }
	
    //	Create injection bundle instance.
    CFBundleRef injectionBundle = NULL;
    if( !err ) {
        injectionBundle = CFBundleCreate( kCFAllocatorDefault, injectionURL );
        if( !injectionBundle )
            err = err_couldnt_load_injection_bundle;
    }
	
    //	Load the thread code injection.
    void *injectionCode = NULL;
    if( !err ) {
        injectionCode = CFBundleGetFunctionPointerForName( injectionBundle, 
        CFSTR( INJECT_ENTRY_SYMBOL ));
        if( injectionCode == NULL )
            err = err_couldnt_find_injectedThread_symbol;
    }
		
    //	Find target by signature.
    ProcessSerialNumber psn;
    if( !err )
        err = mac_err( FindProcessBySignature( 'APPL', 'dock', &psn ));
	
    //	Convert PSN to PID.
    pid_t dockpid;
    if( !err )
        err = mac_err( GetProcessPID( &psn, &dockpid ));
    //if( !err )
    //    printf( "pid: %ld\n", (long) dockpid );
	
    //	Inject the code.
    if( !err )
        err = mach_inject( injectionCode, NULL, 0, dockpid, 0 );
	
    if(err) {
        printf("Failed!\n");
    }
        
    //	Clean up.
    if( injectionBundle )
        CFRelease( injectionBundle );
    if( injectionURL )
        CFRelease( injectionURL );
    if( mainBundle )
        CFRelease( mainBundle );

    return err;
}