Random Python
Below you find various pieces of python code from engagements. Please note I do not consider myself a programmer by any stretch of the imagination.
This code below is for educational purposes only.
Quick example of using the host command to determine the IP address of a target.
import subprocess, string, re
process = subprocess.Popen('/usr/bin/host -a google.com', shell=True, stdout=subprocess.PIPE);
for line in process.stdout:
match = re.findall('A (\d+\.\d+\.\d+\.\d+)', line)
if match: print match[0]
Now lets try downloading a webpage and printing the results.
import urllib2
response = urllib2.urlopen("http://www.bing.com/search?q=philadelphai+flyers")
print response.read()
Now lets take this set further and look through the printed results for a certain string.
import urllib2,re
response = urllib2.urlopen("http://www.bing.com/search?q=fphiladelphia+flyers")
data = response.read()
matches = re.findall('Mike Vecchione', data)
for match in matches:
print "GOT A HIT:",match
What if the website you want to connect to, extact data from and parse requires authentication?
import urllib2
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='Secure Site',
uri='http://192.168.11.101:8080',
user='admin',
passwd='password')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
try:
response = urllib2.urlopen('http://192.168.11.101:8080')
except urllib2.HTTPError:
print "[!] Wrong Login or Password"
else:
print "[*] Login is correct. Printing returned content..."
print response.read()
Written on December 18, 2019