Mac for Hackers: How to Set Up Homebrew to Install & Update Open-Source Tools
A Linux/Unix environment without a package manager means a lot of extra work on your part. So without a good package manager, any additions to the system will need to be compiled from source. Yes, you already have the Mac App Store on your macOS hacking computer, but most of the open-source pentesting tools we rely on as hackers and security professionals are not available there.
It costs developers money to submit to the Mac App Store, and the code must be reviewed and accepted by Apple before it can be published. For some developers, this is too much of a hassle. This means we need another package manager to pull down free software such as Hydra, Aircrack-ng, GNU Coreutils (if you're like me and prefer Linux flags to Unix flags), or some dependencies for tools located on GitHub.
If I wanted to compile Aircrack-ng from source on my macOS machine, first I would need to verify that I had all of the dependencies, and that they were in the correct locations and functioning properly. I would then need to download the source for Aircrack-ng, configure it, and begin the compilation process, resolving any issues I ran into on the way.
A few months down the road, I might need to update the compiled software. To do so, I would have to remove the software from my machine, check to see which libraries are required in the latest version, and repeat the process over again—then check some to see if I have dependencies that are no longer in use (hopefully I've been tracking what I've installed in some sort of list).
If I had a package manager, all of this work would be handled for me.
There are a few package managers for macOS such as Macports and Nix, but I prefer Homebrew. The syntax is very straightforward, it's fast, the packages are well-maintained and up to date, and it leverages more of macOS's default libraries instead of redundantly installing new ones. Also, everything is owned by a regular user, meaning there is no need to use sudo. But best of all, Homebrew is clean, with everything kept in its own sandbox in /usr/local.
With this pacakage manager, the source or binaries are pulled down with their requirements met. Homebrew then keeps track of what has been installed, what is using it, and where it is located. It also keeps track of configuration information and makes the whole process of maintaining open-source software on your Apple product a piece of cake.
I will generally search for a package in Homebrew before cloning it from GitHub and compiling from source. If the package is missing in Homebrew, it's worth considering creating a brew formula for it. However, there aren't a lot of pentesting tools in the Homebrew repos, but there are lots of libraries and general purpose open-source tools which can come in handy.
Our first step is to get Homebrew from the site http://brew.sh. Before installing, though, we should review the source on GitHub. Generally, I don't like piping a script off the internet, but I trust their repository. It's on GitHub, I can read the source code, and the author doesn't have access to the internals of the GitHub servers to muck about with the timing and detect cURL pipes.
Open up iTerm on your Mac and execute this command:
- /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
You will be prompted for your password.
If you search the brew repository often enough, you will want an access token since the GitHub API rate limits queries to their servers.
In order to set your token, you will need to follow the link presented to you in the terminal. You will need to login to GitHub, or create a new account—if you've been following this series, you already have a GitHub account. Next, ensure the boxes are all unchecked—we only need the token, Homebrew doesn't need access to anything in our GitHub account.
At the bottom of the page, click on "Generate Token." You now have a token, so let's get it working with Homebrew.
We're going to have to edit a few Bash files in your home directory to get the token working, both the .bashrc and .bash_profile files. The .bashrc file is the configuration file for Bash, the default shell on macOS. The .bash_profile file is a personal initialization file executed for login shells (SSH or from the console) whereas .bashrc is the individual per-interactive-shell startup file.
While we could just edit the .bash_profile and that's it, I prefer to keep all of my Bash settings in .bashrc, which ensures that no matter how the shell is spawned, I have my preferences set. Setting my .bash_profile to source my .bashrc means that if I login from SSH, I will have the same environment as my local terminal.
I'll be using Vim for this, but if you aren't familiar with Vim, you can use the plain text editor of your choice to do it manually, or just open it with your default text editor using open ~/filename in the terminal.
To use Vim, open a new iTerm window, or type in cd to get to your home directory, then type in the following command. (If you don't have a .bashrc file yet, you can create one first with touch .bashrc).
- vim .bashrc
Now add your token to the bottom of the .bashrc file in insert mode. Use either a to append after the cursor, or i to insert before the cursor.
- export HOMEBREW_GITHUB_API_TOKEN="YOUR_TOKEN_HERE"
Hit escape to exit insert mode back into normal mode, then type :wq to save the file. The colon starts the command, and wq means write and quit.
Next, let's edit your .bash_profile. Type in the following command. (If you don't have a .bash_profile yet, you can create one first with touch .bash_profile).
- vim .bash_profile
Now, at the bottom of the .bash_profile file, add the following. Again, use either a to append after the cursor, or i to insert before the cursor.
- source .bashrc
Again, hit escape and enter :wq to save and exit the file.
Next, let's tell Bash to execute the commands in the .bashrc file as if they were executed on the command line.
- source ~/.bashrc
And we're all done with this part. Let's move on to working with Homebrew!
Homebrew has a similar feel to Linux package managers, and it's fairly easy to run. First we'll use help to see some quick info on getting started.
- brew help
A very simple package manager indeed. If you're already familiar with APT, you may see a similarity.
Now let's go through the steps for installing a package. I have selected the popular brute-force tool Hydra. First, we'll search to ensure Hydra is available. The command is:
- brew search hydra
Next, we'll get info on Hydra, to confirm that it is what we're looking for, using the command:
- brew info hydra
Lastly, we'll install Hydra. I chose to install the GUI as well:
- brew install hydra --with-gtk+
When Homebrew finishes, we'll have a version of Hydra on our macOS machine.
As you can tell from the help page, if you need to update, uninstall, etc. any tools, it's pretty intuitive.
Installing and maintaining packages with Homebrew is a piece of cake, especially if you have previous Linux experience. When I am looking to install an open-source application, I almost always check Homebrew first to save time. If the package isn't available in Homebrew, then it's off to GitHub, but between the two, most of your software needs should be met.