PythonChallenge系列解题步骤@Skiery
Python Challenge 9
Connect the dots
em,网页中没有任何提示,所以观察一下图片,发现上面有很多像素点,开始解题~
Solution 9
首先注意看了一下题目名字,叫做connect the dots,大概是把图中的点都连起来?先不管三七二十一,把代码爬下来再说(说实在的,大家要是懒就直接用之前爬clue的代码,或者F12打开手动粘贴)【注意,本题网页访问时输入了用户名和密码,爬取有点不便,这种爬取方法以后研究吧,先手动复制拿出来看看】
<!--
To see the solutions to the previous level, replace pc with pcc and add :un:pw
i.e. go to: http://www.pythonchallenge.com/pcc/return/good.html:huge:file
Join us at the IRC: irc.freenode.net #pythonchallenge
-->
<!--
first+second=?
first:
146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,
310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,
190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,
389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,
215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,
290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,
279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,
327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,
328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,
259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,
352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,
120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,
214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,
102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,
113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,
133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,
111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,
332,155,348,156,353,153,366,149,379,147,394,146,399
second:
156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,
157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,
125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,
77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,
158,121,157,128,156,134,157,136,156,136
-->
看到了提示是:first+second,并且得到了first和second,乍一看first比second长很多,len(first) = 442 ; len(second)=112;
看不出有什么特别,再回到题目中,发现所谓的连起这些点,em大概跟PIL有关,所以百度了一下,最后查到这些东西了,代码如下:
from PIL import Image,ImageDraw
img = Image.new('RGB',(600,600))
draw = ImageDraw.Draw(img)
draw.polygon(first,fill = 'white') #利用draw.polygon函数对img进行绘制,将元组中的二维坐标全都连起来
draw.polygon(second,fill = 'yellow') #再画一次
img.show()
蹡蹡,得到下面的图形咯:
所以,clue应该…牛? buffalo…不是,beef…不是,ox?…不是,bull…好吧,就是为了烘托一下气氛,最后试出来是cow,bingo,进入下一题~
Python Challenge 10
Solution 10
看一下网页中的提示,是len(a[30]) = ?
,然后看一下网页源码,得到下面结果:
<map name="bull">
<area shape="poly" coords="146,399,163,403,170,393,169,391..."
href="sequence.txt" />
访问一下href这个后面跟的奇怪的txt,发现下面这个序列:
a = [1, 11, 21, 1211, 111221, #看起来好像是一道leetcode题···
CPP递归
class Solution {
public:
string countAndSay(int n)
{
if(n==1)
{
return "1";
}
string temp;
string init="1";
for(int i=0;i<n-1;i++)
{
for(int j=0;j<init.length();j++)
{
int count=0;
while(init[j]==init[j+1])
{
count++;
j++;
}
temp+=to_string(count+1);
temp+=init[j];
}
init=temp;
temp.clear();
}
return init;
}
};
该递归方式还需要改,我们使用python的正则表达式去匹配表述规则:
import re
x = "1"
for i in range(30):
x = "".join([str(len(y)+1)+x, for x,y in re.findall(r"(\d)(\1*)",x)]) # x,y分别对应正则表达式匹配的相同的两个值(即相同序列串的第一个和剩下的部分)
##正则表达式说明,(\d)对数字进行匹配,(\1)在此处是对(\d)进行捕获并继续匹配
len(x)
最终得到flag:len(x) = 5808
,修改url进入下一题
Python Challenge 11
odd even
网页没有提示,只有一张影影绰绰的图片,题目叫odd even,猜想跟奇偶性有关,先看看源网页信息,发现里面没有任何有用的信息量,所以只能从图片里找信息了
Solution 11
考虑到按图元素坐标的奇偶性去划分元素的集合,最终得到想要的flag,可能的分法有:取奇数行和偶数列相交的元素;或者直接将元素两坐标和的奇偶性去分…最终发现是后者,代码如下:
from PIL import Image
img = Image.open("C:\\Users\\1\\Desktop\\cave.jpg") #打开下载的图片
img_odd = Image.new("RGB",(img.width//2,img.height//2)) #新建变量保存坐标和为奇数的像素点
img_even = Image.new("RGB",(img.width//2,img.height//2)) #新建变量保存坐标和为偶数的像素点
for i in range(img.width):
for j in range(img.height):
if (i+j)%2 == 1:
img_odd.putpixel((i//2,j//2),img.getpixel((i,j)))
else:
img_even.putpixel((i//2,j//2),img.getpixel((i,j)))
img_odd.show()
img_even.show() #结果发现图中flag为evil
输入得到的flag,进入下一题~
Python Challenge 12
Dealing Evil
同样的,网页和源网页中没有任何提示信息,按照上一题的处理方式进行处理也没有任何用,所以考虑一下其他方式。
Solution 12
搞了半天没什么头绪,好吧,看一下攻略,发现直接调整evil1.jpg为evil2.jpg就能看到线索···
好气···
改吧,之后看到线索是这样的:
图中写了,not jpg – .gfx,按他的意思试一下evil1 和evil2 和 evil,发现下载得到evil2.gfx,接着看evil3,发现说no more jpg,再看一下提示,百度一下jpg和gfx的关系,没发现什么东西,再看看攻略和图片,发现从图evil1中的线索数字‘5’得知,应该一共有5张图,这五张图应该是一起压缩在gfx文件里面的···所以···用二进制格式读取gfx文件,分五次wb写入五个不同的jpg文件,得到下一题的线索(总觉得解题思路越来越抽象了···),代码如下:
data = open("filepath","rb").read() #以二进制格式读取gfx文件
for i in range(5): #从第一张图中获得的线索,这样的jpg图片一共有五张
open('%d.jpg' % i ,'wb').write(data[i::5])
然后就能得到五张jpg格式的图片如下:
其中第三张图有损坏,自己只能看到一半的预览,大家大概猜猜,得到最终的flag:disproportional,输入线索,进入下一题
Python Challenge 13
call him
观察一下网页,发现5是可以点的超链接,打开发下如下代码:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value>
<int>105</int>
</value>
</member>
<member>
<name>faultString</name>
<value>
<string>
XML error: Invalid document end at line 1, column 1
</string>
</value>
</member>
</struct>
</value>
</fault>
</methodResponse>
然后网页的源代码看起来有用的大概这么多··
<center>
<img src="disprop.jpg" width="640" height="480" border="0" usemap="#evil" />
<map name="evil">
<area shape="circle" coords="326,177,45" href="../phonebook.php" />
</map>
<font color="gold"/>
<br><b>
phone that <remote /> evil
OK,开始解题
Solution 13
百度之后,发现我们可以使用xmlrpc这个框架传输复杂数据,或者实现对象的远程调用,
import xmlrpc.client
conn = xmlrpc.client.ServerProxy("http://www.pythonchallenge.com/pc/phonebook.php")
之后看一下我们得到的可以调用的方法:
conn.system.listMethods()
['phone', 'system.listMethods', 'system.methodHelp', 'system.methodSignature', 'system.multicall', 'system.getCapabilities']
回忆之前的源网页提示phone that <remote /> evil
,自然而然的尝试调用phone函数,先看一下phone的用法:
>>> conn.system.methodHelp("phone")
'Returns the phone of a person'
>>>conn.phone("evil")
'He is not the evil'
为此还重新回到上一道题,看了好久搞清楚谁是evil…,在evil3.jpg里显示no more evils的时候,接着访问evil4.jpg没有显示东西,但是也不是404报错,所以改用ie浏览器重新访问页面,会看到evil是谁的提示:
Bert is evil! go back! #这个是在上一道题challenge 12里面进行的,这一步影响challeng 13的解题,但是并不影响上一题本身解题
理所应当的:
>>> conn.phone("Bert")
'555-ITALY'
尝试一下两个合起来,和分开,发现flag就是ITALY,进入下一题
在此,非常感谢hackingnote提供的一些解题思路,让我迷茫的时候有路可寻,希望一周内能有所收获地刷完pythonhacking~