Warning: Constant ABSPATH already defined in /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php on line 24 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 Warning: Cannot modify header information - headers already sent by (output started at /customers/0/c/e/ogenstad.net/httpd.www/wp-config.php:24) in /customers/0/c/e/ogenstad.net/httpd.www/wp-includes/rest-api/class-wp-rest-server.php on line 1673 {"id":65,"date":"2006-10-04T20:10:46","date_gmt":"2006-10-04T19:10:46","guid":{"rendered":"http:\/\/ogenstad.net\/2006\/10\/04\/breaking-out-of-jail-with-microsoft-word\/"},"modified":"2006-10-04T20:10:46","modified_gmt":"2006-10-04T19:10:46","slug":"breaking-out-of-jail-with-microsoft-word","status":"publish","type":"post","link":"https:\/\/ogenstad.net\/2006\/10\/04\/breaking-out-of-jail-with-microsoft-word\/","title":{"rendered":"Breaking out of Jail with Microsoft Word"},"content":{"rendered":"

“Can you really<\/em> break out of jail with just Microsoft Word, or are you just pulling my leg?”<\/p>\n

No I would never<\/em> do that, Word is really powerful, it can take you places!<\/p>\n

Intended audience<\/strong><\/p>\n

If you are a convicted felon and reading this from prison; sorry but you are not the target audience. However, don’t despair: there’s something in here for you too!<\/p>\n

This article is for people who care about security. If you don’t fall into this second category you really are in the first group, i.e. not the intended audience. When I’m talking about “jail”, I’m referring to a restricted Windows environment. What I mean with this is a locked down Terminal Services session, or a public Windows XP box in “kiosk” mode.<\/p>\n

Administrators who have locked down their terminal services sessions care about what the users can do on the machine. They only want users to run the programs that are approved by the security policy. This means no cmd.exe, no freecell and no poking around in the system.<\/p>\n

So why isn’t this article for people who haven’t locked down their environments? Because of the good ol’ “Security is a Weakest Link Problem”, meaning if you haven’t even tried to lock down your environment, you have other issues to take care of before you read this article.<\/p>\n

Operation Lockdown<\/strong><\/p>\n

For the environment that I have set up to test this, I’ve installed a Windows Server 2003 machine and set it up to run Terminal Services. I’ve also installed Office, the goal here is to only allow a user to run Microsoft Word and nothing else.
\nThe user’s desktop is then redirected to a share where the user doesn’t have write access. The desktop contains one item; a link to Word. Logging in to the terminal server, everything looks good; the user can basically run Word and change his password.<\/p>\n

Apart from the above setting the rest of the GPO settings are located at the end of this article. It’s basically a few lockdown settings and a software restriction policy defaulting to “disallowed”, with an additional path rule allowing Microsoft Word.<\/p>\n

Given this configuration let’s see what we can do.<\/p>\n

Where does Word enter into it?<\/strong><\/p>\n

I guess the title says it all; Yes, Word is the application we are going to use to break out of the locked down jail. If you’ve tried my SYDI<\/a> project, you’ve probably realized that I like Office automation. This concept would work in any Office application but I have chosen Word. If you don’t have Office installed in your locked down environment, I still think you should continue reading. Hopefully you’ll enjoy the article anyway.<\/p>\n

What I’ve done is to write a Word macro which allows me to run shell commands or code of my choice.<\/p>\n

You might object and say, “So? I have Macro Security set to Very High. You’d have to digitally sign the macro first.”<\/p>\n

I most certainly would not! The Macro setting in the Office suite is to protect you against an entirely different<\/em> threat. In that scenario the threat is that an attacker can send us (or our users) a Word document with an evil macro inside which is run when we open the document.<\/p>\n

This setting does not disallow developers to create macros, run them while developing and then sign them when they are in a state where they can be shared with others.<\/p>\n

What does this mean for us? It means that we cannot use a pre-created<\/em> Word document which contains<\/em> the macro. We have to create<\/em> the macro ourselves on the fly each time we want to run macros. See the difference? The security setting in Word doesn’t disallow you to create and run your own macros. If we would save the document and open it again, then<\/em> the security setting would kick in and disallow the macro. If we had signed it before saving then we would be allowed to run it.<\/p>\n

In order to prevent this we would have to have a setting in office called “Don’t Allow Users to Develop Macros”.<\/p>\n

VBA-PrisonBreak<\/strong><\/p>\n

I named the macro I created VBA-PrisonBreak (I must have been watching too much TV<\/a>). You can download the VBA-PrisonBreak from here<\/a>. To test it in your environment open up Microsoft Word and press [Alt]+[F11] to enter the VBA editor. Double click on “This Document”. This will give you a code Window on the right side. Copy the contents from the VBA-PrisonBreak.txt file and paste it in the Window. The file contains different subroutines, or programs if you will.<\/p>\n

To run one of these programs you set the cursor somewhere after the Sub [Program] and before the End statement. To execute the program you press Play or [F5].<\/p>\n

To give it a try and test something harmless, start the routine called RunCommand() and enter.<\/p>\n

ping 127.0.0.1<\/p>\n

When the command is completed, go back to the Word document. If everything worked out as it should you will see which command you tried to run and the results.<\/p>\n

Let’s see if we can poke around a bit:<\/p>\n

cmd \/c dir<\/p>\n

Hm, this won’t work unless we have command prompt scripting allowed. Then again we can create scripts of our own and run them instead. Another sub routine of VBA-PrisonBreak is called ListFilesinFolder() and takes a directory as an argument. This is just a simple example but a lot can be accomplished by scripting.<\/p>\n

So we haven’t done anything terribly exiting. The commands we can run by using the RunCommand() routine depend on a few factors, we will look at the Software Restriction Policies. We know we can run Word, but what about the default additional ones? Among others this includes commands under c:\\windows and c:\\windows\\system32.<\/p>\n

“net view” and “net user \/domain” might be interesting and could give us some information. After looking around we might come to the conclusion that we want to run a command which isn’t allowed by the software restriction policy.<\/p>\n

As a normal unprivileged user we will not have write access to c:\\windows or c:\\windows\\system32, so we can’t place any files in those directories. But in our configuration we have write access to c:\\windows\\temp, and even better CREATOR_OWNER has Full Control. If we can place a file in this directory we will be able to run it with RunCommand() by entering:<\/p>\n

C:\\windows\\temp\\mycommand.exe<\/p>\n

Now how would we place a file there? We can’t just use copy since cmd.exe is blocked. We might be able to use xcopy or tftp. If tftp isn’t available we might still be able to expand (.exe) it from c:\\windows\\i386\\tftp.ex_. There is also a function in VBA-PrisonBreak which lets you download files through http (HTTPDownload()). It takes source url and target file as arguments.<\/p>\n

From here we can download netcat and gain shell access to the server. Note that cmd.exe is still restricted; unfortunately an attacker can use another file or edit a version of cmd.exe. This edited shell file can then be placed under c:\\windows\\temp.<\/p>\n

It is important to realize that the credentials used in this shell will be that of the unprivileged user account.<\/p>\n

How to Protect Yourself<\/strong><\/p>\n

It’s really easy to protect yourself against this feature<\/em>, remember this is not a vulnerability or an exploit in Windows or Word. We are trying to remove features from Windows (such as disabling cmd.exe), since the features we want to remove can be used in harmful ways.<\/p>\n

When you install Office you don’t have to install all the features, one of these features is vba (Visual Basic for Applications). Without vba this prison break wouldn’t be possible (in the way I’ve described). You can find information about this in the Office Security pages<\/a>. However as you can see on the page, Microsoft recommends that you don’t disable vba and also lists the reasons why, so you have to decide for yourself. This page also mentions the “very high” macro setting, but remember what I said before: this setting doesn’t apply in this context.<\/p>\n

Another option is to disable vba through group policies. You can do this by using the .ADM files for Microsoft Office. This is only an option if you won’t be using any macros.<\/p>\n

Even if your business requires you to be able to run vba, I have three words for you; Defense in Depth. If one part of the system fails, this shouldn’t mean that the defense has been breached.<\/p>\n

The default Software Restriction Policies obviously didn’t restrict as much as we would have hoped. We will definitely want to explore these settings. If a user has write (and execute) permissions to any location within a SRP Path rule that user can run whatever he wants. You could set up deny rules for these directories or you could tighten the NTFS permissions and remove execute permissions.<\/p>\n

It would be nice if the default policies provided some sort of protection, at least since Microsoft has written in several places that they shouldn’t be changed unless you are an “advanced user”. But the Software Restriction Policies can be fixed and in this case that’s not really the problem. Since we can write VBA code we have a lot of options. VBA-PrisonBreak is mostly a proof of concept; it doesn’t really do much.<\/p>\n

On a network level if we take the example with tftp, there’s no reason why this should be allowed to or from your machine. If we were only supposed to run Word, allowing http doesn’t make much sense either.<\/p>\n

Earlier on I said it would be good if there was a setting in Office called “Don’t Allow Users to Develop Macros”. Unfortunately, as far as I know this kind of setting doesn’t exist. However we can create one of our own. The VBA editor is using vbe6ext.olb which by default is located in this location:<\/p>\n

C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB<\/p>\n

If we deny the execute permission (NTFS) to this file for the users we want to lock down they won’t be able to launch the vba editor, but will be able to run signed macros. A little word of caution; I don’t know if there are any other issues you should be aware removing execute permissions to this file, but I don’t think there is.<\/p>\n

The problem with scripting<\/strong><\/p>\n

The macro RunCommand() creates an object called Wscript.Shell. Is this required by your business? If you remove the NTFS execute permission (for the terminal services users) to the file c:\\windows\\system32\\scrrun.dll, they won’t be able to use the RunCommand().<\/p>\n

Is there another library an attacker can use if you deny the features from Wscript.Shell? Disabling scrrun.dll would still allow a malicious user to download files with HTTPDownload() it uses an object called Microsoft.XMLHTTP. There are a lot of scriptable libraries in Windows. Chasing them all down is hard, and it wouldn’t solve the whole problem.<\/p>\n

As I said above the problem isn’t related to Software Restriction Policies. Even if the SRPs are set up in a way that would prohibit the user to run a non approved executable, that user would still be able to write vba code. If vba code isn’t scary enough, an attacker could pre compile a dll with a com interface and load the functions from that dll from vba.<\/p>\n

The Weakest Link<\/strong><\/p>\n

To find the weakest link you might have to think outside the box<\/a>. But that is the fun part of security. \ud83d\ude42<\/p>\n

Ask Jesper<\/a> what he thinks of Security Guides and he will tell you that they won’t make you secure (or in Jesper-\/Stevespeak<\/a> you won’t be Protected).<\/p>\n

I think security guides are great but still they are just guides<\/em>, not a blue print for a secure network. Most guides are fairly generic and you have to decide what parts of it to use in your environment. The security of a network isn’t as strong as the sum of all security settings; it’s as strong as the weakest link. When it comes to SRPs, a guide can’t tell you which applications makes sense in you environment.<\/p>\n

Summary<\/strong><\/p>\n

VBA-PrisonBreak in itself won’t allow a malicious user to take control of your server. That user would have to escalate his privileges first, unless the attacker was a “power user” or an administrator to start with.<\/p>\n

VBA-PrisonBreak is just a way to gain another foot-hold in your environment. When trying to protect our networks we don’t want to give any more ground than we have to.<\/p>\n

If you are a criminal and have been wondering through this article how Word can help you to get out of a real prison: It’s quite easy really; just open up Word and start writing “Dear Congressman”, press [enter] twice and write your plea for pardon. If you’re really lucky a magical paper clip will come to your aid!<\/p>\n

Note:<\/strong> We did not lock down the Help Menu in Word, but since we didn’t use it to gain access that way I chose to ignore that and a few other threats in this article.<\/p>\n

Credits<\/strong><\/p>\n

I, Patrick Ogenstad<\/a>, work at Netsafe<\/a>. I would like to thank John Laerum from Cornerstone<\/a> for providing feedback on this article.<\/p>\n

If you liked this article, please stick around! You can subscribe<\/a> to the “feed”<\/a>, or get updates by email<\/a>. Also be sure to check out my security fiction stories<\/a>.<\/p>\n

If you have any feedback concerning this, please post a comment or contact me<\/a>.<\/p>\n

Appendix<\/strong><\/p>\n

The group policy settings used in the test environment:<\/p>\n