<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>
<channel>
	<title>G-Loaded Journal &#187; C</title>
	<atom:link href="http://www.g-loaded.eu/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.g-loaded.eu</link>
	<description>An open-source software and technology related journal</description>
	<lastBuildDate>Mon, 05 Dec 2011 19:55:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Descramble Passwords from gftp Bookmarks using Python</title>
		<link>http://www.g-loaded.eu/2009/05/07/descramble-passwords-from-gftp-bookmarks-using-python/</link>
		<comments>http://www.g-loaded.eu/2009/05/07/descramble-passwords-from-gftp-bookmarks-using-python/#comments</comments>
		<pubDate>Thu, 07 May 2009 07:21:56 +0000</pubDate>
		<dc:creator>George Notaras</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Snippet]]></category>
		<guid isPermaLink="false">http://www.g-loaded.eu/?p=1119</guid>
		<description><![CDATA[If you check the file where gftp keeps its bookmarks, you will notice that passwords are not stored in clear text. Instead, gftp has used an algorithm to scramble them. I cannot recall if it was one or two years ago when I had decided to write a script to convert the bookmarks from the [...]]]></description>
			<content:encoded><![CDATA[<p>If you check the file where <a href="http://gftp.seul.org/">gftp</a> keeps its bookmarks, you will notice that passwords are not stored in <em>clear text</em>. Instead, gftp has used an algorithm to <em>scramble</em> them. I cannot recall if it was one or two years ago when I had decided to write a script to convert the bookmarks from the gftp format to the FileZilla format, but I do recall that I had to descramble the passwords from the gftp bookmarks and the C code of that password descrambling algorithm had given me a hard time, because I had to port it to Python, since Python was the programming language I intended to use for my bookmark converter. At that time, I happened to hang out at <em>#python</em>, so I had asked for some help there and a kind fellow pythonista had saved the day.<br />
<span id="more-1119"></span><br />
The <strong>original password descrambling algorithm</strong> in C as found in the gftp sources at that time (<em>gftp-2.0.18/lib/misc.c</em>):</p>
<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">&nbsp;
<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>
gftp_descramble_password <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>password<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>passwordpos<span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>newstr<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>newpos<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> error<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>password <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'$'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>g_strdup <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  passwordpos <span style="color: #339933;">=</span> password <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  newstr <span style="color: #339933;">=</span> g_malloc <span style="color: #009900;">&#40;</span>strlen <span style="color: #009900;">&#40;</span>passwordpos<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  newpos <span style="color: #339933;">=</span> newstr<span style="color: #339933;">;</span>
&nbsp;
  error <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>passwordpos <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>passwordpos <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>passwordpos <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xc3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #208080;">0x41</span> <span style="color: #339933;">||</span>
          <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>passwordpos <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xc3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #208080;">0x41</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          error <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #339933;">*</span>newpos<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>passwordpos <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0x3c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span>
                  <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>passwordpos <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0x3c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      passwordpos <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      g_free <span style="color: #009900;">&#40;</span>newstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>g_strdup <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #339933;">*</span>newpos <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>newstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>The following is the Python port of the above code. The person who did the porting wished to remain nameless. The exact answer I got when I had asked about how I should give him credit for the code was:</p>
<blockquote><p>
please don&#8217;t attach my name to it. it&#8217;s horrible, awful code. Consider it public domain, do with it as you wish.
</p></blockquote>
<p>Python port:</p>
<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gftp_descrable_password<span style="color: black;">&#40;</span>password<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;gftp password descrambler
&nbsp;
    This code has been released in the Public Domain by the original author.
&nbsp;
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> password.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'$'</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> password
    newpassword = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    pwdparts = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">ord</span>, password<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>pwdparts<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>pwdparts<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span> 0xc3<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= 0x41 <span style="color: #ff7700;font-weight:bold;">or</span>
            <span style="color: black;">&#40;</span>pwdparts<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span> 0xc3<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= 0x41<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> password
        newpassword.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>pwdparts<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span> 0x3c<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> +
                               <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>pwdparts<span style="color: black;">&#91;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&amp;</span> 0x3c<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>newpassword<span style="color: black;">&#41;</span></pre></div></div>
<p>The only thing I have added to the above code is the <em>docstring</em> so that the function can be reused without licensing issues.</p>
<p>The above snippet has not been thoroughly tested, but it seems to work fine.</p>
<p>As for the bookmark converter, I am not interested in doing it any more.</p>
<div class="cc-block"><em><a href="http://www.g-loaded.eu/2009/05/07/descramble-passwords-from-gftp-bookmarks-using-python/">Descramble Passwords from gftp Bookmarks using Python</a></em>, unless otherwise expressly stated, is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>. Terms and conditions beyond the scope of this license may be available at <a href="http://www.g-loaded.eu/about/disclaimer-and-license/">www.g-loaded.eu</a>.</div>
<h4>Related Articles</h4>
<ul><li><a href="http://www.g-loaded.eu/2007/05/11/smart-bookmarks-in-epiphany/" rel="bookmark">Smart Bookmarks in Epiphany</a></li>
<li><a href="http://www.g-loaded.eu/2010/03/26/python-ssh-server-unix-twisted-conch/" rel="bookmark">Python SSH Server for UNIX Systems using Twisted.conch</a></li>
<li><a href="http://www.g-loaded.eu/2009/10/30/python-ping/" rel="bookmark">ping.py &#8211; Python Implementation of the ping command</a></li>
<li><a href="http://www.g-loaded.eu/2006/04/07/python-crash-course/" rel="bookmark">Python Crash Course</a></li>
<li><a href="http://www.g-loaded.eu/2006/05/06/sockets-programming-in-python/" rel="bookmark">Sockets Programming In Python</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.g-loaded.eu/2009/05/07/descramble-passwords-from-gftp-bookmarks-using-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

