<?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>Tue, 31 Jan 2012 02:25:16 +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>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>

