在某ctf网站看到一个比较有意思的题目,题目大概的意思是在一个随机的生成的字母文本里被插了一句有意义的英文句子,由于文本比较大靠人眼去找基本没可能。
解决思路如下:
1.到网上找一个常用的英文单词表做成字典,不要太大基本常用的2000-3000个就够用用了
2.记录字典里每个词在随机文本里的出现位置后再排序去重
3.假设句子的单词个数不少12,最大的单词长度不大于10,这样的话最后可以筛选出符合条件的句子
随便写了个程序跑了下,大概10来秒就可以出结果了,最终大概之后剩下40个结果,一眼就知道结果了.
import os
import time
def FindIndex(index,IndexList,FindedCount,PosList):
if(FindedCount<WordCount):
if (int(IndexList[index+1])-int(IndexList[index]))<WordMaxLen:
FindedCount+=1
index+=1
FindIndex(index,IndexList,FindedCount,PosList)
else:
PosList.append(int(IndexList[index]))
time.clock()
path= os.path.split(os.path.realpath(__file__))[0]
ts=open(path+"/find.txt") #需要查找的文件
text_all=ts.read()
dict=open(path+"/dic.txt") #字典文件
output=open(path+"/output.txt",'w') #输出文件
hash=[]
dic=[]
line = dict.readline()
while line:
dic.append(line)
line = dict.readline()
start = 0
for d in dic:
while True:
index = text_all.find(d.strip('\r\n'), start)
if index == -1:
start=0
break
output.write(str(index)+'\n')
start = index + 1
os.system('cat '+path+'/output.txt |sort -n |uniq >new.txt') #去重排序
new=open(path+"/new.txt")
IndexList=[]
PosList=[]
TotalCount=0
WordCount=12
FindedCount=0
WordMaxLen=9
line = new.readline()
while line:
IndexList.append(line)
line = new.readline()
TotalCount=TotalCount+1
for i in range(0,TotalCount-WordCount):
FindIndex(i,IndexList,FindedCount,PosList)#递归查找
print "Use time:%s" % time.clock() ,"find result ", len(PosList)
if(len(PosList)>0): #打印结果
i=0
for pos in PosList:
print i,' :',
for j in range(pos-21,pos+30):
print text_all[j],
print "\r\n"
i+=1
结果图:
其实还可以就结果以连续区间密度排序下,由于结果不多,就不排序了
只有48条结果,很快就能找到句子了