Code Monkey home page Code Monkey logo

mach_inject's Introduction

Description

mach_inject enables you to "inject" code into an arbitrary process on Mac OS X. "Injection" means both 1) copying over the necessary code into the target's address space and 2) remotely creating a new thread to execute the code.

Must Be This Tall To Ride

Code injection is tricky business. You should be familiar with assembly and runtime calling conventions (ABIs) before using mach_inject.

mach_inject is certainly not bug free and patches are extremely welcome, but the onus is on you when things don't work. Please don't file a bug report stating mach_inject is crashing for you when you try to use it -- you have to be hard-core enough to debug the problem yourself.

Contributing

Please base your work off the unstable branch. Then submit your Pull Request.

Version History

mach_inject 1.3: Nov 08 2016

mach_inject 1.2: Aug 10 2012

  • [NEW] i386 and x86_64 support.

  • [CHANGE] Decoupled from mach_star. Most of folks were just using the side or the other of mach_star and this simplifies things (docs, tests).

mach_star 1.1.1 Dec 18 2005

  • General Xcode 2.2 project cleanup. mach_star now includes .xcodeproj Xcode 2.2 project files for all of its projects. The old .xcode project files have been left in place, but they aren't maintained and may not work. Xcode 2.2 is the recommended mach_star development environment -- Xcode 2.1 had a bug with inter-project dependancies which would cause compilation failure. It works now again in Xcode 2.2.

  • Inter-project dependancies should working under Xcode 2.2. Any project you pick, you should just be able to hit the "Build" button and everything should Just Work™.

  • There was a stray reference to my username in one of the project, which causes compilation headaches for some folks.

  • Bug fix: in mach_inject_bundle.c's mach_inject_bundle_pid() I no longer call CFRelease() on the framework bundle reference. Reported by Scott Kevill.

  • Added some explicit casts now required by gcc 4.

  • Added this document.

mach_star 1.1: Apr 06 2005

  • New package added: mach_inject_bundle. It has a private subproject: mach_inject_bundle_stub. The stub is a generic reusable implementation of the code that gets squirted across the address spaces, which was always tricky to write. mach_inject_bundle is an embeddable framework that wraps mach_inject and the stub with a simple fire-and-forget API.

  • The "DisposeWindowBeeperOverride" example is replaced by "DisposeWindow+Beep".

  • The "FinderDisposeWindowBeeperInjector" is replaced by "DisposeWindow+Beep_Injector".

  • All the text is now wrapped to 80 chars wide. Done to print nicely in Scott Knaster's Hacking Mac OS X Tiger. Probably will undo this word-wrap in the future. We all have widescreens nowadays, right? ;-)

  • Thanks to Jon Gotow for letting me peek at SCPatch, which I used as a guide for mach_inject_bundle. It saved me a bunch of time. Also thanks to Bob Ippolito for CALL_ON_LOAD assistance.

mach_star 1.0: Jun 18 2003

  • Initial release at MacHack 2003.

mach_inject's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mach_inject's Issues

using mach_inject in xamarin.mac

Hello,

I am using xamarin for developing a mac application, my idea is to create a binding between mach_inject and xamarin. As a first step, i created a framework out of this project.To make sure it is working fine, i created a new xcode project and linked the framework. When i run the app, i am getting the following error

2014-10-31 17:58:27.429 BrainloopShareFramework[11184:303] an error occurred while installing com.aspiresys.BrainloopShareFramework.Installer (domain: kSMErrorDomainFramework (4097))
2014-10-31 17:58:27.430 BrainloopShareFramework[11184:303] Couldn't install MachInjectSample (domain: com.erwanb.MachInjectSample.ErrorDomain code: 1)

Am i going in correct approach. is it possible to use mach_inject in xamarin mac app by creating a binding. Please help me.

[Question] Scenario where the dylib library to load is out side of the target process' sandbox container

Hi Experts,
Really impressive idea and great work from great people. Thank you all!

I have studied the code of mach_inject, and got a basic idea on how it is working. Basically it injects a code into the target process, run that piece of code from the target process, and then that code will do a dlopen() function call to load more interesting code to run.

I am think the scenario that if the dylib library dlopen() tries to open is out of the sandbox container of the target process, that is, the target process is not allowed to access the library, then dlopen() function call will fail. Any way to overcome this? Or my understanding is wrong?

Thanks and appreciate if any comment.

Calvin

mach/MACH_ERROR.h

Hi,
on my system the compiler can't find following import because I use a case-sensitive filesystem:

include <mach/MACH_ERROR.h>

I think it should be:

include <mach/mach_error.h>

mach_inject in Mavericks is broken

I have tried to run various examples of code_inject in Mavericks but they result in the target process crashing with the following stack:

Thread 6 Crashed:
0 libsystem_c.dylib 0x00007fff8e68c505 __xvprintf + 106

I am opening an issue hoping we can figure out a solution.

Support for M1 (arm)

Seems like this won't work on M1 (arm) Macs. Not sure if this project is still alive or not but if someone with the knowledge updated it or made a fork with support for arm that would be dope.

Yosemite compatibility?

Hi. I tried mach_inject + some Finder integration code on Mac OS X 10.10 Yosemite (Beta6) - and it fails in injected thread, on the first fopen/freopen/fwrite function call.
Everything works fine on Mavericks.

Anyone more experienced tried to run mach_inject on Yosemite? Or, somebody can recommend a way to debug this issue?
Thanks, all input is appreciated.

help me mac os sierra

mach_inject: found threadEntry image at: 0xde000 with size: 9888
mach_inject failing.. (os/kern) invalid address

help me please

Not able to Inject on Mac OS X 10.12.6 & Mac OS X 10.13 (17A291j) Beta release

When I inject into the process using mach_inject, that application crashes. I have observed this behaviour with one of my 10.12.6 machine(Not reproducible on all 10.12.6) and Mac OS X 10.13 (17A291j) Beta release of 10.13.

When the following code executes

// create thread and launch it
		err = thread_create_running( remoteTask, x86_THREAD_STATE64,
									 (thread_state_t) &remoteThreadState, x86_THREAD_STATE64_COUNT,
									 &remoteThread );

Application in which i am injecting get crashed.

On 10.12 when I disabled SIP, Issue vanished. and now not able to reproduce it even after re-enabling SIP.

swizzle applicationDidFinishLaunching using mach_inject

I am able to swizzle a method like when a button is clicked but I can't swizzle a method like applicationDidFinishLaunching that runs before mach_inject injects the code. Is there a solution to this where I can run code inside the targeted app at injection time?

mach_inject doesn't work on Apple processes in 10.14 unless filesystem SIP is disabled

In macOS 10.11 through 10.13, you didn't have to disable all of SIP in order to use mach_inject in Apple's apps. All that was necessary was to disable debug protections, i.e. csrutil enable --without debug.

Starting in 10.14, in order to inject into Apple processes it is also necessary to disable filesystem protections as well, i.e. csrutil enable --without debug --without fs. With debug protections disabled but filesystem protections enabled, using mach_inject on an Apple process will lead to that process crashing, and using mach_inject on non-Apple processes still works. I thought this was odd as mach_inject doesn't modify the filesystem at all, much less a protected part of it.

I'm not sure if this counts as a bug or if there's anything to be done about it, but I figured I'd bring it up here.

Inject code to deal with NSWindow

Hi there,
I used to create Firefox addons and would run code from within the process to manipulate the NSWindow's. NSWindow's, NSAlert's, etc (UIKit stuff) can only be manipulated from the main thread. ( UIKit operations for OSX and iOS must be on main thread - http://stackoverflow.com/questions/18467114/why-must-uikit-operations-be-performed-on-the-main-thread _

However recently there was a change and all code is from another process (native messaging API), so I needed to inject into the target process (browser/Firefox).

I was researching mach_inject and it seems to create a new thread in the targeted process. I was thinking of doing dispatch_async like this:

dispatch_async(dispatch_get_main_queue(), ^{
	NSAlert *alert = [[[NSAlert alloc] init] autorelease];
	[alert setMessageText:@"Hi there from main thread"];
	[alert runModal];
});

Do you think this will work? I'm very new to writing C/C++/ObjC so your reply would help my morale a lot :)

Invalid Memory Yosemite

I noticed this issue after updating to Yosemite.

It always gives me this error:
module: 0x100014898
bootstrapfn: 0x1000d9d60
pid: 3443
mach_inject failing.. (ipc/send) invalid memory
MACH ERR: 268435468

here is how I'm calling mach_inject:
mach_error_t err = mach_inject((mach_inject_entry)bootstrapfn, lib.UTF8String, strlen(lib.UTF8String) + 1, pid, 0); if (err) printf("MACH ERR: %d\n", err);

Is this happening to anyone else after updating to 10.10 and how can I resolve this issue?

One err check missing

Since the code is diligently checking every return code, it should also check it here:

before:
// Allocate the code.
vm_address_t remoteCode = (vm_address_t)NULL;
if( !err )
err = vm_allocate( remoteTask, &remoteCode, imageSize, 1 );
err = vm_protect(remoteTask, remoteCode, imageSize, 0, VM_PROT_EXECUTE | VM_PROT_WRITE | VM_PROT_READ);

after:
// Allocate the code.
vm_address_t remoteCode = (vm_address_t)NULL;
if( !err )
err = vm_allocate( remoteTask, &remoteCode, imageSize, 1 );
if( !err )
err = vm_protect(remoteTask, remoteCode, imageSize, 0, VM_PROT_EXECUTE | VM_PROT_WRITE | VM_PROT_READ);

otherwise remoteCode could be undefined.

mach_inject causes the injected process to crash after installing Mojave security update 2020-005

It seemed that after installing Mojave security update 2020-005 mach_inject no longer works. Whichever process gets a payload injected into it will crash. I can reproduce it with mach_inject_example, and it doesn't seem to matter which process I have it inject into.

At this point I'm not sure if any recent updates to macOS 10.15 or one of the Big Sur betas are also affected.

The crash report doesn't seem to be particularly revealing, but it does usually look like this for the crashing thread:

Thread 12 Crashed:
0   ???                           	0x000000010f735fc3 0 + 4554186691
1   libsystem_pthread.dylib       	0x00007fff5b49f2eb _pthread_body + 126
2   libsystem_pthread.dylib       	0x00007fff5b4a2249 _pthread_start + 66
3   libsystem_pthread.dylib       	0x00007fff5b49e40d thread_start + 13

edit: should add this still occurs when SIP is disabled

[QUESTION] mach_inject without root permissions like Dropbox

Hi!

I'm making a Finder "plugin" for Ubuntu One:
https://github.com/JoseExposito/U1-Finder-Plugin

Currently I'm using a SIMBL like loader making my plugin as a scripting addition:
https://github.com/JoseExposito/U1-Finder-Plugin/blob/master/U1%20Finder%20Injector/U1FinderInjector.m

But I don't like this method because I need to restart the Finder to allow to inject the plugin.

I see like some applications (like Dropbox) are able to use mach_inject without as for the admin password and my question is... How to do that??

Than you very much for the support in avance!

Update Example

v1.2 is largely a version created just to draw a line in the sand and denote when i386 and x86_64 support was added. The rest of the project is kinda in shambles. I'd like updated Xcode project files (or abandon Xcode project files altogether like I did with mach_override) with a working example, ideally across ppc, i386 and x86_64. But I'd settle for targeting x86_64 at first.

mach_inject crashes target in OSX 10.12 (Sierra), unless if launched through Xcode

Is this working for everyone else in 10.12/Sierra?

I have an odd situation where mach_inject works perfectly fine, when my app is launched through Xcode. If I launch my app directly, the target process crashes as soon as it attempts to inject:

Date/Time:             2016-09-21 22:55:58.682 +0100
OS Version:            Mac OS X 10.12 (16A323)
Report Version:        12
Anonymous UUID:        8AD07C6C-3EFE-5D39-B58B-393D95473947


Time Awake Since Boot: 3400 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00000000e50d34ab
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [0]

External Modification Warnings:
Thread creation by external task.

VM Regions Near 0xe50d34ab:
    Stack                  00000000bf800000-00000000c0000000 [ 8192K] rw-/rwx SM=PRV  
--> 
    Submap                 00000000ffff0000-00000000ffff1000 [    4K] r--/r-- SM=PRV  process-only VM submap

Does anyone have any ideas on how to tackle this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.