<?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>sp2hari@weblog... &#187; programming</title>
	<atom:link href="http://sp2hari.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://sp2hari.com</link>
	<description>I see, learn and rediscover... everyday!</description>
	<lastBuildDate>Wed, 28 Jul 2010 01:13:10 +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>Online Programming Contest Calendar.</title>
		<link>http://sp2hari.com/2009/05/25/online-programming-contest-calendar/</link>
		<comments>http://sp2hari.com/2009/05/25/online-programming-contest-calendar/#comments</comments>
		<pubDate>Tue, 26 May 2009 00:39:17 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=223</guid>
		<description><![CDATA[Hi all, I&#8217;m very happy to present you Online Programming Contest Calendar (http://www.opccalendar.com/). This is something I wanted to do for Spider in college. All I could finish in college was a simple fetcher from algorithmist.com and topcoder.com. Now, I have everything I wanted to have in this new site. And yes, I hated the]]></description>
			<content:encoded><![CDATA[<p>Hi all, </p>
<p>I&#8217;m very happy to present you Online Programming Contest Calendar (<a href="http://www.opccalendar.com/">http://www.opccalendar.com/</a>).  This is something I wanted to do for <a href="http://www.nitt.edu/home/students/clubsnassocs/computing/spider/">Spider</a> in college. All I could finish in college was a simple fetcher from algorithmist.com and topcoder.com. Now, I have everything I wanted to have in this new site. And yes, I hated the algorithmist.com calendar. There was so much I wanted to do for that calendar and the result is <a href="http://www.opccalendar.com/">http://www.opccalendar.com/</a>.</p>
<p>Some of the cool features I like in this site are. </p>
<ul>
<li>Long list of services like Twitter, Google Reader, Google Calendar, Google India SMS Channel, WordPress plugin, Firefox plugin and lots more.</li>
<li>Alerts through Email, Orkut and Facebook are on the way. </li>
<li>Automated parses for most of the common programming contests. </li>
<li>Neat interface. </li>
</ul>
<p>Things I would like to have in this soon. (Of course, depends on how much time I have to spend on this project)</p>
<ul>
<li>Accounts for each member, where they can choose the alerts they want to have. (probably Google accounts integration)</li>
<li>Interface for members to add contests on their own. </li>
<li>A simple forum</li>
</ul>
<p>Any volunteers out there?</p>
<p>There are so many people who helped me in build this site. I guess this is right place to thank them. </p>
<p><code><a href="http://www.topcoder.com/tc?module=MemberProfile&#038;cr=22675930">Yazhini</a> for helping me with the first prototype of the parser code to fetch from algorithmist.com. Written in C++ and still works cool :)<br />
<a href="http://rvivek.com/">Vivek</a> for helping me with the text context for this website.<br />
<a href="http://sindhu-s.com/">Sindhu</a> and her friends <a href="http://sathyasays.com/">Sathyajith</a> and <a href="http://www.shutterexposed.com//">Gautam</a> for helping me with the ads.<br />
<a href="http://theyagar.com">Taggy</a> for giving me webspace in his mediatemple account. Seriously, I should thank this guy for his continuous support with his mediatemple account.<br />
<a href="http://twitter.com/arvindevo">Arvind</a>, <a href="http://twitter.com/dondoniv">Vinod</a> and <a href="http://www.topcoder.com/tc?module=MemberProfile&#038;cr=13366203">Prasanna</a> for their feedback during each iteration of this site. </code></p>
<p>Hope this becomes the one and only place which has all the details about programming contest.</p>
<p>How can you help me promote this site?<br />
1. Keep this site url as GTalk status message. That will really help many many people know about this site.<br />
2. Tell your geek friends about this site.</p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2009/05/25/online-programming-contest-calendar/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Dictionary attack</title>
		<link>http://sp2hari.com/2009/03/13/dictionary-attack/</link>
		<comments>http://sp2hari.com/2009/03/13/dictionary-attack/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 01:14:38 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cracking]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=209</guid>
		<description><![CDATA[This is a tutorial about how to perform a dummy dictionary attack and how web-masters can prevent a dictionary attack. Wikipedia defines Dictionary attack as, In cryptanalysis and computer security, a dictionary attack is a technique for defeating a cipher or authentication mechanism by trying to determine its decryption key or passphrase by searching likely]]></description>
			<content:encoded><![CDATA[<p>This is a tutorial about how to perform a dummy dictionary attack and how web-masters can prevent a dictionary attack. </p>
<p>Wikipedia defines <a href="http://en.wikipedia.org/wiki/Dictionary_attack">Dictionary attack</a> as,<br />
<code><br />
In cryptanalysis and computer security, a dictionary attack is a technique for defeating a cipher or authentication mechanism by trying to determine its decryption key or passphrase by searching likely possibilities.<br />
A dictionary attack uses a brute-force technique of successively trying all the words in an exhaustive list (from a pre-arranged list of values). In contrast with a normal brute force attack, where a large proportion key space is searched systematically, a dictionary attack tries only those possibilities which are most likely to succeed, typically derived from a list of words in a dictionary. Generally, dictionary attacks succeed because many people have a tendency to choose passwords which are short (7 characters or fewer), single words found in dictionaries or simple, easily-predicted variations on words, such as appending a digit.<br />
</code></p>
<p>In this post, I&#8217;m going to write a simple code which will send POST requests to the web server and process the response of each request. Before I start, let me tell you that there are many softwares which already do this. Some of them are, </p>
<p>1. <a href="http://portswigger.net/suite/">Burp Suite</a><br />
2. <a href="http://www.openwall.com/john/">John the Ripper</a> </p>
<p>But what&#8217;s the fun in using a software and cracking something. :) The fun part is when YOUR code does something. After all, <em>Code is Poetry</em> (Check wordpress.org footer)  ;) </p>
<p>For the demo here, I have <a href="http://sp2hari.com/work/dict/login.php">this login page</a>, which is very similar to my college mail server (or any horde server). The user-name and password for a successful login is &#8220;cs10528&#8243; and &#8220;remember&#8221; respectively. If you provide a valid user-name/password combination, the page will display &#8220;Login Successful&#8221;. For any other user-name/password combination, the page will display &#8220;Login Failed&#8221;. So, now all we need is a code which can send POST request to the login page and a dictionary from which we can pick up the probable passwords. </p>
<p><a href="http://sp2hari.com/work/dict/curl.php.html">This is the PHP code</a> I&#8217;ve written to automate the login process. It reads from the &#8220;dict.txt&#8221; file and uses that to send the HTTP request. The code is well commented and main login process is done by curl functions. And finally, <a href="http://sp2hari.com/work/dict/dict.txt">here is the dict.txt</a> I&#8217;ve used for this demo. It has only around 25 words, so that the processing ends sooner. In case you are not comfortable with curl in php, you can <a href="http://sp2hari.com/2008/10/01/fetching-the-internet/">see this post</a>, which explains about curl basics.</p>
<p>You can see the whole login process in action <a href="http://sp2hari.com/work/dict/curl.php">here</a>.</p>
<p><strong>So, how can a web-master prevent these automatic logins?</strong></p>
<p>1. <a href="http://en.wikipedia.org/wiki/Captcha">Captcha</a><br />
 Adding a captcha prevents almost all the automatic login attempts. But be careful about what kind of captcha you choose. There are quite a few captchas which confuse people and scare them away.    </p>
<p>2. <a href="http://sw-guide.de/wordpress/plugins/math-comment-spam-protection/">Math Puzzles</a><br />
Instead of images, you can ask the user to enter some simple arithmetic calculations (like what is 10+15). This loads much faster and prevents any confusion.</p>
<p>3. Maintaining the state in server<br />
Store the login access time for each user, and if the time difference is less than 5 seconds, then block the user for next 10 minutes. This needs some more modification in the server side code, with new tables needed to store the login access time data.</p>
<p>Note :<br />
1. The code posted here won&#8217;t work for horde. There are cookies and other stuff to be handled in many cases. </p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2009/03/13/dictionary-attack/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Obfuscate C Code &#8211; Part III</title>
		<link>http://sp2hari.com/2008/12/14/obfuscate-c-code-part-iii/</link>
		<comments>http://sp2hari.com/2008/12/14/obfuscate-c-code-part-iii/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 22:37:02 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Obfuscation]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=169</guid>
		<description><![CDATA[Hi all, This is my third post on the series of articles I&#8217;m writing on C code obfuscation. You can check all the posts I&#8217;ve written about obfuscation here. The task for this post is quite simple. Given two numbers a and b (b>a>3) as input, print all prime numbers in between a and b]]></description>
			<content:encoded><![CDATA[<p>Hi all, </p>
<p>This is my third post on the series of articles I&#8217;m writing on C code obfuscation. You can check all the posts I&#8217;ve written about obfuscation <a href="http://sp2hari.com/tag/obfuscation/">here</a>.</p>
<p>The task for this post is quite simple. Given two numbers a and b (b>a>3) as input, print all prime numbers in between a and b (including a and b). </p>
<p>Let me first paste final code here. I&#8217;ll be happy if you feel that you can&#8217;t make head or tail of it. ;-)</p>
<p><code><br />
#define _$_ "%d",&#038;I);I;})<br />
Y,A,Z,H,I;main() {<br />
#define __$__ ({ int I; scanf(_$_<br />
#define FOR(_,__,___)for(_=__;_&lt;___;+_++)<br />
Y=__$__,A=__$__;FOR(Z,<br />
#define T ({sscanf("2 is the only even prime number. Do you know that?", _$_<br />
Y,A+T/2)FOR(H,T,Z)if(!(Z%H)||(H==Z-1&#038;&#038;printf("%d\n",Z)))break;}<br />
</code></p>
<p>Like the last post, we will start with a simple, clean code. The following code solves the given task.</p>
<p><code><br />
#include &lt;stdio.h&gt;<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;int a, b, i, j;<br />
&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d", &#038;a);<br />
&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d", &#038;b);<br />
&nbsp;&nbsp;&nbsp;&nbsp;for (i=a ; i&lt;=b; i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (j=2; j&lt;i; j++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (i%j == 0) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (j==i-1) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n", j+1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}<br />
</code></p>
<p>First thing we see here is that we have two for loops. We can replace the for loops using preprocessor statement. This is a common time saving technique if you participate in online programming contests like Topcoder or SPOJ. Also let us remove {} for single line statements.</p>
<p><code><br />
#define FOR(i,a,b) for(i=a;i&lt;b;+i++)<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;int a, b, i, j;<br />
&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d", &#038;a);<br />
&nbsp;&nbsp;&nbsp;&nbsp;scanf("%d", &#038;b);<br />
&nbsp;&nbsp;&nbsp;&nbsp;FOR(i, a, b+1)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FOR(j, 2, i) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (i%j == 0) break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (j==i-1 &#038;&#038; printf("%d\n", i));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></p>
<p>Next step, we remove the return 0 statement and modify int main as simply main. Also we change all the integer variables we use in our code as global variables so that we need not defing their data type. Another modification that can be done here is to replace 2 by TWO and have a preprocessor statement to define TWO. This preprocessor statement is placed in between the for statement, just before we need the value 2. </p>
<p>Next modification is to is to remove the if else and have it in a single if statement. The idea used here is short-circuiting, so that there is no need for the else statement. </p>
<p><code><br />
a,b,i,j,t;main(){<br />
scanf("%d",&#038;a);<br />
scanf("%d",&#038;b);<br />
#define FOR(i,a,b) for(i=a;i&lt;b;+i++)<br />
FOR(i,a,b+1) FOR(j,<br />
#define TWO 2<br />
TWO,i)<br />
if (i%j==0||(j==i-1&#038;&#038;printf("%d\n",i)))break;}<br />
</code></p>
<p>Next we see that #define TWO 2 is straight forward. So modify that as shown below. The catch here is that you can have any statement which starts with 2 and has any non digit character following the 2. That way sscanf will just store the value of 2 to TWO. </p>
<p><code><br />
a,b,i,j,t; main(){<br />
scanf("%d",&#038;a);<br />
scanf("%d",&#038;b);<br />
#define FOR(i,a,b) for(i=a;i&lt;b;+i++)<br />
FOR(i,<br />
#define TWO ({sscanf("2 is the only even prime number. Do you know that?", "%d",&#038;t);t;})<br />
a,b+1) FOR(j,TWO,i) if(!(i%j)||(j==i-1&#038;&#038;printf("%d\n",i)))break;}<br />
</code></p>
<p>Finally, we see that we have 2 scanfs and 1 sscanf. The tail part of these functions are similar. Like<br />
scanf(&#8220;%d&#8221;,&#038;a);<br />
scanf(&#8220;%d&#8221;,&#038;b);<br />
sscanf(&#8220;2 is the only even prime number. Do you know that?&#8221;, &#8220;%d&#8221;,&#038;t);</p>
<p>In the above three statements, we see that &#8220;%d&#8221;, &#038;variable); is common. So add one preprocessor statement to replace that as shown below. Rename the variables to single characters. I have used upper case here. ;-). </p>
<p><code><br />
#define _$_ "%d",&#038;I);I;})<br />
Y,A,Z,H,I;main() {<br />
#define __$__ ({ int I; scanf(_$_<br />
#define FOR(_,__,___)for(_=__;_&lt;___;+_++)<br />
Y=__$__,A=__$__;FOR(Z,<br />
#define T ({sscanf("2 is the only even prime number. Do you know that?", _$_<br />
Y,A+T/2)FOR(H,T,Z)if(!(Z%H)||(H==Z-1&#038;&#038;printf("%d\n",Z)))break;}<br />
</code></p>
<p>You can download all the C files as a single zip file <a href="http://sp2hari.com/files/obfuscate-3.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2008/12/14/obfuscate-c-code-part-iii/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Obfuscate C Code &#8211; Part II</title>
		<link>http://sp2hari.com/2008/11/28/obfuscate-c-code-part-ii/</link>
		<comments>http://sp2hari.com/2008/11/28/obfuscate-c-code-part-ii/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 02:10:25 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Obfuscation]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=163</guid>
		<description><![CDATA[Hi all, this is my second post on C code obfuscation. You can check out all my posts on obfuscation here. In this post, we&#8217;ll see two techniques which one can use in C code obfuscation. The more we apply these to our code, the more obfuscated our code will result in. And of course,]]></description>
			<content:encoded><![CDATA[<p>Hi all, this is my second post on C code obfuscation. You can check out all my posts on obfuscation <a href="http://sp2hari.com/tag/obfuscation/">here</a>. </p>
<p>In this post, we&#8217;ll see two techniques which one can use in C code obfuscation. The more we apply these to our code, the more obfuscated our code will result in. And of course, this is not the complete guide for obfuscation. Just the two important techniques I find interesting. :-)</p>
<p><strong><u>Recursion</u></strong><br />
The first one is recursion. Yes recursion. :-) Well, before we start recursion we should know this. &#8220;To iterate is human, but to recurse is divine.&#8221; ;-). The great point about recursion is that it can make any simple code complicated. Before I explain what I&#8217;m going to do, let me post the code final code here.</p>
<p><code>T<br />
=__LINE__;<br />
#define FF T*T*T*T*T+T*T*T+T*T*T*T-T/T<br />
char *___="He that falls in love with himself will have no rivals.";_$_(int _,int __){(__-_==(T/T)?printf("%c",*(___+_)):(_$_(_,(__-_)/T+_),_$_((__-_)/T+_, __)));}main(){_$_(T-T, FF);}</code></p>
<p>The code simply prints the quote â€œHe that falls in love with himself will have no rivals.â€, quoted by Benjamin Franklin.</p>
<p>So let us start with a simple code which does that.<br />
<code>#include &lt;stdio.h&gt;<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;printf("He that falls in love with himself will have no rivals.");<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></p>
<p>Now, let us see how to introduce recursion into this. You can say, to print a string, first print the first half of the string and then the second half of the string. Got it? Yes, it is like this</p>
<p>Print the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;Print the first half of the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Print the first half of the first half of the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Print the second half of the first half of the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;.<br />
&nbsp;&nbsp;&nbsp;&nbsp;Print the second half of the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Print the first half of the second half of the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Print the second half of the second half of the string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;</p>
<p>After we introduce recursion into this logic, we get a code similar to the following. Again, the code can be made further complicated. Just remember, there is no limit to obfuscation. </p>
<p><code>#include &lt;stdio.h&gt;<br />
char *str = "He that falls in love with himself will have no rivals.";<br />
void print(int start, int  end) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (end-start==1) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%c", *(str+start));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(start, (end-start)/2+start);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print((end-start)/2+start, end);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;print(0, 55);<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}<br />
</code></p>
<p>Next, we remove the 2 in the else part of the code using #define. I use __LINE__ in this example. :). So now the code looks like this. </p>
<p><code>#include &lt;stdio.h&gt;<br />
t=__LINE__;<br />
char *str = "He that falls in love with himself will have no rivals.";<br />
void print(int start, int  end) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (end-start==1) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%c", *(str+start));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(start, (end-start)/t+start);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print((end-start)/t+start, end);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;print(0, 55);<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></p>
<p>Next, we remove the if else in the print statement and have a single conditional statement. That changes the whole picture in the print function, as shown below. </p>
<p><code>#include &lt;stdio.h&gt;<br />
t=__LINE__;<br />
char *str = "He that falls in love with himself will have no rivals.";<br />
void print(int start, int  end) { (end-start==1?printf("%c", *(str+start)):(print(start, (end-start)/t+start),print((end-start)/t+start, end))); }<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;print(0, 55);<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></p>
<p>Finally we change all the variable names so that they have only _&#8217;s and remove unnecessary space. Also you can change the value 55 to as 32+16+8-1 (all of these generated only using the previously used 2). So this is the final code. :)</p>
<p><code>T<br />
=__LINE__;<br />
#define FF T*T*T*T*T+T*T*T+T*T*T*T-T/T<br />
char *___ = "He that falls in love with himself will have no rivals.";_$_(int _,int __){(__-_==1?printf("%c", *(___+_)):(_$_(_,(__-_)/T+_),_$_((__-_)/T+_, __)));}main(){_$_(T-T, FF);}<br />
</code></p>
<p><strong><u>Preprocessor Statements</u></strong><br />
The second technique I find interesting is using #ifdef, #else, #endif and #include. You can make any code obfuscated without much programming.</p>
<p>Again, let us take the same simple code to explain the usage of preprocessor statements. Though it is much more effective when you apply this to an obfuscated code. </p>
<p>So the sample code with which we start will be </p>
<p><code>#include &lt;stdio.h&gt;<br />
int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;printf("He that falls in love with himself will have no rivals.");<br />
&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />
}</code></p>
<p>The resultant obfuscated code will be </p>
<p><code><br />
#ifdef T1<br />
&nbsp;&nbsp;&nbsp;&nbsp;#ifdef T2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#ifdef T3<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#ifdef T4<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;//Line 5<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#define T4<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;&nbsp;&nbsp;&nbsp;&nbsp;//Line 4<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include __FILE__<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#endif&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#define T3<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("He that falls in love with himself will have no rivals.");&nbsp;&nbsp;&nbsp;&nbsp;//Line 3<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include __FILE__<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#endif<br />
&nbsp;&nbsp;&nbsp;&nbsp;#else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#define T2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int main() { //Line 2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include __FILE__<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;#endif<br />
#else<br />
&nbsp;&nbsp;&nbsp;&nbsp;#define T1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include &lt;stdio.h&gt; //Line 1<br />
&nbsp;&nbsp;&nbsp;&nbsp;#include __FILE__<br />
#endif</code></p>
<p>I guess I can&#8217;t explain much on this. This is one simple concept in which preprocessor complies selected lines and then the same file is included again. :) If the above code is confusing, read <a href="http://www.phanderson.com/C/preprocess.html">this article</a> on preprocessing and then try some simple code using this idea. Then this code will be a cakewalk. </p>
<p>You can download all the C files as a single zip file <a href="http://sp2hari.com/files/obfuscate-2.zip">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2008/11/28/obfuscate-c-code-part-ii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Obfuscate C Code &#8211; Part I</title>
		<link>http://sp2hari.com/2008/11/26/obfuscate-c-code-part-i/</link>
		<comments>http://sp2hari.com/2008/11/26/obfuscate-c-code-part-i/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 09:07:09 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Obfuscation]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=162</guid>
		<description><![CDATA[Hello, this is the first post I&#8217;m writing on C code obfuscation. Let me warn you before I start. I am a beginner in code obfuscation and still learning from different code snippets I get from my friends. Since this is the first post in this series, we will start with hello world. But instead]]></description>
			<content:encoded><![CDATA[<p>Hello, this is the first post I&#8217;m writing on C code obfuscation. Let me warn you before I start. I am a beginner in code obfuscation and still learning from different code snippets I get from my friends. </p>
<p>Since this is the first post in this series, we will start with hello world. But instead of printing the same old hello world, we will print the quote &#8220;He that falls in love with himself will have no rivals.&#8221;, quoted by Benjamin Franklin.</p>
<p>We need a simple base program to start with right? The following code prints the quote.</p>
<p><code>#include <stdio.h><br />
int main() {<br />
	char s[123] = "He that falls in love with himself will have no rivals.";<br />
	printf("%s", s);<br />
	return 0;<br />
}</code></p>
<p>Surely, that is not the obfuscated code. First, let us convert the string into character array and instead of printing the string, we print the character array. Simple right? So the code now looks like this.</p>
<p><code>#include <stdio.h><br />
int main() {<br />
	int i;<br />
	char s[] = {'H','e',' ','t','h','a', 't', ' ', 'f', 'a', 'l', 'l', 's', ' ', 'i', 'n', ' ', 'l', 'o', 'v', 'e', ' ', 'w', 'i', 't', 'h', ' ', 'h', 'i', 'm', 's', 'e', 'l', 'f', ' ', 'w', 'i', 'l', 'l', ' ', 'h', 'a', 'v', 'e', ' ', 'n', 'o', ' ', 'r', 'i', 'v', 'a', 'l', 's', '.'};<br />
	for (i=0; i&lt;55; i++) {<br />
		printf("%c", s[i]);<br />
	}<br />
	return 0;<br />
}<br />
</code></p>
<p>Next step, we take the two variables &#8220;i&#8221; and &#8220;s&#8221; outside the main. Since now the two variables are in global scope, if we don&#8217;t define the data type, it is an integer by default. Since we can define a character array as an integer array, we need not provide the datatype for both the variables. So now the code looks like this. Also we can change &#8220;int main&#8221; to &#8220;main&#8221; and remove the &#8220;return 0;&#8221; statement. </p>
<p>Note:: This idea of not defining the data type shouldn&#8217;t be used if you are practising for IOCCC. In there, the guidelines clearly states the following.<br />
<em>    When declaring local or global variables, you should declare the type:</p>
<p>	int this_is_ok;<br />
	this_is_not;	   <-- don't use such implicit type declarations</em></p>
<p><code>i, s[] = {'H','e',' ','t','h','a', 't', ' ', 'f', 'a', 'l', 'l', 's', ' ', 'i', 'n', ' ', 'l', 'o', 'v', 'e', ' ', 'w', 'i', 't', 'h', ' ', 'h', 'i', 'm', 's', 'e', 'l', 'f', ' ', 'w', 'i', 'l', 'l', ' ', 'h', 'a', 'v', 'e', ' ', 'n', 'o', ' ', 'r', 'i', 'v', 'a', 'l', 's', '.'};<br />
main() {<br />
	for (i=0; i&lt;55; i++) {<br />
		printf("%c", s[i]);<br />
	}<br />
}</code></p>
<p>Next, we remove the 55 to the size of the array. We need to replace 55 by &#8220;sizeof(s)/sizeof(int)&#8221;. Let us do that in a better way as shown below. The two preprocessor statements added below to replaces ck by sizeof(s)/sizeof(int). The point to note here is that we have added the preprocessor statements in between the for statement.</p>
<p><code><br />
i, s[] = {'H','e',' ','t','h','a', 't', ' ', 'f', 'a', 'l', 'l', 's', ' ', 'i', 'n', ' ', 'l', 'o', 'v', 'e', ' ', 'w', 'i', 't', 'h', ' ', 'h', 'i', 'm', 's', 'e', 'l', 'f', ' ', 'w', 'i', 'l', 'l', ' ', 'h', 'a', 'v', 'e', ' ', 'n', 'o', ' ', 'r', 'i', 'v', 'a', 'l', 's', '.'};<br />
main() {<br />
	for (i=0;<br />
	#define f(z) sizeof(z)<br />
	#define ck f(s)/f(int)<br />
	i&lt;ck; i++) {<br />
		printf("%c", i[s]);<br />
	}<br />
}</code></p>
<p>Since i is a global variable, we need not initialize the value to 0. Again, we can remove the i++ statement by changing s[i] to s[i++]. And also note that a[i] and i[a] are both the same. So we can use i++[s]. So the code now becomes</p>
<p><code>i, s[] = {'H','e',' ','t','h','a', 't', ' ', 'f', 'a', 'l', 'l', 's', ' ', 'i', 'n', ' ', 'l', 'o', 'v', 'e', ' ', 'w', 'i', 't', 'h', ' ', 'h', 'i', 'm', 's', 'e', 'l', 'f', ' ', 'w', 'i', 'l', 'l', ' ', 'h', 'a', 'v', 'e', ' ', 'n', 'o', ' ', 'r', 'i', 'v', 'a', 'l', 's', '.'};<br />
main() {<br />
	for (;<br />
	#define f(z) sizeof(z)<br />
	#define ck f(s)/f(int)<br />
	i&lt;ck;<br />
	)<br />
		printf("%c", i++[s]);<br />
}</code></p>
<p>Next, we remove the extra spaces so that the code is &#8220;poorly&#8221; aligned, as shown below. Also, now, we can replace the variable names by _ and __ etc. I haven&#8217;t done much to the character array here. That is not the aim of this post. Will write a separate post on manipulating strings. But for now, we can convert the character array to hexadecimal or octal numbers. ;-) (This is not the best way to obfuscate it though). </p>
<p>So the final code looks something like this. </p>
<p><code>#define ____(z) sizeof(z)<br />
_, __[] = {0x48, 0x65, 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x66, 0x61, 0x6c, 0x6c, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x68, 0x69, 0x6d,0x73, 0x65, 0x6c, 0x66, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20,0x68, 0x61, 0x76, 0x65, 0x20, 0x6e, 0x6f, 0x20, 0x72, 0x69, 0x76, 0x61, 0x6c, 0x73, 0x2e}; main() { for (;<br />
#define ___ ____(__)/____(int)<br />
_&lt;___;) printf("%c", _++[__]);}</code></p>
<p>All suggestions on still screwing up this code are welcome. ;-)</p>
<p>Update: You can download all the C files as a single zip file <a href="http://sp2hari.com/files/obfuscate-1.zip">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2008/11/26/obfuscate-c-code-part-i/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Forgot the match again?</title>
		<link>http://sp2hari.com/2008/07/10/forgot-the-match-again/</link>
		<comments>http://sp2hari.com/2008/07/10/forgot-the-match-again/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 14:15:22 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[opc]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[topcoder]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=139</guid>
		<description><![CDATA[Do you play Topcoder? If yes, then there might be something for you in this post. If you don&#8217;t know what topcoder is, check http://topcoder.com/tc and then come back here. If you play topcoder, then keeping track of when the next match is a big headache. There are many many people who login to the]]></description>
			<content:encoded><![CDATA[<p>Do you play Topcoder? If yes, then there might be something for you in this post. If you don&#8217;t know what topcoder is, check <a href="http://topcoder.com/tc">http://topcoder.com/tc</a> and then come back here.</p>
<p>If you play topcoder, then keeping track of when the next match is a big headache. There are many many people who login to the arena one day before the contest and ask why there is not many playing today.  Come on, it is not funny to get up at 5.30 in the morning to know that the match is not today and it is tomorrow. The rest think the match is one day later, who curse themselves, bang their head for missing a match. If you want to forget forgetting :P about topcoder matches, follow the simple steps given below. </p>
<p>All you need is a Google Account. </p>
<ul>
<li>Login to google calendars at <a href="http://calendar.google.com/">http://calendar.google.com/</a></li>
<li>Search for &#8220;Topcoder&#8221; in public calendars. ( By clicking &#8220;Search Public Calendars&#8221; button). I got some 50 results when I wrote this. This may increase or decrease later. Choose anything from the list (anything from the first 5 will be good enough).</li>
<li>Click on the &#8220;Add to Calendar&#8221; button.</li>
<li>Go back to your calendar page. You can see the Topcoder calendar in the &#8220;Other calendars&#8221;.</li>
<li>You need to activate mobile setup by activating your phone number. This takes less than a minute. Give your country and phone number. Google will send you a verification code in 1 or 2 minutes. Type that validation code there and voila, you are done with the mobile setup. :-)</li>
<li>Now click on the &#8220;Calendars&#8221; tab in the settings page. </li>
<li>Click the &#8220;Notification&#8221; of your new Topcoder calendar.</li>
<li>Click the &#8220;Add a reminder&#8221; to add new remainders. </li>
<li>You can have upto 5 remainders for this calendar.</li>
</ul>
<p>See my notifications page here.<br />
<a href='http://sp2hari.com/wp-content/uploads/2008/07/topcoder.jpg'><img src="http://sp2hari.com/wp-content/uploads/2008/07/topcoder.jpg" alt="Google Calendar Notification" title="Google Calendar Notification" width="500" height="165" class="alignnone size-full wp-image-141" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2008/07/10/forgot-the-match-again/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Seg Fault</title>
		<link>http://sp2hari.com/2007/05/03/seg-fault/</link>
		<comments>http://sp2hari.com/2007/05/03/seg-fault/#comments</comments>
		<pubDate>Thu, 03 May 2007 08:00:00 +0000</pubDate>
		<dc:creator>sp2hari</dc:creator>
				<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[segfault]]></category>
		<category><![CDATA[technical]]></category>

		<guid isPermaLink="false">http://sp2hari.com/?p=36</guid>
		<description><![CDATA[Wikipedia says,A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not]]></description>
			<content:encoded><![CDATA[<p>Wikipedia says,<br />A <a href="http://en.wikipedia.org/wiki/Segmentation_fault"><span style="font-weight: bold;">segmentation fault</span></a>  (often shortened to <b>segfault</b>) is a particular error condition that can occur during the operation of <a href="http://en.wikipedia.org/wiki/Computer_software" title="Computer software">computer software</a>. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).&#8221;</p>
<p>I have seen hundreds and hundreds of seg faults ;-) while coding for record module of <a href="http://ldtp.freedesktop.org/">LDTP</a> and Spider SMS. But the one i saw yesterday was new, strange and fascinating. I am not sure whether i will be able to reproduce it again. The screenshot says why it is strange and fascinating :-) .</p>
<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp2.blogger.com/_7owHVJdIzX8/RjmX9bSaGlI/AAAAAAAAArw/mJl5A_goeGE/s1600-h/Screenshot.png"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://bp2.blogger.com/_7owHVJdIzX8/RjmX9bSaGlI/AAAAAAAAArw/mJl5A_goeGE/s320/Screenshot.png" alt="" id="BLOGGER_PHOTO_ID_5060242737880242770" border="0" /></a></p>
<p>-bash-3.1$ man su<br /><span style="font-style: italic;">says </span></p>
<p>AUTHOR<br />       Written by David MacKenzie.</p>
<p>REPORTING BUGS<br />       Report bugs to &lt;bug-coreutils@gnu.org&gt;.</p>
<p>Maybe i should consider reporting this :P .</p>
]]></content:encoded>
			<wfw:commentRss>http://sp2hari.com/2007/05/03/seg-fault/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
