Search code examples
c#.netsessioncookieshttpwebrequest

How to solve the task called 'Fast and Furious' from 'Break In 2017' challenge in C#?


I try to solve the task called Fast and Furious from Break In 2017 challenge.

The task is simple. Need to HTTP post back the result of a math expression contained in a HTML page. It can be achieved by this python script:

import requests
import re

url = 'https://felicity.iiit.ac.in/contest/extra/fastandfurious/'
s = requests.Session()
r = s.get(url)
print(r.text)
m = re.search('\(.*\)', r.text)
while m:
    ans = eval(m[0])
    print(m[0] + ' -> ' + str(ans))
    r = s.post(url, data={'ques_ans' : ans})
    print(r.text)
    if ('the_flag_is_' in r.text): break
    m = re.search('\(.*\)', r.text)

I want to do the same in C#. I tried like this:

using System;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
    static CookieContainer cookies = new CookieContainer();

    static HttpWebRequest Create(string url)
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = cookies;
        request.Accept = "*/*";
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        request.UserAgent = "dummy";
        return request;
    }

    static string Get(string url)
    {
        var request = Create(url);
        request.Method = "GET";

        using (var response = request.GetResponse())
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            return reader.ReadToEnd();
        }
    }

    static string Post(string url, string postData)
    {
        var request = Create(url);
        request.Method = "POST";

        var data = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        using (var response = request.GetResponse())
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            return reader.ReadToEnd();
        }
    }

    static string Eval(string expression)
    {
        DataTable dt = new DataTable();
        return dt.Compute(expression, "").ToString();
    }

    static void Main()
    {
        string url = "https://felicity.iiit.ac.in/contest/extra/fastandfurious";
        string text = Get(url);
        Console.WriteLine(text);
        var m = Regex.Match(text, @"\(.*\)");

        while (m.Success)
        {
            var ans = Eval(m.Value);
            Console.WriteLine(m.Value + " -> " + ans);
            text = Post(url, "ques_ans=" + ans);
            Console.WriteLine(text);
            if (text.Contains("the_flag_is_")) break;
            m = Regex.Match(text, @"\(.*\)");
        }
    }
}

But it does not work, because I always get back the 'Level 1' question. I used HttpWebRequest.CookieContainer property to reuse the cookies across different requests to keep up a session.

I don't know what is the problem. Maybe the session doesn't work. Or perhaps HttpWebRequest is too slow to post back the result in time.

How to solve this automation task in C#?


Solution

  • Your code isn't handling being redirected. Watching the traffic we can see that this particular server would like your requests to /fastandfurious to end with a trailing slash. so change '../fastandfurious' to '../fastandfurious/' and that will fix it.