Capture the Flag

Capture the Flag

by invalid
#!/usr/bin/bash

cat << __FINI__ | gcc -Wall -s -o bliss -x c -
/**
 * level: easy
 *  flag: FLAG{54t4t4_64m4_K4rt4_R4h4rdj4}
 *   p/s: sorry kodenya kacau :(
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int fn_chk6(char *v1)
{
    if (v1[14] != 'm') return 1;
    if (v1[25] != 'h') return 1;
    if (v1[17] != 'K') return 1;
    if (v1[23] != 'R') return 1;
    if (0 != strncmp(v1 + 27, "rdj", 3)) return 1;
    if (0 != strncmp(v1 + 19, "rt", 2)) return 1;
    return 0;
}

int fn_chk5(char *v1)
{
    int i;
    static const int b[] = {9, 20, 7};

    for (i = 0; i < 3; i++)
    {
        if (v1[b[i]] != 't') return 1;
    }
    return 0;
}

int fn_chk4(char *v1)
{
    int i;
    static char a[2];
    static const int b[] = {26, 15, 8, 21, 18, 10, 30, 24};

    for (i = 0; i < 8; i++)
    {
        a[0] = v1[b[i]];
        a[1] = '\x00';

        if (0x04 != atoi(a)) return 1;
    }
    return 0;
}

int fn_chk3(char *v1)
{
    static char a[3];

    a[0] = v1[12];
    a[1] = v1[13];
    a[2] = '\x00';

    if (0x40 != atoi(a)) return 1;
    return 0;
}

int fn_chk2(char *v1)
{
    static char a[3];

    a[0] = v1[5];
    a[1] = v1[6];
    a[2] = '\x00';

    if (0x36 != atoi(a)) return 1;
    return 0;
}

int fn_chk1(char *v1)
{
    if (v1[31] != '}') return 1;
    return (strncmp(v1, "FLAG{", 5));
}

int fn_chk0(char *v1)
{
    if (strlen(v1) != 32) return 1;
    return 0;
}


int main(int argc, char *argv[])
{

    if (argc < 2) {
        printf("%s <flag>\n", argv[0]);
        exit(1);
    }

    if (0 != fn_chk0(argv[1])) goto hell;
    if (0 != fn_chk1(argv[1])) goto hell;
    if (0 != fn_chk2(argv[1])) goto hell;
    if (0 != fn_chk3(argv[1])) goto hell;
    if (0 != fn_chk4(argv[1])) goto hell;
    if (0 != fn_chk5(argv[1])) goto hell;
    if (0 != fn_chk6(argv[1])) goto hell;
    if (argv[1][31] != '}') goto hell;
    puts("Congratulation! :)");
    return 69;

hell:
    puts("Wrong flag :(");
    return 1;
}
__FINI__

echo "[i] Testing..."

./bliss FLAG{54t4t4_64m4_K4rt4_R4h4rdj4}

if [ [ $? == 69 ] ]; then
    echo "[+] Test passed..."
else
    echo "[x] Test FAILED!"
fi