<p>Recently our site has been deluged with the resurgence of the <a href="https://en.wikipedia.org/wiki/Asprox_botnet" rel="noreferrer">Asprox botnet</a> <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="noreferrer">SQL injection</a> attack. Without going into details, the attack attempts to execute SQL code by encoding the <a href="http://en.wikipedia.org/wiki/Transact-SQL" rel="noreferrer">T-SQL</a> commands in an ASCII encoded BINARY string. It looks something like this:</p>

<pre><code>DECLARE%20@S%20NVARCHAR(4000);SET%20@S=CAST(0x44004500...06F007200%20AS%20NVARCHAR(4000));EXEC(@S);--
</code></pre>

<p>I was able to decode this in SQL, but I was a little wary of doing this since I didn't know exactly what was happening at the time.</p>

<p>I tried to write a simple decode tool, so I could decode this type of text without even touching <a href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server" rel="noreferrer">SQL&nbsp; Server</a>. The main part I need to be decoded is:</p>

<pre><code>CAST(0x44004500...06F007200 AS
NVARCHAR(4000))
</code></pre>

<p>I've tried all of the following commands with no luck:</p>

<pre><code>txtDecodedText.Text =
    System.Web.HttpUtility.UrlDecode(txtURLText.Text);
txtDecodedText.Text =
    Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(txtURLText.Text));
txtDecodedText.Text =
    Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(txtURLText.Text));
txtDecodedText.Text =
    Encoding.ASCII.GetString(Encoding.Unicode.GetBytes(txtURLText.Text));
txtDecodedText.Text =
    Encoding.Unicode.GetString(Convert.FromBase64String(txtURLText.Text));
</code></pre>

<p>What is the proper way to translate this encoding without using SQL Server? Is it possible? I'll take VB.NET code since I'm familiar with that too.</p>

<hr>

<p>Okay, I'm sure I'm missing something here, so here's where I'm at.</p>

<p>Since my input is a basic string, I started with just a snippet of the encoded portion - 4445434C41 (which translates to DECLA) - and the first attempt was to do this...</p>

<pre><code>txtDecodedText.Text = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(txtURL.Text));
</code></pre>

<p>...and all it did was return the exact same thing that I put in since it converted each character into is a byte.</p>

<p>I realized that I need to parse every two characters into a byte manually since I don't know of any methods yet that will do that, so now my little decoder looks something like this:</p>

<pre><code>while (!boolIsDone)
{
    bytURLChar = byte.Parse(txtURLText.Text.Substring(intParseIndex, 2));
    bytURL[intURLIndex] = bytURLChar;
    intParseIndex += 2;
    intURLIndex++;

    if (txtURLText.Text.Length - intParseIndex &lt; 2)
    {
        boolIsDone = true;
    }
}

txtDecodedText.Text = Encoding.UTF8.GetString(bytURL);
</code></pre>

<p>Things look good for the first couple of pairs, but then the loop balks when it gets to the "4C" pair and says that the string is in the incorrect format.</p>

<p>Interestingly enough, when I step through the debugger and to the GetString method on the byte array that I was able to parse up to that point, I get ",-+" as the result.</p>

<p>How do I figure out what I'm missing - do I need to do a "direct cast" for each byte instead of attempting to parse it?</p>

[{'answer_id': 304, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 3, 'selected': False, 'text': "<p>Try removing the <code>0x</code> first and then call <code>Encoding.UTF8.GetString</code>. I think that may work.</p>\n\n<p>Essentially: 0x44004500</p>\n\n<p>Remove the 0x, and then always two bytes are one character:</p>\n\n<pre><code>44 00 = D\n\n45 00 = E\n\n6F 00 = o\n\n72 00 = r\n</code></pre>\n\n<p>So it's definitely a Unicode/UTF format with two bytes/character.</p>\n"}, {'answer_id': 2585, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 6, 'selected': True, 'text': '<p>I went back to Michael\'s post, did some more poking and realized that I did need to do a double conversion, and eventually worked out this little nugget:</p>\n\n<pre><code>Convert.ToString(Convert.ToChar(Int32.Parse(EncodedString.Substring(intParseIndex, 2), System.Globalization.NumberStyles.HexNumber)));\n</code></pre>\n\n<p>From there I simply made a loop to go through all the characters 2 by 2 and get them "hexified" and then translated to a string.</p>\n\n<p>To Nick, and anybody else interested, I went ahead and <a href="http://www.codeplex.com/urldecoder" rel="nofollow noreferrer">posted my little application</a> over in <a href="http://en.wikipedia.org/wiki/CodePlex" rel="nofollow noreferrer">CodePlex</a>. Feel free to use/modify as you need.</p>\n'}]
['https://Stackoverflow.com/questions/109', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/71/']