<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Herein lies madness</title>
    <link rel="alternate" type="text/html" href="http://www.core.dk/~lp/" />
    <link rel="self" type="application/atom+xml" href="http://www.core.dk/~lp/atom.xml" />
    <id>tag:www.core.dk,2009-05-20:/~lp//1</id>
    <updated>2009-12-17T18:17:58Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.25</generator>

<entry>
    <title>Mac OS X code injection</title>
    <link rel="alternate" type="text/html" href="http://www.core.dk/~lp/2009/08/mac-os-x-code-injection.html" />
    <id>tag:www.core.dk,2009:/~lp//1.7</id>

    <published>2009-08-29T18:24:08Z</published>
    <updated>2009-12-17T18:17:58Z</updated>

    <summary> Code injection on Mac OS X is possible via mach_star package which consists of two functions, mach_override (replace or extend existing functions at runtime) and mach_inject (dynamically load your code into a running process). An Intel port is available...</summary>
    <author>
        <name>Lars Pedersen</name>
        
    </author>
    
        <category term="Mac OS X" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-US" xml:base="http://www.core.dk/~lp/">
        <![CDATA[
<p>
Code injection on Mac OS X is possible via <a target="_blank" href="http://rentzsch.com/mach_star/">mach_star</a> package which consists of two functions,  <code>mach_override</code> (replace or extend existing functions at runtime) and <code>mach_inject</code> (dynamically load your code into a running process). An Intel port is available <a target="_blank" href="http://guiheneuf.org/mach%20inject%20for%20intel.html">here</a>.
</p>

<h3>mach_star</h3>

<p>
While the example provided in the mach_star package worked out of the box (if I recall correctly), I wanted to create a project that didn't rely on XCode and the Carbon API. In order to get that to work, I had to add <code>MAP_SHARED</code> to the <code>mmap</code> call in <code>mach_inject.c</code>:

<pre>
char * fileImage = mmap (NULL, mapSize, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
</pre>

<h3>Loader bundle</h3>

<p>
Following the layout of the provided example, the idea is to inject just enough code to load a bundle in the targeted running application. Once the injected bundle is loaded, the environment should then be stable and we can proceed to override existing functions in the application. So we create a loader bundle <code>loader.dylib</code> which consists of:
</p>

<pre>
void *pthread_entry(void *patch_bundle)
{
	void *bundle = dlopen((char *)patch_bundle, RTLD_NOW);
	if (!bundle)
		fprintf(stderr, "Could not load patch bundle: %s\n", dlerror());
	return 0;
}

void inject_entry(ptrdiff_t offset, void *param, size_t psize, void *dummy)
{
	extern void __pthread_set_self(void *);

	__pthread_set_self(dummy);

	pthread_attr_t attr;
	pthread_attr_init(&attr); 
	
	int policy;
	pthread_attr_getschedpolicy(&attr, &policy);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
	
	struct sched_param sched;
	sched.sched_priority = sched_get_priority_max(policy);
	pthread_attr_setschedparam(&attr, &sched);

	pthread_t thread;
	pthread_create(&thread, &attr,
			(void * (*)(void *))((long)pthread_entry),
			(void *)param);
	pthread_attr_destroy(&attr);
	
	thread_suspend(mach_thread_self());
}
</pre>

<p>
This <code>loader.dylib</code> bundle is then loaded by my application and injected into the targeted application using <code>mach_inject</code> with the name of the bundle we wish load inside the targeted application as a parameter. 
</p>
 
<h3>Patch bundle</h3>

<p>
A new bundle <code>patch.dylib</code> that will be loaded inside the targeted application is created, which in part consists of:
</p>

<pre>
void install(void) __attribute__ ((constructor));

void install()
{
	// .. 
}
</pre>

<p>
The <code>constructor</code> function attribute ensures that the <code>install</code> function is called automatically when <code>patch.dylib</code> is loaded. We now have a sane environment and can proceed to wreck chaos in the targeted application.  Enter <code>mach_override</code>:
</p>

<pre>
typedef void (*override_fn)(void);
override_fn orig_fn;

void install()
{
	if (mach_override("_override_fn", NULL, patch_fn, (void **) &orig_fn) != 0)
		fprintf(stderr, "mach_override failed\n");
}
</pre>

<p>
In the above piece of code, we override the function <code>override_fn</code> with our own <code>patch_fn</code> function. A pointer to the original <code>override_fn</code> is stored in the <code>orig_fn</code> variable. And we are done.
</p>]]>
        
    </content>
</entry>

<entry>
    <title>No news ...</title>
    <link rel="alternate" type="text/html" href="http://www.core.dk/~lp/2009/08/no-news.html" />
    <id>tag:www.core.dk,2009:/~lp//1.8</id>

    <published>2009-08-25T11:36:51Z</published>
    <updated>2009-08-25T11:37:20Z</updated>

    <summary>.. is good news...</summary>
    <author>
        <name>Lars Pedersen</name>
        
    </author>
    
    
    <content type="html" xml:lang="en-US" xml:base="http://www.core.dk/~lp/">
        .. is good news
        
    </content>
</entry>

</feed>

