<?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>edgeblog &#187; Scripting</title>
	<atom:link href="http://www.edgeblog.net/category/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edgeblog.net</link>
	<description>Notes from the edge</description>
	<lastBuildDate>Mon, 30 Jan 2012 19:27:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>DNSCMD Kung Fu</title>
		<link>http://www.edgeblog.net/2008/dnscmd-kung-fu/</link>
		<comments>http://www.edgeblog.net/2008/dnscmd-kung-fu/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 05:17:47 +0000</pubDate>
		<dc:creator>bill</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Systems]]></category>
		<category><![CDATA[dns]]></category>
		<category><![CDATA[dnscmd]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.edgeblog.net/?p=113</guid>
		<description><![CDATA[     <link rel="alternate" type="application/atom+xml" title="edgeblog Category: Scripting" href="http://www.edgeblog.net/category/scripting/feed/" />
     <link rel="alternate" type="application/atom+xml" title="edgeblog Category: Systems" href="http://www.edgeblog.net/category/systems/feed/" />
<p>In <a href="http://www.edgeblog.net/2008/top-5-ways-windows-is-better-than-unix-or-linux/">a previous article</a>, I extolled the virtues of DNS on Windows. In particular, I love the scripting interface that <a href="http://technet.microsoft.com/en-us/library/cc756116.aspx" target="_blank">DNSCMD</a> provides. In that article, I claimed: &#8220;Need to create 500 host records, both forward and reverse, in different domains and subnets? DNSCMD can do it with a 1-line script&#8230; there is no *nix alternative that is this simple or powerful.&#8221; Well, enough people have been bugging me to provide it so here is is:</p>
<p id="code">REM### Copyright 2008 William L. Dougherty<br />
REM### Script for bulk uploading DNS records into Windows DNS<br />
REM### Script requires hosts.txt file in format:  FQDN,IPADDR  1 host per line<br />
REM### Example:<br />
REM######host1.domain.com,192.168.1.1<br />
REM######host2.domain.com,192.168.1.2<br />
for /F &#8220;tokens=1-7 delims=,. &#8221; %%a in (hosts.txt) do dnscmd /recordadd %%b.%%c %%a a %%d.%%e.%%f.%%g &amp;&amp; dnscmd /recordadd %%f.%%e.%%d.in-addr.arpa %%g ptr %%a.%%b.%%c</p>
<p>Just put it into a batch file and you are good to go. Simple, right? Well, maybe I should explain. <!--more-->The basic concept here is we use a <strong>for</strong> loop to iterate through a text file, parsing the components into variables. We then feed these variables into <strong>dnscmd</strong> to create the A (host) record, and feed them a second time into dnscmd to create the PTR record. I <a href="http://www.edgeblog.net/2007/lockdown-windows-&lt;/p&gt;&lt;p&gt;2003-xp-with-simple-scripts/">tend to use for loops in a lot of my scripts</a>, because they are an easy way to execute repetitive commands in batch files.</p>
<p>The above script takes as input a file named hosts.txt that must be located in the same directory as the script being executed. This files should be in the format <strong>FQDN,IPADDRESS</strong>. This script will work with either Active Directory Integrated, or Primary zones, but it does require the zones already exist on the server.</p>
<p>So what do you do if the zones don&#8217;t already exist? Another simple script will take care of this. Just iterate through the same list, and create the zone files first. If the zone already exists, then Windows will throw an error to the console, but there are no other bad consequences, so running this every time is fairly benign. You just need to know in advance whether you want Primary or Active Directory Integrated zones. Here are the scripts for each:</p>
<p id="code">Active Directory Integrated:<br />
for /F &#8220;tokens=1-7 delims=,. &#8221; %%a in (hosts.txt) do dnscmd /zoneadd %%b.%%c /dsprimary &amp;&amp; dnscmd /zoneadd %%f.%%e.%%d.in-addr.arpa /dsprimary<br />
Primary:<br />
for /F &#8220;tokens=1-7 delims=,. &#8221; %%a in (hosts.txt) do dnscmd /zoneadd %%b.%%c /primary /file %%b.%%c.dns &amp;&amp; dnscmd /zoneadd %%f.%%e.%%d.in-addr.arpa /primary /file %%f.%%e.%%d.in-addr.arpa.dns</p>
<p>As one of my co-workers is fond of saying, &#8220;it&#8217;s too easy!&#8221; DNSCMD has many, many other options. Two of my favorites are /enumzones and /enumrecords. Using these two options, you can easily dump all your DNS records into text files. I&#8217;ve used this to back up my records every night, and I&#8217;ve also used this for change control. Dump the files nightly and diff them to the previous night, and you&#8217;ll know what changed. This script creates a seperate text record for each of your zones:</p>
<p id="code">dnscmd /enumzones &gt; zones.txt<br />
for /f %%a in (zones.txt) do dnscmd /enumrecords %%a @ &gt; %%a.txt</p>
<p><a href="http://technet.microsoft.com/en-us/library/cc756116.aspx" target="_self">DNSCMD</a> gives you a scriptable interface into anything/everything you need to administrate zones and records on Windows. Learn to love it, and you&#8217;ll never go back to Bind.</p>
]]></description>
		<wfw:commentRss>http://www.edgeblog.net/2008/dnscmd-kung-fu/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Lockdown Windows 2003 &amp; XP with Simple Scripts</title>
		<link>http://www.edgeblog.net/2007/lockdown-windows-2003-xp-with-simple-scripts/</link>
		<comments>http://www.edgeblog.net/2007/lockdown-windows-2003-xp-with-simple-scripts/#comments</comments>
		<pubDate>Sat, 17 Mar 2007 14:00:07 +0000</pubDate>
		<dc:creator>bill</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Systems]]></category>

		<guid isPermaLink="false">http://www.edgeblog.net/2007/lockdown-windows-2003-xp-with-simple-scripts/</guid>
		<description><![CDATA[<p><a href="http://www.amazon.com/gp/product/0735622442?ie=UTF8&amp;tag=bdog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0735622442" target="_blank" title="Windows Advanced Scripting"><img src="http://www.edgeblog.net/wp-content/uploads/2007/06/script.thumbnail.jpg" title="Windows Advanced Scripting" alt="Windows Advanced Scripting" align="left" /></a>Now that DST 2007 is over, we are going to start a series of articles on securing systems and networks. I have built a lot of systems for various companies over the years. The challenge is to create repeatable processes that work in a variety of operating environments. Having a strong scripting toolkit can make all the difference, especially when you are under deadline.</p>
<p>The first script in the series is a Windows Services lockdown script for Windows XP &amp; 2003. Disabling services is generally a good idea to reduce the threat profile of your computer, and to improve its performance. Every security guide out there tells you to disable unnecessary services. A few of them also give some guidance as to which services are unnecessary. Few of them tell you how to disable them consistently.</p>
<p>There are three ways to disable services: 1) Use the Services MMC GUI. This is a time consuming process and is prone to mistakes. 2) Use Group Policy. This works well for environments that use Group Policy, but is harder to implement for stand-alone servers, such as web servers. 3) Use the <strong>sc.exe</strong> command line utility.</p>
<p>If you do not know the <strong>sc</strong> command, learn it! sc is a powerful utility for controlling services on local or remote hosts. sc will let you configure how services start, change the user account and password they run under, and start/stop/pause the services. The basic syntax of sc is:</p>
<p id="code">sc &lt;server&gt; [command] [service name] &lt;option1&gt; &lt;option2&gt;</p>
<p>We are going to use 2 different sc commands in our service lockdown script: config &amp; stop. These should be self explanatory, but config will allow us to disable the service, and stop will stop the service. To make this work, we need three files: 1) The script batch file; 2) a list of servers by name called hosts.txt; 3) a list of services we want to disable called services.txt. The two text files must be in the same directory as the batch file. The code is fairly simple:<!--more--></p>
<p id="code">REM***(c) 2007 William L. Dougherty<br />
REM***Script created by Bill Dougherty to disable services on Windows 2003 &amp; XP<br />
for /f %%a in (hosts.txt) do call :serviceconfig %%a<br />
goto :eom<br />
:serviceconfig<br />
for /f %%b in (services.txt) do (sc \\%1 config %%b start= disabled)<br />
for /f %%c in (services.txt) do (sc \\%1 stop %%c)<br />
:eom</p>
<p>Pretty simple, huh? We loop through a list of servers, and then for each server, we loop through a list of services. The service list must be the service name and not the display name of the services. Some service names are less than intuitive, such as the service name for IPSEC Services is PolicyAgent. You can get the service name from the services MMC by clicking the service and looking at its properties, or from the command line using sc query. Either way, once you build your service list the first time, you&#8217;ll rarely need to revist this.</p>
<p>This script requires you to have administrator access on the target host. This works great in a domain environment, but what if you are dealing with stand-alone servers? You can still use this script so long as the user credentials you are logged in as have admin access on the target. If not, the you&#8217;ll need to log into each server and run the script locally. If this is the case, you only need the script file and the list of services. You should modify the script like this:</p>
<p id="code">for /f %%b in (services.txt) do sc config %%b start= disabled<br />
for /f %%c in (services.txt) do sc stop %%c<br />
:eom</p>
<p>This script works with both Windows XP and Windows 2003, although the two platforms have different services. There are several good sources for figuring out which services are unnecessary. I highly recommend the TechRepublic guides: <a href="http://techrepublic.com.com/i/tr/downloads/home/windows_xp_services_that_can_be_disabled.pdf">XP Services Guide</a> and <a href="http://downloads.techrepublic.com.com/download.aspx?docid=172015">2003 Services Guide</a>. Over the years, I have developed my own lists. I highly recommend you spend the time to research these services, and test the impact of disabling them BEFORE you run this script against your production network!</p>
<p>Windows 2003 services that can be disable:</p>
<p id="code">Alerter<br />
AppMgmt<br />
ClipSrv<br />
TrkWks<br />
TrkSvr<br />
MSDTC<br />
ERSvc<br />
helpsvc<br />
HidServ<br />
ImapiService<br />
IsmServ<br />
LicenseService<br />
Messenger<br />
mnmsrvc<br />
NetDDE<br />
NetDDEdsdm<br />
nla<br />
NtLmSsp<br />
WmdmPmSM<br />
appmgr<br />
RemoteAccess<br />
SCardSvr<br />
SENS<br />
LmHosts<br />
TapiSrv<br />
TlntSvr<br />
Tssdis<br />
Themes<br />
uPS<br />
uploadmgr<br />
WebClient<br />
AudioSrv<br />
stisvc<br />
WZCSVC</p>
<p>Windows XP services that can be disabled:</p>
<p id="code">Alerter<br />
AppMgmt<br />
ClipSrv<br />
TrkWks<br />
MSDTC<br />
ERSvc<br />
FastUserSwitchingCompatibility<br />
helpsvc<br />
HidServ<br />
CiSvc<br />
Messenger<br />
mnmsrvc<br />
NetDDE<br />
NetDDEdsdm<br />
nla<br />
NtLmSsp<br />
SysmonLog<br />
WmdmPmSM<br />
RSVP<br />
RemoteAccess<br />
SCardSvr<br />
SSDPSRV<br />
LmHosts<br />
TapiSrv<br />
TlntSvr<br />
Themes<br />
uPS<br />
upnphost<br />
WZCSVC</p>
<p>I hope you find this script useful. Check back often for additional scripts in the series.</p>
<p>-Bill</p>
]]></description>
		<wfw:commentRss>http://www.edgeblog.net/2007/lockdown-windows-2003-xp-with-simple-scripts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Clean Up After Terminated Windows Administrators</title>
		<link>http://www.edgeblog.net/2006/clean-up-after-terminated-windows-administrators/</link>
		<comments>http://www.edgeblog.net/2006/clean-up-after-terminated-windows-administrators/#comments</comments>
		<pubDate>Fri, 01 Dec 2006 09:26:49 +0000</pubDate>
		<dc:creator>bill</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.edgeblog.net/2006/clean-up-after-terminated-windows-administrators/</guid>
		<description><![CDATA[<p><img align="left" src="http://www.edgeblog.net/wp-content/uploads/2006/12/win2k3.thumbnail.jpg" alt="win2k3.jpg" title="win2k3.jpg" id="image56" />Losing an administrator is always a painful process. Even the best administrators usually forget to document something. The worst admins document nothing, create up multiple backdoor accounts, and install services to run under their own credentials. It is important to immediately check your servers when an admin leaves for several reasons: Disgruntled admins may leave backdoors in your system that they will later use to attack you; Disabling the admin&#8217;s account may cause services to stop running; Scripts may be scheduled to run that will grant the admin access weeks or months later.</p>
<p>Fortunately, it is possible to perform a rapid clean up if you follow a simple process, and use tools to help. This process is specific to the platform the administrator supported. The process for cleaning up after a Windows administrator is as follows:</p>
<ol>
<li>Create a list of all servers in your environment. If you aren&#8217;t sure, check DNS and Active Directory</li>
<li>Search Active Directory for all users with privileged (admin) group memberships</li>
<li>Search every server for services that run under domain or local accounts instead of LocalSystem or NT</li>
<li>Search every server for scheduled tasks that run under domain or local accounts</li>
<li>Change the password on every privileged user account. Assume that the old admin could have had access to every account at some point.</li>
<li>Change the password on every service and scheduled task to match the new passwords in step 5.</li>
<li>Change any service or scheduled task that runs under the old admin&#8217;s account to run under a new service account</li>
<li>Review any scheduled tasks that are scripts, to make sure you know what they do. A clever admin could bury a script to recreate his admin account inside of another script.</li>
<li>Disable the old admin account</li>
</ol>
<p>There are many good commercial tools available for searching servers for service accounts and scheduled task accounts, but I&#8217;m a big believer in using simple scripts where possible to get the job done. If you want a commercial product to help, check out:</p>
<ul>
<li><a target="_blank" href="http://www.scriptlogic.com/products/serviceexplorer/">ScriptLogic Service Explorer</a></li>
<li><a target="_blank" href="http://www.liebsoft.com/index.cfm/products/sam">Service Account Manager</a></li>
</ul>
<p>If like me, you hate to spend good money for tools that duplicate the built-in power of Windows, then these scripts are for you:<!--more--></p>
<p>Code:</p>
<div id="code">REM ***Script Created by William Dougherty<br />
REM ***Script used to check for services and scheduled tasks that run under domain accounts<br />
REM ***Script requires Windows 2003 Resource Kit (global.exe &amp; findstr.exe). Must be run with domain admin permissions<br />
REM ***Script requires GnuWin32 (gsar) found at http://gnuwin32.sourceforge.net/packages/gsar.htm<br />
REM ***Script requires servers.txt file located in same directory. servers.txt should be a list of server names to be checked<br />
REM ***The First Step is to check all the privileged admin groups in the domain<br />
set /P domainname=Please enter domain name:<br />
global.exe &#8220;DnsAdmins&#8221; %domainname% &gt; checktheseaccounts.txt<br />
global.exe &#8220;Domain Admins&#8221; %domainname% &gt;&gt; checktheseaccounts.txt<br />
global.exe &#8220;Enterprise Admins&#8221; %domainname% &gt;&gt; checktheseaccounts.txt<br />
global.exe &#8220;Schema Admins&#8221; %domainname% &gt;&gt; checktheseaccounts.txt<br />
REM ***First step is to generate a list of services running on each server<br />
for /f %%a in (servers.txt) do (sc \\%%a query &gt; %%a.tx2)<br />
dir /B *.tx2 &gt; serverlist.tx2<br />
findstr /f:serverlist.tx2 /c:SERVICE_NAME &gt; servicelist.tx2<br />
REM ***Now we need to remove the leading space in front of the service name using GSAR, to deal with multi-word service names<br />
gsar -s&#8221;:: &#8221; -r:: -o servicelist.tx2   </p>
<p>REM ***Once we have our service lists, we call a subroutine to do an advanced query which will tell us what service accounts the service uses to run.<br />
for /f &#8220;tokens=1-4 delims=.:&#8221; %%b in (servicelist.tx2) do set server=%%b&amp;&amp;set service=%%e&amp;&amp;call :ServiceScan</p>
<p>REM ***Now that we have the accounts, we filter out the services that run as LocalSystem or NT<br />
for /f &#8220;tokens=1-3 delims=, &#8221; %%m in (services.tx2) do (if not %%o==LocalSystem echo %%m,%%n,%%o &gt;&gt; 1stcheck.tx2)<br />
for /f &#8220;tokens=1-3 delims=, &#8221; %%m in (1stcheck.tx2) do (if not %%o==NT echo %%m,%%n,%%o &gt;&gt; checktheseservices.txt)</p>
<p>REM ***The final step is to go back and check each server for scheduled tasks<br />
for /f %%a in (servers.txt) do (schtasks /query /v /FO CSV /NH /s %%a &gt;&gt; checkthesetasks.txt)<br />
del /F *.tx2<br />
goto :EOM</p>
<p>:ServiceScan<br />
sc \\%server% qc &#8220;%service%&#8221; &gt; service.tx2<br />
findstr /c:SERVICE_START_NAME service.tx2 &gt; servicename.tx2<br />
for /f &#8220;tokens=1-3&#8243; %%c in (servicename.tx2) do (echo %server%,%service%,%%e &gt;&gt; services.tx2)</p>
<p>:EOM</p>
<p>REM ***Now we have two files that tell us what we need:<br />
REM ***checktheseservices.txt lists the services to check<br />
REM ***checkthesetasks.txt lists the scheduled tasks to check</p></div>
<p>This script does a couple of basic things, but it does them well. First, it prints out the group memberships of the four Windows admin groups. This tells you which accounts need to be changed. Second, it searches every server listed in the &#8220;servers.txt&#8221; file for any services running under a domain or local account, instead of LocalSystem or NT. This tells you which services need to be changed. Lastly, it tells you every scheduled task running on every server in the &#8220;servers.txt&#8221; file. This tells you which scheduled tasks need to be changed. Once you have your lists of services and tasks that need to be updated, you can also script the update.</p>
<p>Code to update Scheduled Tasks:</p>
<p id="code">REM ***Create a new document called tasklist.txt with 2 columns, separated by a comma.<br />
REM ***Column 1 should be the server name. Column 2 should be the scheduled task name<br />
REM ***EXAMPLE: server1,task1<br />
REM ***These names can be extracted from the checkthesetasks.txt file<br />
set /P newpassword=Enter the new password you want each scheduled task to run with:<br />
for /f &#8220;tokens=1-2 delims=,&#8221; %%a in (tasklist.txt) do (schtasks /change /s %%a /RP %newpassword% /TN %%b)</p>
<p>Code to update Services:</p>
<p id="code">REM ***Create a new document called servicelist.txt with 2 columns, separated by a comma.<br />
REM ***Column 1 should be the server name. Column 2 should be the service name<br />
REM ***EXAMPLE: server1,service1<br />
REM ***These names can be extracted from the checktheseservices.txt fileset /P newpassword=Enter the new password you want each service to run with:<br />
for /f &#8220;tokens=1-2 delims=,&#8221; %%a in (servicelist.txt) do (sc %%a config &#8220;%%b&#8221; password= %newpassword%)</p>
<p>After you have changed the services and scheduled tasks, you should be safe to disable the old admin&#8217;s account. Although the above scripts apply to Windows servers, the same concepts can be applied to other platforms: Identify any processes/services/scripts that run with privileged user access and change them; Identify all privileged accounts and change them as well; Remove the admin&#8217;s account from the system.</p>
<p>This process is not foolproof. A determined, disgruntled admin could install rootkits, keyloggers, trojans, and logic bombs prior to leaving. Administrators are GOD on their systems, so the only defense against this is to diligently screen your admins before you hire them. This process WILL allow you to locate and clean up access that was legitimate when it was created, even if it is not well documented. I hope you find these scripts useful.</p>
<p>-Bill</p>
<p><img width="16" src="http://digg.com/img/badges/16x16-digg-guy.gif" alt="Digg!" height="16" /> <a href="http://www.digg.com/security/How_to_clean_up_after_your_Windows_sysadmin_leaves">Digg This Story!</a></p>
]]></description>
		<wfw:commentRss>http://www.edgeblog.net/2006/clean-up-after-terminated-windows-administrators/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.692 seconds -->

