#!/usr/bin/python
#Searches msn with a user defined query then writes the results to a file. Also can add shell 
#location onto the end of the sites. This is alittle different than the goog2text scanner, it 
#finds the entire http address.

#http://www.darkc0de.com
#d3hydr8[at]gmail[dot]com

import sys, re, string, urllib2, sets

def StripTags(text):
    finished = 0
    while not finished:
        finished = 1
        start = text.find("<")
        if start >= 0:
            stop = text[start:].find(">")
            if stop >= 0:
                text = text[:start] + text[start+stop+1:]
                finished = 0
    return text
		  
def getsites():
	
	page_counter=0
	try:
		#Change this 50 to search for more sites.(multiples of 10)
    		while page_counter < int(sys.argv[2]):
        		results_web = 'http://search.msn.com/results.aspx?q='+str(query)+'&first='+repr(page_counter)
        		request_web = urllib2.Request(results_web)
        		request_web.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)')
        		opener_web = urllib2.build_opener()                           
        		text = opener_web.open(request_web).read()
        		names = re.findall(('\w+\.[\w\-]*\.[\w\.\-/\?\=]*'),StripTags(text))
        		for name in names:
				if name not in d:
					d.append(name)
        		page_counter +=10
        
	except IOError:
    		print "[-] Can't connect to MSN Web!"+""
	
		  
if len(sys.argv) != 4 and len(sys.argv) != 5:
	print "\n   d3hydr8[at]gmail[dot]com MSN2Text v1.1"
	print "------------------------------------------------"
	print "\nUsage: ./msn2text.py <query> <how many> <file to save results> <rfi-path>"
	print "Ex: ./msn2text.py \"inurl:/etc/shadow\" 200 results.txt /contact.php?dir=shell\n"
	sys.exit(1)
	
else:
	print "\n   d3hydr8[at]gmail[dot]com msn2Text v1.1"
	print "------------------------------------------------"
	print "[+] Searching: search.msn.com"
	print "[+] Target:",sys.argv[1]
	print "[+] Total:",sys.argv[2]
	print "[+] File:",sys.argv[3]
	try:
		print "[+] Shell:",sys.argv[4]
	except(IndexError):
		print "None"
		pass

if sys.argv[2].isdigit() == False:
	print "\n[-] Argument [",sys.argv[2],"] must be a number.\n"
	sys.exit(1)
if int(sys.argv[2]) <= 10:
	print "\n[-] Argument [",sys.argv[2],"] must be greater than 10.\n"
	sys.exit(1)

query = re.sub("\s","+",sys.argv[1])
if len(sys.argv) == 5:
	shell=sys.argv[4]
	if shell[0] != "/":
		shell = "/"+shell
else:
	shell = ""
	
d=[]

getsites()
d = list(sets.Set(d))
		
file = open(sys.argv[3], "a")
print "\n[+] Found:",len(d)
print "[+] Writing Data:",sys.argv[3]
for sites in d:	
	if sites.find('www') != -1:
		sites = sites[sites.find('www'):]
	if len(sites) > 12: 
		file.writelines(sites+shell+"\n")
file.close()
print "[-] Done\n"

