CTF【每日一题20160608】

简单编程-字符统计

点这个链接 → http://ctf.idf.cn/game/pro/37

你会看到题目,要快速提交答案哦,刷新后内容会变,所以编个程序读网页,再计算。


-------------------------------------------------------------------------------------------------

分析:

发现网上有人解决的不错,我就不多言了。

网上的解法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,提交即可




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值