<?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>108.bz &#187; Perl</title>
	<atom:link href="http://www.108.bz/posts/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.108.bz</link>
	<description>Wandering futilities...</description>
	<lastBuildDate>Wed, 08 Sep 2010 13:45:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dumping streaming media in 25 lines of Perl</title>
		<link>http://www.108.bz/posts/it/dumping-streaming-media-in-25-lines-of-perl/</link>
		<comments>http://www.108.bz/posts/it/dumping-streaming-media-in-25-lines-of-perl/#comments</comments>
		<pubDate>Thu, 13 May 2010 10:11:23 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Network Protocol Analysis]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=459</guid>
		<description><![CDATA[Analysing TCP based protocols often means dealing with TCP sessions (also called streams or flows). A TCP connection, from an application point of view, is much like a bidirectional file descriptor through which ordered data can be read or written. &#8220;On the wire&#8221; though, data is not ordered at all. It is split into packets, [...]]]></description>
			<content:encoded><![CDATA[<p>Analysing TCP based protocols often means dealing with TCP <i>sessions</i> (also called streams or flows).<br />
A TCP connection, from an application point of view, is much like a bidirectional file descriptor through which ordered data can be read or written. &#8220;On the wire&#8221; though, data is not ordered at all. It is split into packets, possibly shuffled and mixed with other traffic. You can capture packets using a sniffer, but to make any sense of them you also need an analyzer tool able to do the reordering/reassembling job. <a href="http://www.wireshark.org">Wireshark</a>, for instance, doubles as a sniffer and an analyzer, backed up by the ubiquitous <a href="http://en.wikipedia.org/wiki/Libpcap">libpcap</a>.</p>
<p>Imagine having dumped/sniffed 1GB worth of traffic. We would like to pinpoint a single TCP session, isolating it from the rest. Here&#8217;s how we could proceed:</p>
<ul>
<li>Identify the source/destination addresses and source/destination ports we&#8217;re interested in. Then throw away any packet that doesn&#8217;t match this tuple. That&#8217;s what Wireshark basically does when you select a packet, right click and hit &#8220;Follow TCP Stream&#8221;. If the same tuple doesn&#8217;t get reused for another, unrelated, session, this method works just fine<sup class='footnote'><a href='#fn-459-1' id='fnref-459-1'>1</a></sup>.</li>
<li>Reorder/reassemble packets.</li>
<li>Extract packets&#8217; payload.</li>
<li>Present the payload in a way that makes sense. That depends on the L7 protocol. HTTP without <a href="http://en.wikipedia.org/wiki/HTTP_persistent_connection">keep-alive</a> is strictly request/response: print what the client sent to the server (outbound traffic) before and then what the server answered (inbound traffic). Other protocols may behave differently and you may choose to separate inbound traffic from outbound, or rely on timing to correctly present the dialogue between peers.</li>
</ul>
<p>Besides Wireshark, there are tools that do just that and can also be automated. See <a href="http://www.wireshark.org/docs/man-pages/tshark.html">TShark</a> or <a href="http://www.circlemud.org/~jelson/software/tcpflow/">tcpflow</a>.</p>
<p>What if you want to script everything and build your own TCP analyzer? Perl&#8217;s module <a href="http://search.cpan.org/search?query=Net%3A%3AAnalysis&#038;mode=module">Net::Analysis</a> is surprisingly convenient for the task. It does the dirty job I described above and presents your code with ready to be processed TCP sessions.</p>
<p>Practical goal: saving MP3 files streamed by <a href="http://grooveshark.com">Grooveshark</a>. Disclaimer: I&#8217;m by no means pushing anyone to illegally download stuff, this is just a working, sensible, instructional example that uses a song freely available anyway (by Revolution Void, check them out <a href="http://www.jamendo.com/en/artist/revolutionvoid/">here</a>, they&#8217;re great).</p>
<p><span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">GroovesharkListener.pm</span> extends <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Net::Analysis::Listener::HTTP</span>. It sniffs all the traffic from/to port 80 and, as soon as he sees an HTTP response with a content-type of &#8220;audio&#8221;, dumps its content to file and quits. Simple as that.</p>
<p>Put the module some place where Perl can find it and then launch (as root):</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># perl -MNet::Analysis -e main GroovesharkListener 'port 80'<br />
(starting live capture)<br />
/crossdomain.xml<br />
text/xml<br />
/service.php?addSongsToQueueExt<br />
text/html; charset=UTF-8<br />
/static/amazonart/m8c8c9f4291508bca130c1caac2bda75b.png<br />
image/png<br />
[...some more cruft...]<br />
/stream.php<br />
audio/mpeg<br />
Dumping 8481224 bytes to groovesharkgyzBy.mp3 be patient...<br />
<br />
# id3v2 -l groovesharkgyzBy.mp3<br />
id3v1 tag info for groovesharkgyzBy.mp3:<br />
Title &nbsp;: Invisible Walls &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Artist: Revolution Void &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
Album &nbsp;: Increase the Dosage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Year: 2004, Genre: Other (12)<br />
Comment: http://www.jamendo.com/ &nbsp; &nbsp; &nbsp; &nbsp; Track: 1</div></div>
<p>That&#8217;s it, just one more thing. <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Net::Analysis</span> doesn&#8217;t allow you to select a specific network interface, it just picks up the first available one. I wrote a small <a href='http://www.108.bz/wp-content/uploads/2010/05/NetAnalysis_device_support_in_live_capture.diff_.txt'>patch</a> to address this shortcoming, it adds a &#8220;<span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">device=</span>&#8221; parameter that you can use this way:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"># perl -MNet::Analysis -e main GroovesharkListener,device=wlan1 'port 80'</div></div>
<p>And here&#8217;s what <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">GroovesharkListener.pm</span> looks like:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># choose a song</span><br />
<span style="color: #666666; font-style: italic;"># run (as root or via sudo):</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; perl -MNet::Analysis -e main GroovesharkListener 'port 80'</span><br />
<span style="color: #666666; font-style: italic;"># click &quot;play&quot; and wait for the file to be dumped...</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Giuliano - http://www.108.bz</span><br />
<a href="http://perldoc.perl.org/functions/package.html"><span style="color: #000066;">package</span></a> Net<span style="color: #339933;">::</span><span style="color: #006600;">Analysis</span><span style="color: #339933;">::</span><span style="color: #006600;">Listener</span><span style="color: #339933;">::</span><span style="color: #006600;">GroovesharkListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> base <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span>Net<span style="color: #339933;">::</span><span style="color: #006600;">Analysis</span><span style="color: #339933;">::</span><span style="color: #006600;">Listener</span><span style="color: #339933;">::</span><span style="color: #006600;">HTTP</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Temp</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> http_transaction <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$http_req</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>req<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$http_resp</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$args</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>resp<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <br />
<br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$http_req</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$http_resp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Content-Type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;$content_type<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$content_type</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/audio/i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File<span style="color: #339933;">::</span><span style="color: #006600;">Temp</span><span style="color: #009900;">&#40;</span>TEMPLATE <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'groovesharkXXXXX'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SUFFIX &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'.mp3'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UNLINK &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Dumping &quot;</span><span style="color: #339933;">.</span><a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$http_resp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; bytes to &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$fh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">filename</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; be patient...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$fh</span> <span style="color: #0000ff;">$http_resp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">content</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></div></div>
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-459-1'>newer Wireshark(s) use the &#8220;tcp.stream eq <i>x</i>&#8221; primitive <span class='footnotereverse'><a href='#fnref-459-1'>&#8617;</a></span></li>
</ol>
</div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=459" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/dumping-streaming-media-in-25-lines-of-perl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Abusing Backup Exec&#8217;s internal database</title>
		<link>http://www.108.bz/posts/it/abusing-backup-exec-internal-database/</link>
		<comments>http://www.108.bz/posts/it/abusing-backup-exec-internal-database/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 23:18:35 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Backup Exec]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Symantec]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=348</guid>
		<description><![CDATA[What&#8217;s the point of having data stored somewhere if you can&#8217;t access it and turn it into useful information? Of course the means to do so should be safe, supported, non destructive and flexible if not easy. But usually all you&#8217;re left with is some kind of &#8220;reporting&#8221; feature that necessarily doesn&#8217;t do exactly what [...]]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s the point of having data stored somewhere if you can&#8217;t access it and turn it into useful information? Of course the means to do so should be safe, supported, non destructive and flexible if not easy. But usually all you&#8217;re left with is some kind of &#8220;reporting&#8221; feature that necessarily doesn&#8217;t do <i>exactly</i> what you need, doesn&#8217;t output in a convenient format and so on.</p>
<p>But enough squabbling: in this article I&#8217;ll deal with Backup Exec&#8217;s internal database.<br />
Here&#8217;s what I&#8217;m trying to do:</p>
<ul>
<li>Look up all the &#8220;Duplicate&#8221; Jobs. Show when they started, how long they took to complete, the rate, &#8230;</li>
<li>For each one of them, try and find the relevant tapes.</li>
</ul>
<p>I will use the generated report to know which media I should eject out of the library for safe storage. The report will also allow me to quickly and easily update the Excel worksheets where we keep track of how backup&#8217;s going.</p>
<p>Our BE database runs on Microsoft SQL Server Express. First thing to do is configure the instance to allow remote TCP/IP connections. Refer to this <a href="http://blogs.msdn.com/sqlexpress/archive/2005/05/05/415084.aspx">post</a>, and <a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;914277">KB914277</a>.</p>
<p>Then I&#8217;m able to point <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&#038;FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796">SQL Server Management Studio</a> at it<sup class='footnote'><a href='#fn-348-1' id='fnref-348-1'>1</a></sup>, and see how the BEDB database is organized. </p>
<p>The view named <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">vwJobHistorySummary</span> is the equivalent of what is seen in BE&#8217;s GUI, under <i>Job Monitor</i> &rarr; <i>Job List</i> &rarr; <i>Job History</i>. Easy enough to find out.</p>
<p>What&#8217;s not that immediate to guess is how Media IDs relate to Job IDs: skimming through the database tables doesn&#8217;t help&#8230; How could you reverse engineer BE GUI and discover what SQL queries it&#8217;s doing to carry out its job? In fact, there&#8217;s a way to &#8220;sniff&#8221; SQL queries while they&#8217;re running:</p>
<ul>
<li>open up BE GUI and select (but don&#8217;t open) a completed Job in <i>Job History</i>.</li>
<li>run <i>SQL Server Profiler</i>.</li>
<li>create a <i>New Trace</i>.</li>
<li>Under the <i>Event Selection</i> tab, deselect everything except <i>SQL:BatchStarting</i>. This is not a particularly crowded database, hence no need for filters.</li>
<li>Double click on the previously selected Job; SQL Profiler should capture a query similar to:</li>
</ul>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> dbo<span style="color: #66cc66;">.</span>vwJobHistory <span style="color: #993333; font-weight: bold;">WHERE</span><br />
JobHistoryID<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'8507cfa9-8417-44ae-88e6-9ac19a0333a9'</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>JHD<span style="color: #66cc66;">.</span>StartTime<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO</div></div>
<p>Looks like Job details are fetched as globs of XML data, perfect to throw our beloved Regular Expressions at.</p>
<p>You can find the script I made at the end of the post. The obligatory notes are:</p>
<ul>
<li>By convention, in our scenario, Policies used to create Duplicate jobs bear a name ending with &#8220;-D&#8221;. I&#8217;m SELECting the last Job IDs with a similar pattern; change it according to your needs, for instance if you&#8217;re interested in all the tape directed Jobs (and not just the Duplicate ones).</li>
<li>Columns are as follow:
<ul>
<li>Job name. In case you wonder, &#8220;FSIWDTH&#8221; means: Full Saturday, Incremental Weekdays, Duplicate on Thursday.</li>
<li>Actual start timestamp.</li>
<li>End timestamp.</li>
<li>Elapsed time (seconds).</li>
<li>Total bytes written. No bytes written? I skip this Job.</li>
<li>Rate (MBytes/minute). Oddly, BE doesn&#8217;t seem to always get this value right.</li>
</ul>
</li>
<li>The &#8220;<span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">convert( varchar(</span>&#8221; stuff in the main query is needed to fetch dates in a non driver-dependent format (see FreeTDS <a href="http://www.freetds.org/faq.html#dateformat">FAQ</a>).</li>
<li>Dates are stored in UTC timezone. I make sure of adding the local TZ offset before printing them out.</li>
</ul>
<p>Example output:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">./be_job_media_report.pl <br />
Sel SERVER03-FSIWDTH-D;20100311 09:15;20100312 11:26;94239;602103130444;405<br />
AK8008L3<br />
AK8011L3<br />
Sel SERVER07-FSIWDTH-D;20100311 09:00;20100311 09:15;921;5133161638;452<br />
AK8011L3<br />
Sel SERVER16-FSIWDWE-D;20100310 10:35;20100310 17:34;25155;5352;0<br />
AK8011L3<br />
Sel SERVER13-FSIWDWE-D;20100310 09:00;20100310 17:29;30572;230425324573;515<br />
AK7140L1<br />
AK8011L3</div></div>
<p>And the script itself:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl </span><br />
<span style="color: #666666; font-style: italic;"># Giuliano - http://www.108.bz</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> List<span style="color: #339933;">::</span><span style="color: #006600;">Uniq</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span>uniq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Piece</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> pretty_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$)</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$time</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$time</span> <span style="color: #339933;">=</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Piece</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strptime</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># 2010-03-10 16:34:19</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$time</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">localtime</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">tzoffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #0000ff;">$time</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'%Y%m%d %H:%M'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> print_last_jobids<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$$</span><span style="color: #339933;">;</span><span style="color: #0000ff;">$)</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$number</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$jobname_like</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOQ;<br />
SELECT TOP 20 JobHistoryID, JobName,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; convert( varchar(30), OriginalStartTime, 120),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; convert( varchar(30), ActualStartTime, 120),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; convert( varchar(30), EndTime, 120),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ElapsedTimeSeconds,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FinalJobStatus, FinalErrorCode, TotalDataSizeBytes, TotalRateMBMin<br />
FROM vwJobHistorySummary<br />
-- WHERE<br />
-- Jobname LIKE '$jobname_like'<br />
ORDER BY ActualStartTime DESC<br />
EOQ</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/-- (WHERE)/$1/</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$jobname_like</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/-- (Jobname LIKE)/$1/</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$jobname_like</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$row</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_arrayref</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># TotalDataSizeBytes &gt; 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pretty_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pretty_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;%s;%.f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a> <span style="color: #ff0000;">';'</span><span style="color: #339933;">,</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$row</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; print_media_by_jobhistoryid<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> print_media_by_jobhistoryid<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$$</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$jobid</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&lt;</span>EOQ<br />
SELECT <span style="color: #339933;">*</span> FROM dbo<span style="color: #339933;">.</span>vwJobHistory where<br />
JobHistoryID<span style="color: #339933;">=</span><span style="color: #ff0000;">'$jobid'</span> ORDER BY <span style="color: #009900;">&#91;</span>JHD<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#93;</span><br />
FOR XML AUTO<br />
EOQ<br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@media</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$row</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_arrayref</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$record</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a> <span style="color: #ff0000;">';'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@$row</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a> <span style="color: #0000ff;">@media</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$1</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$record</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/Data=&quot;(.*?)&quot;/</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> uniq<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><a href="http://perldoc.perl.org/functions/sort.html"><span style="color: #000066;">sort</span></a> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">@media</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">@media</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">### Main</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'dbi:Sybase:server=bedbdatasource;database=BEDB'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'DOMAIN\username'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a><span style="color: #339933;">;</span><br />
print_last_jobids<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'%-D'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span></div></div>
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-348-1'>No need to enable TCP/IP on the instance, if Management Studio is installed on BE server itself <span class='footnotereverse'><a href='#fnref-348-1'>&#8617;</a></span></li>
</ol>
</div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=348" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/abusing-backup-exec-internal-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detecting malware using Windows Auditing events</title>
		<link>http://www.108.bz/posts/it/detecting-malware-using-windows-auditing-events/</link>
		<comments>http://www.108.bz/posts/it/detecting-malware-using-windows-auditing-events/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 23:58:18 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=314</guid>
		<description><![CDATA[This post1 explains how to use nmap and smb-check-vulns to scan a network in search of Conficker infected hosts. I thought that the whole Conficker case was over, but hopefully some of the measures I took to deal with it almost an year ago, will still be relevant to other kinds of malware. And, also, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.disordine.com/2010/02/11/come-cercare-luntore-nella-propria-rete/">This</a> post<sup class='footnote'><a href='#fn-314-1' id='fnref-314-1'>1</a></sup> explains how to use nmap and smb-check-vulns to scan a network in search of <a href="http://en.wikipedia.org/wiki/Conficker">Conficker</a> infected hosts. I thought that the whole Conficker case was over, but hopefully some of the measures I took to deal with it almost an year ago, will still be relevant to other kinds of malware. And, also, the method I&#8217;ll show you here differs from the nmap one in that the latter is active, whereas mine is passive. Actively probing an host for vulnerabilities could be very very much alike &#8220;exploiting&#8221; it as malware does, and have similar effects. For instance, a service/process could crash, making it not always advisable to run active scans on your servers subnet. Passive analysis, on the other hand, unobtrusively collects clues about who&#8217;s misbehaving. </p>
<p>During the Conficker/Downadup outburst, we observed that:</p>
<ul>
<li>Antivirus wasn&#8217;t always able to detect/stop it.</li>
<li>The virus was copying files in known directories (<span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">C:\WINDOWS\SYSTEM32</span>) on about to be infected machines.</li>
<li>Security patched hosts were still subject to the remote malicious file copying routine. The copy could either succeed or fail, depending on which permissions had the user that &#8220;runs&#8221; the virus. The copy in itself doesn&#8217;t pose any security concern. Even if no A/V is active on the destination host, but virus exploitable flaws have been patched, malware won&#8217;t be able to activate itself. Otherwise, the A/V would remove suspect files as soon as they are caught, without interfering with our detection purposes.</li>
</ul>
<p>This behaviour makes it possible to use a &#8220;honeypot&#8221; approach. The detecting server can be any production host provided that it is security patched and A/V protected. You could, as we did, choose a Domain Controller and: </p>
<ul>
<li>Run <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Administrative Tools</span> &rarr; <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Domain Controller Security Policy</span></li>
<p><a href="http://www.108.bz/wp-content/uploads/2010/03/detectingmalware1.gif"><img src="http://www.108.bz/wp-content/uploads/2010/03/detectingmalware1.gif" alt="" title="detectingmalware1" width="429" height="44" class="alignnone size-full wp-image-322" /></a></p>
<li>Modify the Audit Policy, enabling tracking of successful <i>logon events</i> and <i>object access</i>. By default the OS will only log <i>failures</i>, but that&#8217;s not enough.</li>
<p><a href="http://www.108.bz/wp-content/uploads/2010/03/detectingmalware21.gif"><img src="http://www.108.bz/wp-content/uploads/2010/03/detectingmalware21.gif" alt="" title="detectingmalware2" width="562" height="180" class="alignnone size-full wp-image-329" /></a></p>
<li><i>Object Access</i> is activated at a file/directory level. Open up the Properties of a directory you know is accessed by the virus, click on <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Security</span>, then <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Advanced</span>. The <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">Auditing</span> tab is what you&#8217;re interested in. Set things up so that any &#8220;Create File/Write Data&#8221; attempt of Type &#8220;Success&#8221; will be logged. The semantics about how auditing settings are propagated from parent to child works in the same way as NFTS permissions.</li>
<p><a href="http://www.108.bz/wp-content/uploads/2010/03/detectingmalware3.gif"><img src="http://www.108.bz/wp-content/uploads/2010/03/detectingmalware3.gif" alt="" title="detectingmalware3" width="549" height="465" class="alignnone size-full wp-image-324" /></a></p>
<li>From this point on, you should monitor the honeypot server&#8217;s Security Event Log. I wrote a Perl script to do it for me. It works by selecting events with ID 560 and 540, extracting their text and printing just the needed info.</li>
</ul>
<p>Let&#8217;s look at how it&#8217;s used (the only parameter is the hostname/address of the honeypot server):</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\loganalysis&gt;perl ddloganalysis.pl honeypot-srv.domain.lan &gt; ddlog.txt</div></div>
<p>Skimming through the generated log, you&#8217;ll notice the files being dropped into <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">C:\WINDOWS\system32</span> (or any directory you set up for auditing), the user that actually created them and, before (time-wise), from which address the user is coming.</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">17/03/2009 16.26.19 &nbsp; 560 : C:\WINDOWS\system32\onevthx.vr (Administrator)<br />
17/03/2009 16.26.18 &nbsp; 540 : &nbsp;(10.1.1.94 - Administrator)<br />
17/03/2009 15.35.24 &nbsp; 560 : C:\WINDOWS\system32\onevthx.vr (SpectrumLT)<br />
17/03/2009 15.35.24 &nbsp; 540 : &nbsp;(10.6.3.6 - SpectrumLT)</div></div>
<p>We successfully used the script to pinpoint the rogue hosts. Deeming it useful, here it is:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!perl</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Win32<span style="color: #339933;">::</span><span style="color: #006600;">EventLog</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> POSIX <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a> <span style="color: #009900;">&#40;</span> strftime <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@matches</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#'job$', &nbsp; # useless, since scheduled tasks are always created by SYSTEM</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">'system32'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">'eicar.com'</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Usage:<span style="color: #000099; font-weight: bold;">\n</span>$0 servername&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ev</span><span style="color: #339933;">=</span>Win32<span style="color: #339933;">::</span><span style="color: #006600;">EventLog</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Security'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Can't open EventLog<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$recs</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$ev</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">GetNumber</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$recs</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Can't get number of EventLog records<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$base</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$ev</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">GetOldest</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$base</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">&quot;Can't get number of oldest EventLog record<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> getts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$)</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> strftime <span style="color: #ff0000;">'%d/%m/%Y %H.%M.%S'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/localtime.html"><span style="color: #000066;">localtime</span></a> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@progress</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'-'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>,'</span><span style="color: #339933;">|</span><span style="color: #ff0000;">','</span><span style="color: #339933;">/</span><span style="color: #ff0000;">','</span><span style="color: #339933;">-</span><span style="color: #ff0000;">','</span>\<span style="color: #ff0000;">','</span><span style="color: #339933;">|</span><span style="color: #ff0000;">','</span><span style="color: #339933;">/</span><span style="color: #ff0000;">');<br />
<br />
my $x = $recs-1;<br />
my $h;<br />
while ($x &gt;= 0) {<br />
&nbsp; &nbsp; $ev-&gt;Read(EVENTLOG_FORWARDS_READ|EVENTLOG_SEEK_READ,<br />
&nbsp; &nbsp; &nbsp; &nbsp; $base + $x,<br />
&nbsp; &nbsp; &nbsp; &nbsp; $h)<br />
&nbsp; &nbsp; &nbsp; &nbsp; or die &quot;Can'</span>t <a href="http://perldoc.perl.org/functions/read.html"><span style="color: #000066;">read</span></a> EventLog entry <span style="color: #666666; font-style: italic;">#$x\n&quot;;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #0000ff;">$progress</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$#progress</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">@progress</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Source<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'Security'</span> <span style="color: #b1b100;">and</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>EventID<span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">560</span> <span style="color: #b1b100;">or</span> <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>EventID<span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">540</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Win32<span style="color: #339933;">::</span><span style="color: #006600;">EventLog</span><span style="color: #339933;">::</span><span style="color: #006600;">GetMessageText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>EventID<span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">560</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Message<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/Object Name:[\t ]*(.*?)\r/gis</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Message<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/Client User Name:[\t ]*(.*?)\r/gis</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$clientusername</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/grep.html"><span style="color: #000066;">grep</span></a> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$m</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$filename</span> <span style="color: #339933;">=~</span> <span style="color: #339933;">/</span><span style="color: #0000ff;">$m</span><span style="color: #339933;">/</span>i<span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;%s %5d : %s (%s)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> getts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>TimeGenerated<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>EventID<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$clientusername</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>EventID<span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">540</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Message<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/User Name:[\t ]*(.*?)\r/gis</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Message<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/Workstation Name:[\t ]*(.*?)\r/gis</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$workstation</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Message<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/Source Network Address:[\t ]*(.*?)\r/gis</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$addr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/printf.html"><span style="color: #000066;">printf</span></a> <span style="color: #ff0000;">&quot;%s %5d : %s (%s - %s)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> getts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>TimeGenerated<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$h</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>EventID<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$workstation</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$addr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$username</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$workstation</span> <span style="color: #b1b100;">or</span> <span style="color: #0000ff;">$addr</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$x</span><span style="color: #339933;">--;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span></div></div>
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-314-1'>In italian, sorry. Look <a href="http://jahboite.co.uk/blog/2009/03/nmap-4-85beta5-and-conficker.html">here</a> for an english equivalent and <a href="http://nmap.org/nsedoc/scripts/smb-check-vulns.html">here</a> for more info. <span class='footnotereverse'><a href='#fnref-314-1'>&#8617;</a></span></li>
</ol>
</div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=314" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/detecting-malware-using-windows-auditing-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From SQL to Excel, with Perl</title>
		<link>http://www.108.bz/posts/it/from-sql-to-excel-with-perl/</link>
		<comments>http://www.108.bz/posts/it/from-sql-to-excel-with-perl/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 12:24:39 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Swyx]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=239</guid>
		<description><![CDATA[Quite often I&#8217;m asked to pull out some information from a database, process it and produce an Excel report. Here is a minimal Perl script that carries out the task. Define the column headings and their widths. @columns array. Handle the command line parameters. There are 5 in the example, assigned to the $p_* variables. [...]]]></description>
			<content:encoded><![CDATA[<p>Quite often I&#8217;m asked to pull out some information from a database, process it and produce an Excel report.<br />
Here is a minimal Perl script that carries out the task.</p>
<ul>
<li>Define the column headings and their widths. <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">@columns</span> array.</li>
<li>Handle the command line parameters. There are 5 in the example, assigned to the <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">$p_*</span> variables.</li>
<li>Prepare the Excel worksheet, defining cell formatting, &#8230;</li>
<li>Connect to the database.</li>
<li>Prepare the query, substituting the command line parameters.</li>
<li>Fetch rows, populate the sheet.</li>
</ul>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl </span><br />
<span style="color: #666666; font-style: italic;"># Giuliano - http://www.108.bz</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Spreadsheet<span style="color: #339933;">::</span><span style="color: #006600;">WriteExcel</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> constant C_HEADING <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> constant C_WIDTH &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@columns</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Date'</span><span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">22</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Caller'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #cc66cc;">20</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Called'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #cc66cc;">20</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Connected'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Duration'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Reason'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #cc66cc;">24</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'XferExt'</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'XferName'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #cc66cc;">22</span> <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #339933;">&lt;&lt;</span>EOM <span style="color: #b1b100;">unless</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
usage<span style="color: #339933;">:</span><br />
<span style="color: #0000ff;">$0</span> year month day phonenumber file<span style="color: #339933;">.</span>xls<br />
EOM<br />
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p_year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_phnumber</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$workbook</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> Spreadsheet<span style="color: #339933;">::</span><span style="color: #006600;">WriteExcel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p_filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sheet</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_worksheet</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$default_format</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_format</span><span style="color: #009900;">&#40;</span>num_format <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'@'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$default_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_font</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Verdana'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$default_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_border</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$bold_format</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_format</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_font</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Verdana'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_bold</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_border</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$columns</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>C_HEADING<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$#columns</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_column</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$columns</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>C_WIDTH<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$#columns</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'dbi:Sybase:server=dsnname;database=dnbame'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'username'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&lt;</span>EOQ<br />
SELECT IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>OriginationNumber<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>CalledNumber<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>DestinationNumber<span style="color: #339933;">,</span> DATEDIFF<span style="color: #009900;">&#40;</span>ss<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>EndTime<span style="color: #009900;">&#41;</span> AS Duration<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>DisconnectReason<span style="color: #339933;">,</span> IpPbxCDR_1<span style="color: #339933;">.</span>CalledNumber AS XferExt<span style="color: #339933;">,</span> <br />
IpPbxCDR_1<span style="color: #339933;">.</span>CalledName AS XferName<br />
FROM IpPbxCDR LEFT OUTER JOIN<br />
IpPbxCDR AS IpPbxCDR_1 ON IpPbxCDR<span style="color: #339933;">.</span>TransferredToCallId <span style="color: #339933;">=</span> IpPbxCDR_1<span style="color: #339933;">.</span>CallId<br />
WHERE <span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>CalledNumber LIKE <span style="color: #ff0000;">'$p_phnumber'</span><span style="color: #009900;">&#41;</span> AND<br />
<span style="color: #009900;">&#40;</span>MONTH<span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p_month</span><span style="color: #009900;">&#41;</span> AND<br />
<span style="color: #009900;">&#40;</span>YEAR<span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p_year</span><span style="color: #009900;">&#41;</span> AND <br />
<span style="color: #009900;">&#40;</span>DAY<span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p_day</span><span style="color: #009900;">&#41;</span><br />
ORDER BY IpPbxCDR<span style="color: #339933;">.</span>StartTime<br />
EOQ<br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$row</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_arrayref</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write_string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$default_format</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$#</span><span style="color: #0000ff;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">activate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span></div></div>
<p>Actually, the example does something useful. It connects to a Swyx Call Detail Record database, selecting phone calls placed to a given number on a given day. The generated report also contains call duration and transfer status/destination, if any. Here&#8217;s what it looks like (some data has been obfuscated, to protect the innocent &#8211; click to see all the columns):</p>
<p><a href="http://www.108.bz/wp-content/uploads/2010/01/callreport.gif"><img class="alignnone size-medium wp-image-246" title="Call Report" src="http://www.108.bz/wp-content/uploads/2010/01/callreport_cut.gif" alt="" width="579" height="83" /></a></p>
<p>And here&#8217;s the command that produces it:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">./callreport.pl 2010 1 19 '+39%10123123' x.xls</div></div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=239" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/from-sql-to-excel-with-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
