Pages

Categories

C++ Obfuscated code

C++ Obfuscated code
Published on 11 March, 2014
while on IRC, some one linked this piece of C++ code:
Selec All Code:
1
int main()<%<:]{%>();%>
It’s a perfectly valid piece of code too! It compiles, and as expected has no output. But how
does it work? It was puzzling at first, until they shared this piece of code:
Selec All Code:
1
2
3
4
%:include <iostream>
using namespace std;
int main()<%<:]{cout<<“Hello World”;%>();%>
Then it started to make sense. The give away was the %:include line. It’s a digraph,
characters which when combined together are treated as a different character at compilation
time. Reasons behind why digraphs exist is explained in this stackoverflow post.
So to deobfuscate it would have the source code of:
Selec All Code:
1
2
3
4
5
6
7
8
9
10
#include
using namespace std;
int main()
{
[]
{
cout<<“Hello World”;
}();
}
Within the main block of code, is a lambda function that gets called with no parameters,
executing the standard output statement.
The ISO C++ 14882 2011 has a section on digraphs at 2.6 “Alternative tokens”, and in ISO C
9899 2011 at 6.4.6-3 “Punctuators”.
Filed in Programming | No replies