我尝试从Fast and Furious质询中解决名为Break In 2017的任务。
任务很简单。需要HTTP回发HTML页面中包含的数学表达式的结果。它可以通过这个python脚本来实现:
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)
我想在C#中做同样的事情。我试过这样:
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, @"\(.*\)");
}
}
}
但它不起作用,因为我总是回到'1级'问题。我使用HttpWebRequest.CookieContainer属性在不同的请求中重用cookie以保持会话。
我不知道是什么问题。也许会议不起作用。 或许HttpWebRequest太慢,无法及时回发结果。
如何用C#解决这个自动化任务?
答案 0 :(得分:0)
您的代码未经处理重定向。观察流量,我们可以看到这个特定的服务器希望你的请求/ fastandfurious以尾随斜杠结束。所以改变' ../ fastandfurious'到' ../ fastandfurious /'这将解决它。