<?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>.:Computer Defense:. &#187; Python</title>
	<atom:link href="http://www.computerdefense.org/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.computerdefense.org</link>
	<description>Sharing my thoughts with the world.</description>
	<lastBuildDate>Sun, 15 Aug 2010 15:32:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQLdb in Cygwin</title>
		<link>http://www.computerdefense.org/2009/06/mysqldb-in-cygwin/</link>
		<comments>http://www.computerdefense.org/2009/06/mysqldb-in-cygwin/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 04:26:02 +0000</pubDate>
		<dc:creator>jgraver</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=748</guid>
		<description><![CDATA[I use Windows XP on all of my 4 primary machines (work, work, laptop &#38; home). The only reason I like Windows XP is because it holds PuTTY windows so nicely and allows me to Alt-Tab between them. No Linux distro or Window Manager has ever really had the Alt-Tab experience that Windows XP provides.

The [...]]]></description>
			<content:encoded><![CDATA[<p>I use Windows XP on all of my 4 primary machines (work, work, laptop &amp; home). The only reason I like Windows XP is because it holds PuTTY windows so nicely and allows me to Alt-Tab between them. No Linux distro or Window Manager has ever really had the Alt-Tab experience that Windows XP provides.</p>
<p><img class="alignnone size-full wp-image-753" src="http://www.computerdefense.org/wp-content/uploads/2009/06/sshv2-putty-window.jpg" alt="sshv2-putty-window" width="451" height="292" /></p>
<p>The main problem with XP is that to do anything remotely useful with it you need a decent terminal/shell. I love rxvt/bash and the best rxvt you can find for Windows is via cygwin. Out of the box cygwin is an ugly brute - bash in cmd.exe != a real term.</p>
<p><img class="alignnone size-full wp-image-752" src="http://www.computerdefense.org/wp-content/uploads/2009/06/cygwin_shell.png" alt="cygwin_shell" width="565" height="352" /></p>
<p>Install rxvt via cygwin, then add some Consolas fonts (from Windows Vista) and edit your <a href="http://www.computerdefense.org/wp-content/uploads/2009/06/xdefaults.txt">.Xdefaults</a> to get a much nicer term.</p>
<p><img class="alignnone size-full wp-image-749" src="http://www.computerdefense.org/wp-content/uploads/2009/06/rxvt.jpg" alt="rxvt" width="605" height="237" /></p>
<p>Download a <a href="http://www.vim.org/download.php#pc">sadistic editor</a>. Drop in a <a href="http://www.cs.cmu.edu/~maverick/VimColorSchemeTest/index-c.html">beautiful colour-scheme</a> (brookstream). You now have a sweet shell and editor.</p>
<p><img class="alignnone size-full wp-image-756" src="http://www.computerdefense.org/wp-content/uploads/2009/06/gvim.jpg" alt="gvim" width="582" height="424" /></p>
<p>Install Python via cygwin and away you go.</p>
<p><strong>Perfection?</strong></p>
<p>I was attempting to connect to a remote MySQL DB when my sweet setup failed me.</p>
<p>Nothing in cygwin's repository could help -- there is basically no support in cygwin for MySQL (odd). There is tons of support for Python and Postgres, so this MySQL FAIL surprised me.</p>
<p>After an entire morning of Googleing it turns out the people at Cygwin Ports have solved this problem. For the sake of your sanity follow these simple steps to get Cygwin + Python + MySQL working.</p>
<ol>
<li>Follow the steps above to create a sweet cygwin development environment.</li>
<li>Follow <a href="http://sourceware.org/cygwinports/">the overly convoluted steps</a> to enable cygwin ports in your setup.exe</li>
<li>Install the following package "python-mysql" including the crucial dependancy "libmysqlclient-devel"</li>
<li> Test your setup</li>
</ol>
<p><img class="alignnone size-full wp-image-757" src="http://www.computerdefense.org/wp-content/uploads/2009/06/python.jpg" alt="python" width="605" height="198" /></p>
<p><strong>Rejoice?</strong></p>
<p>This process has saved you the experience of python eggs, installing gcc, compiling anything from source and best of all it just works.</p>
<p>Enjoy!</p>
<p><strong><em>- Graver</em></strong></p>
<p>One last note - if you have ever found a slick PNG that you wanted as an ICO (for Windows XP) check out this <a href="http://www.convertico.com/">online converter site</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2009/06/mysqldb-in-cygwin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TwCuP</title>
		<link>http://www.computerdefense.org/2009/01/twcup/</link>
		<comments>http://www.computerdefense.org/2009/01/twcup/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 07:19:59 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[twcup]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[twyt]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=639</guid>
		<description><![CDATA[Those of you that follow me on twitter may have noticed that yesterday I was posting quite a bit more frequently and most of them contained the word 'test'. I was playing around with twyt and decided to build a curses-based Twitter GUI. I've never done any curses programming before, so this was my way [...]]]></description>
			<content:encoded><![CDATA[<p>Those of you that follow me on twitter may have noticed that yesterday I was posting quite a bit more frequently and most of them contained the word 'test'. I was playing around with <a href="http://andrewprice.me.uk/projects/twyt/">twyt</a> and decided to build a curses-based Twitter GUI. I've never done any curses programming before, so this was my way of learning the functionality. I implemented command-line support in a style I found more to my liking (even though twyt already has this) and then started buliding the curses GUI. If I go very far with this, I may eventually rewrite the API to fit in with the rest of my code, but for now I'll use twyt on the backend.</p>
<p>The code is very basic, but already it can do a few things:</p>
<ul>
<li>Display latest friends list updates.</li>
<li>Display recent replies.</li>
<li>Display recent sent and received DMs.</li>
<li>Update your status.</li>
<li>Send a DM.</li>
</ul>
<p>Unfortunately the next update is most likely a week away, but when I get around to it, I plan on splitting the screen into multiple windows with your current status always displayed, along with a regularly updated friends list. Right now everything is jammed into a single window.</p>
<p>I do need to figure out how to get Twitter to display my client name (apparently I need to contact them for that) but so far, so good. Anyone wishing to take a look at my (very alpha) code, can check it out <a href="http://ns.slashconslashcon.com/websvn/listing.php?repname=TwCuP&amp;path=%2F&amp;sc=0">here</a>.</p>
<p>For those of you wondering about the name... TwCuP kinda reminds me of 'hiccup', so I found it slightly amusing at ~4am when I was trying to come up with a name.</p>
<p>Eventually this will (hopefully) be a client that can be left idling in a screen session... that's my goal anyways.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2009/01/twcup/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Playing with Python &#8211; nmap XML port frequency</title>
		<link>http://www.computerdefense.org/2008/12/playing-with-python-nmap-port-frequency/</link>
		<comments>http://www.computerdefense.org/2008/12/playing-with-python-nmap-port-frequency/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 08:27:10 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[lxml]]></category>
		<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[nmap]]></category>
		<category><![CDATA[pyplot]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=609</guid>
		<description><![CDATA[Two Python libraries have caught my attention lately, the first is lxml and the second is matplotlib. Ideally I wanted to write something short, just to get a feel for the basics of them. The goal was figuring out what I could do quickly so that I could play around with arguments and output, yet [...]]]></description>
			<content:encoded><![CDATA[<p>Two Python libraries have caught my attention lately, the first is <a href="http://codespeak.net/lxml/">lxml</a> and the second is <a href="http://matplotlib.sourceforge.net/">matplotlib</a>. Ideally I wanted to write something short, just to get a feel for the basics of them. The goal was figuring out what I could do quickly so that I could play around with arguments and output, yet still have an actual result. I also wanted a nicely formatted useful XML file to parse. I ended up going with <a href="http://nmap.org/">nmap's</a> XML output. I also decided that I would plot the frequency of ports in the file.</p>
<p>Is this useful... perhaps? I could see a sys admin wanting a count of specific open ports on the network or a pen tester wanting to know the most common ports on a target network.</p>
<p>The <a href="http://www.computerdefense.org/wp-content/uploads/2008/12/nmap_port_frequency.txt">python script</a> is extremely basic and is called via <em>nmap_port_frequency.py &lt;XML Input&gt; &lt;png Output&gt;</em>. The result is an image similar to this:</p>
<p><a href="http://www.computerdefense.org/wp-content/uploads/2008/12/nmap3.png"><img class="aligncenter size-medium wp-image-611" title="nmap3" src="http://www.computerdefense.org/wp-content/uploads/2008/12/nmap3-300x225.png" alt="" width="300" height="225" /></a></p>
<p>The above image is the output from scanning three hosts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2008/12/playing-with-python-nmap-port-frequency/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python 2.6 Released</title>
		<link>http://www.computerdefense.org/2008/10/python-26-released/</link>
		<comments>http://www.computerdefense.org/2008/10/python-26-released/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 05:58:10 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=543</guid>
		<description><![CDATA[I haven't been blogging much lately (hopefully that will change shortly). However, I wanted to make sure I mentioned this. Python 2.6 has been released. What's new in Python 2.6 can be found here.
]]></description>
			<content:encoded><![CDATA[<p>I haven't been blogging much lately (hopefully that will change shortly). However, I wanted to make sure I mentioned this. Python 2.6 has been <a href="http://www.python.org/download/releases/2.6/">released</a>. What's new in Python 2.6 can be found <a href="http://docs.python.org/dev/whatsnew/2.6.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2008/10/python-26-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: Download Heroes Graphic Novels</title>
		<link>http://www.computerdefense.org/2007/05/python-download-heroes-graphic-novels/</link>
		<comments>http://www.computerdefense.org/2007/05/python-download-heroes-graphic-novels/#comments</comments>
		<pubDate>Wed, 23 May 2007 06:51:40 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=322</guid>
		<description><![CDATA[I'm a big Heroes fan... but something I didn't do was read the graphic novels available online... I'm not sure if reading them after the season is over will have the same affect but I guess I'll find out. Anyways NBC has 34 of these novels now and I decided I wanted to grab them [...]]]></description>
			<content:encoded><![CDATA[<p>I'm a big Heroes fan... but something I didn't do was read the graphic novels available online... I'm not sure if reading them after the season is over will have the same affect but I guess I'll find out. Anyways NBC has 34 of these novels now and I decided I wanted to grab them all... Instead of using some wget-fu (since my wget-fu isn't so good) I decided to throw together a little python script to do the job. I'm adding the result here just in case anyone else wants all of these downloaded.</p>
<p><a id="p323" href="http://www.computerdefense.org/wp-content/uploads/2007/05/getheroes.txt">getHeroes.py</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2007/05/python-download-heroes-graphic-novels/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Webfuzz &#8212; A series of basically useless Python Scripts</title>
		<link>http://www.computerdefense.org/2006/12/webfuzz-a-series-of-basically-useless-python-scripts/</link>
		<comments>http://www.computerdefense.org/2006/12/webfuzz-a-series-of-basically-useless-python-scripts/#comments</comments>
		<pubDate>Sat, 09 Dec 2006 20:20:13 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=174</guid>
		<description><![CDATA[So a while ago I wrote a few small scripts and I based them all off the same imported script. They're nothing special but I figure maybe someone, somewhere has a use for them... So I've done up a small readme (which I've included with the files) and I've put them into a package for [...]]]></description>
			<content:encoded><![CDATA[<p>So a while ago I wrote a few small scripts and I based them all off the same imported script. They're nothing special but I figure maybe someone, somewhere has a use for them... So I've done up a small readme (which I've included with the files) and I've put them into a package for everyone to enjoy (or laugh at <img src='http://www.computerdefense.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ). So grab them, play with them and let me know what you think</p>
<p><a id="p175" href="http://www.computerdefense.org/wp-content/uploads/2006/12/webfuzz.tar.gz">Webfuzz Scripts</a></p>
<p align="right">Peace,<br />
HT</p>
<p align="left">From the Readme:<br />
-------</p>
<p align="left">headerfuzz.py:  This script takes a host and a max string size<br />
as it's two arguments. It then iterates through<br />
various Methods and Headers... The data for each<br />
header will grow until max string size is reached.</p>
<p>mimefuzz.py:    This script takes two headers (Accept and<br />
Content-Type) and iterates through them.<br />
Various mime types are appended to these<br />
fields. The only input for this script is<br />
a host.</p>
<p>randheader.py:  This script takes host, max string size, headers<br />
per send, and max times to run as it's arguments.<br />
Randon headers are paired together (to the<br />
supplied max) and each is populated with a<br />
string of characters (to max string size).</p>
<p>randmime.py:    This script takes a host, a number of iterations,<br />
and a max mimetype count. It loops through each<br />
iteration, appending random mimetypes.</p>
<p>webfuzz.py:     My original intent was that this could be used<br />
to fingerprint devices which speak http. Certain<br />
"allowed" values are populated for various<br />
headers. This is iterated through.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2006/12/webfuzz-a-series-of-basically-useless-python-scripts/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PyPy</title>
		<link>http://www.computerdefense.org/2006/10/pypy/</link>
		<comments>http://www.computerdefense.org/2006/10/pypy/#comments</comments>
		<pubDate>Tue, 10 Oct 2006 16:16:13 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=126</guid>
		<description><![CDATA[Hey Hey,
I just came across this today and felt I should share it... PyPy allows the translation of python into other languages... Currently it supports C, .Net, LLVM and a mimimal setup of Javascript (to be worked on during the next Google Summer of Code).
I was able to download and grab pypy 0.9 using svn [...]]]></description>
			<content:encoded><![CDATA[<p>Hey Hey,</p>
<p>I just came across this today and felt I should share it... PyPy allows the translation of python into other languages... Currently it supports C, .Net, LLVM and a mimimal setup of Javascript (to be worked on during the next Google Summer of Code).</p>
<p>I was able to download and grab pypy 0.9 using svn by running the command:</p>
<p><tt class="docutils literal"><span class="pre">svn</span> <span class="pre">co</span> <span class="pre">http://codespeak.net/svn/pypy/release/0.9.x</span> <span class="pre">pypy-0.9.x</span></tt></p>
<p><tt class="docutils literal"><span class="pre" /></tt> I used their example... it worked quite well I had a small functional .NET application that executed under mono but didn't work under .NET quite yet (Edit: This was due to user stupidity... I forgot to copy over the associated dll) ...</p>
<p>Check it out @ <a href="http://pypy.org/">http://pypy.org/</a></p>
<div align="right">Peace,<br />
HT</div>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2006/10/pypy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing a Port Scanner in Python</title>
		<link>http://www.computerdefense.org/2006/09/developing-a-port-scanner-in-python/</link>
		<comments>http://www.computerdefense.org/2006/09/developing-a-port-scanner-in-python/#comments</comments>
		<pubDate>Thu, 28 Sep 2006 03:58:56 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=117</guid>
		<description><![CDATA[Another in my line of tutorial reposts.... I really need to finish up some stuff on this... it was just a small thing I played with... there are problems with it... but it will introduce the basics..
The original (with the code) can be found here
I will also attempt to add the code here in the [...]]]></description>
			<content:encoded><![CDATA[<p>Another in my line of tutorial reposts.... I really need to finish up some stuff on this... it was just a small thing I played with... there are problems with it... but it will introduce the basics..<br />
The original (with the code) can be found <a href="http://www.antionline.com/showthread.php?threadid=257106">here</a></p>
<p>I will also attempt to add the code here in the next day or two.... (I've also got a couple smaller code releases planned)...<br />
<font size="2" class="nf"><br />
Due to errors with code formatting and my inability to get wordpress to display html without rendering it I'm going to attach the tutorial as a text file.</font><a onmousedown="selectLink(118);" id="p118" href="http://www.computerdefense.org/wp-content/uploads/2006/09/tutorial.txt" /></p>
<p><a onmousedown="selectLink(118);" id="p118" href="http://www.computerdefense.org/wp-content/uploads/2006/09/tutorial.txt">Download the Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2006/09/developing-a-port-scanner-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python Modules &#8211; Tar, Sys &amp; String &#8212; Python Tutorial</title>
		<link>http://www.computerdefense.org/2006/09/python-modules-tar-sys-string-python-tutorial/</link>
		<comments>http://www.computerdefense.org/2006/09/python-modules-tar-sys-string-python-tutorial/#comments</comments>
		<pubDate>Thu, 21 Sep 2006 06:02:09 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=102</guid>
		<description><![CDATA[Yet another repost... I have nothing new to post at the moment... but all in due time.
Original (Remember even this one is over two years old... the others are three years.... I was still learning when I wrote these... but they're good for introducing the basics)
 ********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another repost... I have nothing new to post at the moment... but all in due time.</p>
<p><a title="http://www.antionline.com/showthread.php?s=&threadid=254866" href="http://www.antionline.com/showthread.php?s=&threadid=254866">Original</a> (Remember even this one is over two years old... the others are three years.... I was still learning when I wrote these... but they're good for introducing the basics)<br />
<font size="2" class="nf"> ********************<br />
Step-by-Step Process<br />
********************<br />
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).<br />
2. Type (indentation lost -- email me if you need help)<br />
</font></p>
<blockquote>
<pre><font class="sf">code:</font>
<hr /> """
Python Script to Deal with Tarballs Flags: c(ompress), d(compress), g(zip)
"""
import tarfile
import string
import sys
try : strFlags = sys.argv[1]
except :
strFlags = "-h"
decompress = "yes"
compress = "yes"
gzip = "yes"
if string.find(strFlags, "-") == 0 :
if string.find(strFlags, "h") != -1 :
print "Usage: %s -  " % sys.argv[0]
print "Flags: d[ecompress]n       c[ompress]n       g[zip]"
sys.exit(0)
try :
strArchive = sys.argv[2]
except :
print "Error Argument Missing"
sys.exit(0)
if string.find(strFlags, "d") == -1 :
decompress = "no"
if string.find(strFlags, "c") == -1 :
compress = "no"
if string.find(strFlags, "g") == -1 :
gzip = "no"
if compress == "yes" and decompress == "yes" :
print "ERROR - CANNOT DECOMPRESS AND COMPRESS"
sys.exit(0)
elif compress == "yes" and gzip == "no" :
try : strFile = sys.argv[3]
except :
print "Error Argument Missing"
sys.exit(0)
tarball = tarfile.open(strArchive, "w")
tarball.add(strFile)
tarball.close()
elif compress == "yes" and gzip == "yes" :
try : strFile = sys.argv[3]
except :
print "Error Argument Missing"
sys.exit(0)
tarball = tarfile.open(strArchive, "w:gz")
tarball.add(strFile)
tarball.close()
elif decompress == "yes" and gzip == "no" :
try:
tarball = tarfile.open(strArchive, "r")
except :
print "ERROR - FILE MISSING"
sys.exit(0)
for tarfile.tarinfo in tarball :
tarball.extract(tarfile.tarinfo)
tarball.close()
elif decompress == "yes" and gzip == "yes" :
try :
tarball = tarfile.open(strArchive, "r:gz")
except :
print "Error - File Missing"
sys.exit(0)
for tarfile.tarinfo in tarball :
tarball.extract(tarfile.tarinfo)
tarball.close()
else :
print "ERROR NO FLAGS GIVEN"
sys.exit(0)
<hr /></pre>
</blockquote>
<p><font size="2" class="nf" /></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">3. Save the script as <strong>tar.py</strong><br />
4. Open a command prompt and type <em>python tar.py -h</em><br />
5. Now for the walk through.</font></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">We start off with a comment which is signified by 3 quotation marks<br />
</font></p>
<blockquote>
<pre><font size="2" class="nf"></font><font class="sf">code:</font>
<hr /><font size="2" class="nf"> """ </font>
<hr /></pre>
</blockquote>
<p><font size="2" class="nf"></font><font size="2" class="nf"><br />
This is also how a comment is ended.<br />
Following this we import the 3 modules we are going to use (tarfile, sys and string). We do so using the import statement. In previous tutorials we used import * from . This was done so that we wouldn't have to reference the module. However I now feel that you can keep up and reference the correct module, this is a more proper way of programming.</font></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">I have used a fair amount of error checking in this, so I will cover all those lines right no. While the error checking and the cod are by no means complete, I decided to cover some of it. I mentioned error checking in <a target="_blank" href="http://www.computerdefense.org/?p=97">Introduction to Python #3</a> if you need to go back and look at it. Basically what it does is it tries to execute the code following try : and if it is successful it carries on with the rest of the program, however if the code fails (if the argument isn't present for example) then it runs the except : code, which prints and error and then uses sys.exit(0) to tell the program to exit cleanly.</font></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">Next I set a few variables equal to yes. I suppose I could have used 1/0 but yes/no worked easier for simplicity I wanted. Basically these three variables will store the values of our flags (on or off)</font></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">Now we'll check to see if we have a - to signify our flags. The code is slightly redundant here, it has already checked for the present argument and if it didn't exist it set it to -h (the first try and except). This is just making sure the - exists to be picky, if it doesn't the program will exit.<br />
If the flag is set to -h (help) which as you can see makes use of %s to allow us to include the value of a variable in our string, as well as \n which represents a new line (for more info on either of these see <a target="_blank" href="http://www.computerdefense.org/?p=96">Introduction to Python #2</a> and <a href="http://www.computerdefense.org/?p=97">Introduction to Python #3</a>.<br />
The string.find(strFlags, -) command, simply checks to where the hyphen exists in the strFlags variable. If the - didn't exist a -1 would be returned, since it is in the first position the index of 0 is returned.<br />
Next comes a collection of if statements (I explored if statements in the original <a target="_blank" href="http://www.computerdefense.org/?p=95">Python Introduction</a></font></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">We are now into our tarfile module code. This is what we really want to explore. I have used three options since they will be most recognized, tar, untar, and gzip. The first thing we do every time is open the file we want to work with (this could be creation or an already existing file). We open a file by creating a variable to "store the file" (sorry, I'm a networking guy, not a programmer.. I'm not up on all the lingo).We use the tarfile.open to reference the file.. The first value passed to tarfile.open is the name of the archive we wish to open/create (in this case stored as strArchive), The second value is the mode (r[ead] or w[rite]). If we are dealing with gzip compression we add :gz to tell the module about the compression.<br />
If we are compressing the file, it is rather simple we just access the file by referencing it's variable (tarball) and use the add function, which we pass the name of the file we are compressing, We then close our file stream (Hey I remember the word.. I think.. but i'm not changing it in case I'm wrong) and we close it by referencing the variable/stream (tarball) with the .close function.<br />
If we are decompressing the file, we must decompress once for each file in the tar. We use a For statement (addressed in a previous tutorial i believe), if not it simple says for each file name in this file. We access the module fuction tarfile.tarinfo to find the names of the files in the archive. Then we use that name to extract it using the extract function on the filestream. Passing it the tarinfo function which stores the name of the current file in the archive.<br />
We then close the filestream in the same way we did while compressing a file.</font></p>
<p><font size="2" class="nf"></font><font size="2" class="nf">The only thing I didn't touch on was arguments. For you C/C++ programs, this should seem fairly familiar (at least based on my basic knowledge of those languages). sys.argv is an array that stores all the arguments. The first argument would be sys.argv[0], which would be the name of the script being executed, sys.argv[1] would be the name of the first argument following the script name. There is however no sys.argc function, to get the equivalent of argc in C/C++ you would have to use len(sys.argv).</font></p>
<p align="right"><font size="2" class="nf">Peace,<br />
HT</font></p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2006/09/python-modules-tar-sys-string-python-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simplistic SMTP &#8220;HoneyPot&#8221; &#8211; Python Tutorial</title>
		<link>http://www.computerdefense.org/2006/09/simplistic-smtp-honeypot-python-tutorial/</link>
		<comments>http://www.computerdefense.org/2006/09/simplistic-smtp-honeypot-python-tutorial/#comments</comments>
		<pubDate>Wed, 20 Sep 2006 05:57:34 +0000</pubDate>
		<dc:creator>Tyler Reguly</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.computerdefense.org/?p=101</guid>
		<description><![CDATA[Here's another tutorial repost
Original
 ***********************
Step-by-Step Process
***********************
1. Open your favourite editor(Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type


code:
 # Demonstrates various methods of importing modules.
from socket import *
import string
import time
# create a socket of the basic type.
s = socket(AF_INET, SOCK_STREAM)
# define our banner.
senddata1 = "220 desktop Microsoft ESMTP MAIL Service, Version 6.0.2600.1106 ready at" + time.strftime("%a, [...]]]></description>
			<content:encoded><![CDATA[<p>Here's another tutorial repost</p>
<p><a title="http://www.antionline.com/showthread.php?s=&threadid=257106" href="http://www.antionline.com/showthread.php?s=&threadid=257106">Original</a><br />
<font size="2" class="nf"> ***********************<br />
Step-by-Step Process<br />
***********************<br />
1. Open your favourite editor(Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).<br />
2. Type<br />
</font></p>
<blockquote>
<pre><font class="sf">code:</font>
<hr /> # Demonstrates various methods of importing modules.</pre>
<pre>from socket import *
import string
import time
# create a socket of the basic type.
s = socket(AF_INET, SOCK_STREAM)
# define our banner.
senddata1 = "220 desktop Microsoft ESMTP MAIL Service, Version 6.0.2600.1106 ready at" + time.strftime("%a, %d %b %Y %H:%M:%S %Z")
# Query the user for their IP Address and set that and the port
HOST = raw_input("Enter IP Address to bind socket to: ")
PORT = 25 s.bind((HOST, PORT))
# Bind the socket to an IP Address and Port
s.listen(1)
# Have the socket listen for a connection
(incomingsocket, address) = s.accept()
# Accept an incoming connection
incomingsocket.send(senddata1)
# Send our banner
straddress = str(address)
# Convert incoming address to a string
testlist = string.split(straddress, ",")
# Split the tuple into lists
gethost = string.split(testlist[0], "'")
# Split the host portion of the list
getaddr = string.split(testlist[1], ")")
# Split the port portion of the list
host = gethost[1]
# Remove just the address from the list
incomingport = int(getaddr[0])
# Remove just the port from the list
# define our Warning
senddata2 = "Illegal Access of this server, your IP [" + host +"] has been logged."
# Print connection information to the stdout
print "Connection attempt on port", PORT, "from", host, ":", incomingport
# Listen for incoming data
data = incomingsocket.recv(1024)
# Send the Warning
incomingsocket.send(senddata2)
# Close the socket incomingsocket.close
<hr /></pre>
</blockquote>
<p><font size="2" class="nf" /></p>
<p><font size="2" class="nf"><font size="2" class="nf" /></font><font size="2" class="nf">3. Save the script as <strong>honeypot.py</strong><br />
4. Open the command prompt and type <em>python honeypot.py</em> (If you get an error, you may already have port 25 in use, simply edit the file to change the port number.)<br />
5. You will be prompted with <strong>Enter IP Address to bind socket to: </strong> Enter the IP address you wish to have the honey pot listen on. This could be 127.0.0.1 if you simply with to test it, or your outgoing IP if you wish to actually listen for connection attempts.You will now notice nothing, however you can telnet or nc to the IP you entered on port 25. Your connection will display the defined banner <strong>220 desktop Microsoft ESMTP MAIL Service, Version 6.0.2600.1106 ready at</strong> followed by the current time (the %</font></p>
<p><font size="2" class="nf"><font size="2" class="nf" /></font><font size="2" class="nf">This is only a single connect server and very basic, no complex commands. As time goes on I will post another tutorial on this same honey pot, only expanded to actually convince the user they are connected to the mail server.</font></p>
<p><font size="2" class="nf"><font size="2" class="nf" /></font><font size="2" class="nf">The new commands in this tutorial include various socket commands, the time.strftime command and a few string commands.</font></p>
<p><font size="2" class="nf"><font size="2" class="nf" /></font><font size="2" class="nf"><strong>time.strftime("format") </strong>- This command returns a string containing the time and date in the specified format.<br />
<strong>str(non-string) </strong>- Converts a non-string to a string (there are also int() and tuple() commands).<br />
<strong>string.split(string, delimitor)</strong> - Splits a string into a list at every delimitor. The list is then referenced by listname[list object number] (numbering begins at 0).</font></p>
<p><font size="2" class="nf"><font size="2" class="nf" /></font><font size="2" class="nf">This script contains similar starting socket commands as my first Python Tutorial, however it includes a few extras. After accepting the connection, this script sends the banner using the incomingsocket.send command. This takes the variable we defined on the s.accept (socket accept command) and tells the computer to send data back to it. It then lists for data (incomingsocket.recv) with a maximum buffer size of 1024. Upon recieving this data it again transmits data to the connecting PC and closes the connect with incomingsocket.close.</font></p>
<p><font size="2" class="nf"><font size="2" class="nf" /><font size="2" class="nf" /></font><font size="2" class="nf"><br />
<em> Source: <a target="_blank" href="http://www.python.org/doc/lib/module-time.html">http://www.python.org/doc/lib/module-time.html</a> </em><br />
time.strftime format flags</font></p>
<p><font size="2" class="nf"><font size="2" class="nf" /><font size="2" class="nf" /></font><font size="2" class="nf">%a Locale's abbreviated weekday name.<br />
%A Locale's full weekday name.<br />
%b Locale's abbreviated month name.<br />
%B Locale's full month name.<br />
%c Locale's appropriate date and time representation.<br />
%d Day of the month as a decimal number [01,31].<br />
%H Hour (24-hour clock) as a decimal number [00,23].<br />
%I Hour (12-hour clock) as a decimal number [01,12].<br />
%j Day of the year as a decimal number [001,366].<br />
%m Month as a decimal number [01,12].<br />
%M Minute as a decimal number [00,59].<br />
%p Locale's equivalent of either AM or PM.<br />
%S Second as a decimal number [00,61].<br />
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.<br />
%w Weekday as a decimal number [0(Sunday),6].<br />
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.<br />
%x Locale's appropriate date representation.<br />
%X Locale's appropriate time representation.<br />
%y Year without century as a decimal number [00,99].<br />
%Y Year with century as a decimal number.<br />
%Z Time zone name (no characters if no time zone exists).<br />
%% A literal "%" character. </font></p>
<p align="right"><font size="2" class="nf"><font size="2" class="nf" /></font><font size="2" class="nf">Peace,<br />
HT</font></p>
]]></content:encoded>
			<wfw:commentRss>http://www.computerdefense.org/2006/09/simplistic-smtp-honeypot-python-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
