import subprocess
import sys
#Dependecy on searchsploit

#def checkVuln(banner,port,servname,result,target,scan_id):
def checkVuln(banner):
	global output
	vuln = "searchsploit" + " " + "-w" +  " " + '"' + str(banner) + '"' + "|" + "grep -v '^ '|grep -v '^-'|sed 's/|//g'"
	#files = "searchsploit " + '"' + str(banner) + '"' + "|awk -F'|' '{print $2}'|grep -v '[()]'|sed 's/Path//g'" + '|grep  "[a-zA-Z0-9/.]"' + '|sed "s/ //g"'
	#vufile = subprocess.getoutput(files)
	#print(vufile)

	links = vuln + "|" + "grep -o 'https://[a-zA-Z./0-9-]*'"
	#listvuln = vuln + "|" + "awk -F'https' '{print $1}'"
	listvuln = vuln + "|" + "awk -F'https' '{print $1}'"
	vulname = subprocess.getoutput(listvuln)
	#print(vulname)
	vulink = subprocess.getoutput(links)
	if not vulname:
		output = "Not Vulnerable"
		cve = "N/A"
		print(output)
		print(cve)

	else:
		print("Vulneribility caller")  #Fo debugging purpose
		output = "Vulnerable"
		with open('/root/Music/vuln_file','w',encoding='utf-8') as f:
			print(vulink, file=f)
			f.close()
		print("file created")
		#lines = [line.rstrip('\n') for line in open('/home/xyz/Music/vuln_file','rt',encoding='utf-8')]
		#print(lines)

		#st = ""
		#linez = [] # Declare an empty list named "lines"
		'''for name in range(len(lines)):
			filename = lines[name]
			namef= '/opt/exploit-database/platforms' + str(filename)
			print(namef)
			with open (namef, 'rt') as in_file:
				 # Open file lorem.txt for reading of text data.
				for line in in_file: # For each line of text in in_file, where the data is named "line",
					linez.append(line) # add that line to our list of lines.
				for element in linez: # For each element in our list,
					s = element.find("CVE")
					if s != -1:
						for i in range(8,len(element)):
						#print(element[i], end='')
							st += str(element[i])

		print(st)'''
		#tf = open("link_file","rt",encoding='utf-8')
		lines = [line.rstrip('\n') for line in open('/root/Music/vuln_file','rt',encoding='utf-8')]
		#print(lines)
		#temp = urllib.request.urlopen("https://www.exploit-db.com/exploits/30850/")
		#pattern = "echo" + " " + HTML + "|grep -o 'CVE[-0-9]*'"
		cve_arr=[]
		cve_avail=[]
		cve_notavail=[]
		#for link in range(len(lines)):
		for file_in in range(len(lines)):
			title = lines[file_in]
			#pattern = " curl -s --compressed " + title + " -o /home/xyz/Music/curl_dump.html"
			pattern = "curl  -s --compressed " + title + "|grep 'cve.mitre.org'" + "|awk -F'name=' '{print $2}'" + "|awk -F" + "'" + '"' + "'" + " '{print $1}'"
			#print(pattern)
			html_file = subprocess.getoutput(pattern)
			cve_out = subprocess.getoutput(pattern)

			if not cve_out:
				c=0
				cve_notavail.append(c)
			else:
				c=1

				cve_arr.append(cve_out)
				cve_out = cve_out + ","
				cve_avail.append(cve_out)



		if c == 0:
			cve_result = "Not Available"
		else:
			for i in range(len(cve_avail)):
				print("++",cve_avail[i])
				print(port)
				
			print(len(cve_notavail),"More found with same name!!")
			print(cve_avail)
			#cve_str = ''.join(cve_avail)
			#print(cve_str)

		#print(st)
			#print("HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAH",cve_avail)  #For debugging purpose 
def main():
	result = sys.argv[1]
	global port
	global client_id
	#client_id = "ABCXYZ123"
	#port = sys.argv[2]
	#print(port)#port = sys.argv[2]
	#servname = sys.argv[3]
	#target = sys.argv[4]
	#scan_id = sys.argv[5]

	checkVuln(sys.argv[1])   #,port,servname,result,target,scan_id)

	#checkVuln("HFS")  #For Testing HFS vulnerability


if __name__ == '__main__':
    main()
