CTF【每日一題20160608】
阿新 • • 發佈:2019-01-05
簡單程式設計-字元統計
你會看到題目,要快速提交答案哦,重新整理後內容會變,所以編個程式讀網頁,再計算。
-------------------------------------------------------------------------------------------------
分析:
發現網上有人解決的不錯,我就不多言了。
網上的解法1:【java程式實現】
一開始以為只要簡單統計好了,後來一看,發現竟然寫著要2秒內提交。。。好吧,沒注意到,於是決定用HttpWebRequest來完成。用httpwatch抓包,出來個post提交的資料是answer=?,怪我功底不好,不論怎麼改程式碼,資料都post不上去啊。。。鬱悶了
想到之前做過一個軟體,用的是webBrowser,試試看吧,程式碼如下:
<pre name="code" class="java">using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Web; using System.Net; using System.IO; namespace 簡單字元統計 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string getWord(string str) { int w = 0, o = 0, l = 0, d = 0, y = 0; char[] ch = str.ToCharArray(); for(int i = 0; i < str.Length; i++) { if (ch[i] == 'w') w++; if (ch[i] == 'o') o++; if (ch[i] == 'l') l++; if (ch[i] == 'd') d++; if (ch[i] == 'y') y++; } return w + "" + o + "" + l + "" + d + "" + y; } private void button2_Click(object sender, EventArgs e) { webBrowser1.Navigate("http://ctf.idf.cn/game/pro/37/index.php"); } public int flag = 1; private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { StreamReader getReader = new StreamReader(this.webBrowser1.DocumentStream, System.Text.Encoding.GetEncoding("gb2312")); string gethtml = getReader.ReadToEnd(); int a = gethtml.IndexOf("<hr />"); int b = gethtml.LastIndexOf("<hr />"); gethtml = gethtml.Substring(a + 6, b - a - 6); HtmlDocument web = webBrowser1.Document; //獲取web頁面控制元件 HtmlElement Anwser = web.All["anwser"]; Anwser.SetAttribute("value", getWord(gethtml)); HtmlElementCollection bn = webBrowser1.Document.GetElementsByTagName("input");//填充 foreach (HtmlElement btn in bn)//接管按鈕 { if (btn.GetAttribute("value") == "走你!" && flag==1) { btn.InvokeMember("click"); flag = 0; } } } } }
成功了,感動得作者熱淚盈眶。
提交,通過!
-------------------------------------------------------------------------
解法2 ,用python程式計算
參考http://blog.csdn.net/u012241633/article/details/45766281的解法,小改一了下。
這個程式不僅要抓網頁內容、用post方法提交request、獲取response,還特別要注意cookie的使用,伺服器用cookie來區別使用者,所以提交時要驗證cookie的,不匹配的任務不是你做的。
# -*- coding: utf-8 -*- import urllib import urllib2 import string import re import cookielib #需要提交post的url TARGET_URL = "http://ctf.idf.cn/game/pro/37/" ''' 不拿cookie不行 wp = urllib.urlopen(TARGET_URL) content = wp.read() ''' # 設定一個cookie處理器 req = urllib2.Request(TARGET_URL) cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) res = opener.open(req) content = res.read() ct = re.findall(r'<hr />(.*)<hr />',content,re.S)[0] print ct #看看抓到的內容對不對 #計算ct中的w,o,l,d,y wn = ct.count('w') on = ct.count('o') ln = ct.count('l') dn = ct.count('d') yn = ct.count('y') result = str(wn)+str(on)+str(ln)+str(dn)+str(yn) print result # 提交 ''' 網頁提交處原始碼 <form action="" method="post"> <p>答案: <input type="text" name="anwser" /></p> <input type="submit" value="走你!" /> </form> ''' value = {'anwser': result} #input name is 'anwser' data = urllib.urlencode(value) request = urllib2.Request(TARGET_URL,data) response = opener.open(request) #urllib2.urlopen(request)# r = response.read() f=file('response.html','w+') print >> f,r #之後看response.html中的key,提交即可