how to get proxy ip and port from text file - python

how to get proxy ip and port from text file
Code :
import re , urllib , urllib2 , socks , socket
proxys = open('IPs.txt', 'r')
links = open('URs.txt', 'r')
#----
P = proxys.readlines()
E = links.readlines()
#----
nm = 0
#----
PROXY = P[nm]
#----
for links in E :
Post = 'id='+ links
cj = CookieJar()
#----
IP,PORT = PROXY.split(":")
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, PORT)
socket.socket = socks.socksocket
#----
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
request = urllib2.Request('https://google.com/', Post)
# ----------------- ++
nm+=1
PROXY = P[nm]
# ----------------- ++
IPs.txt:
96.47.156.166:10200
96.47.88.7:14328
97.88.243.210:24598
98.201.217.101:23320
Error Here :
PROXY = P[0] # = 96.47.156.166:10200 #from the text file
IP,PORT = PROXY.split(":")
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "96.47.156.166", "10200")
i need it like this to work :
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "96.47.156.166", 10200) #withot ""
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You need to convert PORT to an int:
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, int(PORT))
Note that this will raise a ValueError if for some whatever reason PORT can't be converted, so you may want to catch it.
Depending on the structure of your file, it is most likely that PORT will include a '\n' in the end. You will need to get rid of it with strip before trying to convert it to an int.
try:
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, int(PORT.strip()))
except ValueError:
print('Illegal port')

Related

gethostbyaddr looping true list

I'm trying to get the following code working
import socket
#import dns.resolver
#my_resolver = dns.resolver.Resolver()
#my_resolver.nameservers = ['192.168.1.2']
ip_list = []
for ip in range(1, 256):
ip_list.append('192.168.1.' + str(ip))
with open(os.devnull, "wb") as limbo:
for ip in ip_list:
name = socket.gethostbyaddr(ip)
print(name)
my problem is that at the moment the ip has no registration in the DNS it gives an error
File "/data/opt/python/resolv.py", line 15, in <module>
name = socket.gethostbyaddr(ip)
socket.herror: [Errno 1] Unknown host
How can I solve this
I'm using python2 due to some limitations
Thanks in regards.
the error ecours because there is the ip address you tried does not exists.
you can try catch the with try and except, or check if they are up.
try and except:
import socket
#import dns.resolver
#my_resolver = dns.resolver.Resolver()
#my_resolver.nameservers = ['192.168.1.2']
ip_list = []
for ip in range(1, 256):
ip_list.append('192.168.1.' + str(ip))
with open(os.devnull, "wb") as limbo:
for ip in ip_list:
try:
name = socket.gethostbyaddr(ip)
print(name)
except:
continue
check if up:
import socket
#import dns.resolver
#my_resolver = dns.resolver.Resolver()
#my_resolver.nameservers = ['192.168.1.2']
ip_list = []
for ip in range(1, 256):
ip_list.append('192.168.1.' + str(ip))
HOST_UP = True if os.system("ping -c 5 " + SOMEHOST.strip(";")) is 0 else False
with open(os.devnull, "wb") as limbo:
for ip in HOST_UP:
try:
name = socket.gethostbyaddr(ip)
print(name)
except:
continue

Python Error handling in try block pass entry to array

I am ingesting a list of servers from a text file and using pyopenssl to resolve, connect,and retrieve the SSL Certificate information and passing my results to an array. It is working perfectly until one of my servers in the list does not resolve and I get a socket.gaierror error.
Although I can capture the error in the logs I am trying pass along something that will note the exception in my array results and that I will be able to pass to a table and send in an email. I want it to note in the host field "Unable to resolve" Can anyone point me towards a good way of accomplishing that? Thanks!
Basic order of operations:
Grab each host in the file
Create an array to house the results
Connect to server using SSL
Get SSL info and close connection
From SSL certificate get host name, expiration date, and decode
Get date, format, and calculate number of days until SSL expires
Record entry in the ssl_results array
import ssl
from datetime import datetime
import OpenSSL
import socket
from datetime import timedelta
import datetime
import traceback
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:% (message)s')
file_handler = logging.FileHandler('log/SSLNag.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
try:
ipfile = open('server_ip.txt')
cur_date = datetime.datetime.utcnow()
ssl_results = {}
except Exception as e:
logger.warning("ERROR ENCOUNTERED! \n\n")
logger.warning(str(traceback.format_exc()))
for ip in ipfile:
ssl_results[str(ip)] = {'host': '', 'server_name': '',
'exp_date': '', 'days_to_expire': ''}
try:
host = ip.strip().split(':')[0]
port = ip.strip().split(':')[1]
print('\nChecking certificate for server ', host)
ctx = OpenSSL.SSL.Context(ssl.PROTOCOL_TLSv1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(port)))
cnx = OpenSSL.SSL.Connection(ctx, s)
cnx.set_connect_state()
cnx.do_handshake()
cert = cnx.get_peer_certificate()
s.close()
server_name = cert.get_subject().commonName
print(server_name)
edate = cert.get_notAfter()
edate = edate.decode()
exp_date = datetime.datetime.strptime(edate, '%Y%m%d%H%M%SZ')
days_to_expire = int((exp_date - cur_date).days)
print(exp_date)
print('day to expire', days_to_expire)
ssl_results[str(ip)]['host'] = host
ssl_results[str(ip)]['server_name'] = server_name
ssl_results[str(ip)]['exp_date'] = exp_date
ssl_results[str(ip)]['days_to_expire'] = days_to_expire
except Exception as e:
logger.warning('Error on connection to Server,', str(ip))
logger.warning("ERROR ENCOUNTERED", host, "\n\n")
logger.warning(str(traceback.format_exc()))
first theres an Indent missing at the second try and except :-)
Isnt it just this simple:
import ssl
from datetime import datetime
import OpenSSL
import socket
from datetime import timedelta
import datetime
import traceback
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:% (message)s')
file_handler = logging.FileHandler('log/SSLNag.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
try:
ipfile = open('server_ip.txt')
cur_date = datetime.datetime.utcnow()
ssl_results = {}
except Exception as e:
logger.warning("ERROR ENCOUNTERED! \n\n")
logger.warning(str(traceback.format_exc()))
for ip in ipfile:
global server_name, host, exp_date, days_to expire
ssl_results[str(ip)] = {'host': '', 'server_name': '',
'exp_date': '', 'days_to_expire': ''}
try:
host = ip.strip().split(':')[0]
port = ip.strip().split(':')[1]
print('\nChecking certificate for server ', host)
ctx = OpenSSL.SSL.Context(ssl.PROTOCOL_TLSv1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(port)))
cnx = OpenSSL.SSL.Connection(ctx, s)
cnx.set_connect_state()
cnx.do_handshake()
cert = cnx.get_peer_certificate()
s.close()
server_name = cert.get_subject().commonName
print(server_name)
edate = cert.get_notAfter()
edate = edate.decode()
exp_date = datetime.datetime.strptime(edate, '%Y%m%d%H%M%SZ')
days_to_expire = int((exp_date - cur_date).days)
print(exp_date)
print('day to expire', days_to_expire)
ssl_results[str(ip)]['host'] = host
ssl_results[str(ip)]['server_name'] = server_name
ssl_results[str(ip)]['exp_date'] = exp_date
ssl_results[str(ip)]['days_to_expire'] = days_to_expire
except Exception as e:
logger.warning('Error on connection to Server,', str(ip))
logger.warning("ERROR ENCOUNTERED", host, "\n\n")
logger.warning(str(traceback.format_exc()))
ssl_results[str(ip)]['host'] = "Unable to resolve"
ssl_results[str(ip)]['server_name'] = " "
ssl_results[str(ip)]['exp_date'] = " "
ssl_results[str(ip)]['days_to_expire'] = " "
Or what to you want to accomplish?
Do you also want the email client?
Try this:https://realpython.com/python-send-email/
I just need to add an exception for the socket.gaierror and update the array.
except socket.gaierror as e:
ssl_results[str(ip)]['host'] = host
ssl_results[str(ip)]['server_name'] = "Could not connect"
ssl_results[str(ip)]['exp_date'] = 0
ssl_results[str(ip)]['days_to_expire'] = 0

Python GET request with sockets - 400 Bad request

I wrote this code to manually make a GET request using only python sockets. It worked perfectly fine back in 2016 when I wrote it but now I need it again and I keep getting the error code 400 bad request. I tried switching python version but it's still the same. I have been looking through Stackoverflow questions, asking more or less the same thing I do, but I just can't get it to work. I would appreciate if anyone could help me out. Here is my code, I removed all the IO and only posted the networking code.
URL_PATTERN = re.compile("^(.*://)?([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$")
HEADER_END = re.compile("\r\n\r\n")
URL_DATA = re.match(URL_PATTERN, INPUT_URL)
PROTOCOL = URL_DATA.groups()[0][:-3]
HOSTNAME = URL_DATA.groups()[1]
PATHNAME = URL_DATA.groups()[3] if URL_DATA.groups()[3] != "" else "/"
PORT = 80 if PROTOCOL == "http" else 443
BUFFER_SIZE = 4096
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOSTNAME, PORT))
s.send("GET " + PATHNAME + " HTTP/1.1\r\nHost: " + HOSTNAME + "\r\nConnection: close\r\n\r\n")
resp = s.recv(BUFFER_SIZE)
HEADER_INDEX = re.search(HEADER_END, resp).start()
HTTP_RESPONSE_HEADER = resp[:HEADER_INDEX]
s.close()
When I run my program on the URL https://doc.rust-lang.org/book/2018-edition/foreword.html
The variables from my program has the values:
PORT: 443
PROTOCOL: https
HOSTNAME: doc.rust-lang.org
PATHNAME: /book/2018-edition/foreword.html
And then I get the 400 bad request code back. I don't understand what I'm doing wrong and would appreciate any help I can get.
I believe it's all about SSL. For reference you can check this question Python socket server handle HTTPS request.
I suggest you use:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
and create a secure socket:
s_sock = context.wrap_socket(s, server_hostname=HOSTNAME)
s_sock.connect((HOSTNAME, PORT))
Additionally you might need to encode the message.
At the end your code could look like:
import re
import socket
import ssl
URL_PATTERN = re.compile("^(.*://)?([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$")
HEADER_END = re.compile("\r\n\r\n")
INPUT_URL = "https://doc.rust-lang.org/book/2018-edition/foreword.html"
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
URL_DATA = re.match(URL_PATTERN, INPUT_URL)
PROTOCOL = URL_DATA.groups()[0][:-3]
HOSTNAME = URL_DATA.groups()[1]
PATHNAME = URL_DATA.groups()[3] if URL_DATA.groups()[3] != "" else "/"
PORT = 80 if PROTOCOL == "http" else 443
BUFFER_SIZE = 4096
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_sock = context.wrap_socket(s, server_hostname=HOSTNAME)
s_sock.connect((HOSTNAME, PORT))
message = "GET " + PATHNAME + " HTTP/1.1\r\nHost: " + HOSTNAME + "\r\nConnection: close\r\n\r\n"
s_sock.send(message.encode('utf-8'))
resp = bytearray()
while True:
part = s_sock.recv(BUFFER_SIZE)
if not part:
break
resp += part
s_sock.close()
resp_string = str(resp, 'utf-8')
HEADER_INDEX = re.search(HEADER_END, resp_string).start()
HTTP_RESPONSE_HEADER = resp_string[:HEADER_INDEX]

Python: I couldn't connect port 80 to get the web page I want

I entered http://127.0.0.1:443/www.google.com and I got 'Illegal request' on the command prompt.
It doesn't work from connecting to port 80 in that code.
what is wrong and How can I get this web site using proxy server?
(I already set proxy setting of internet browser-127.0.0.1 (ip), 443(port)
` Hello.py
from socket import *
import sys
from idlelib.rpc import response_queue
if len(sys.argv) <= 1:
print ('Usage : "python ProxyServer.py server_ip"\n [server_ip : It is the IP Address Of Proxy Server')
sys.exit(2)
# Create a server socket, bind it to a port and start listening
tcpSerSock = socket(AF_INET, SOCK_STREAM)
port = 443
max_connections = 2
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind((sys.argv[1],port))
tcpSerSock.listen(max_connections)
while 1:
# Start receiving data from the client
print ('Ready to serve...')
tcpCliSock, addr = tcpSerSock.accept()
print ('Received a connection from:', addr)
message = tcpCliSock.recv(1024)
print ('Msg: ' ,message)
# Extract the filename from the given message
print ('Msg decoded: ', message.decode().split()[1])
filename = message.decode().split()[1].partition("/")[2]
print ('File name: ', filename)
fileExist = "false"
filetouse = "/" + filename
print ('File touse: ', filetouse)
try:
# Check whether the file exist in the cache
f = open(filetouse[1:], "r")
outputdata = f.readlines()
fileExist = "true"
print ('File exists')
# ProxyServer finds a cache hit and generates a response message
tcpCliSock.send("HTTP/1.0 200 OK\r\n")
tcpCliSock.send("Content-Type:text/html\r\n")
resp = ""
for s in outputdata:
resp += s
tcpCliSock.send(resp)
print ('Read from cache')
# Error handling for file not found in cache
except IOError:
if fileExist == "false":
# Create a socket on the proxy server
c = socket(AF_INET, SOCK_STREAM)
hostn = filename.replace("www.","",1)
print ('File doesn\'t exist')
print (hostn)
try:
# Connect to the socket to port 80
c.connect((hostn, 80))
# Create a temporary file on this socket and ask port 80
# for the file requested by the client
fileobj = c.makefile('r', 0)
fileobj.write("GET " + "http://" + filename + " HTTP/1.0\n\n")
# Read the response into buffer
resp = c.recv(4096)
response = ""
while resp:
response += resp
resp = c.recv(4096)
# Create a new file in the cache for the requested file.
# Also send the response in the buffer to client socket and the corresponding file in the cache
tmpFile = open("./" + filename,"w")
tmpFile.write(response)
tmpFile.close()
tcpCliSock.close()
except:
print ("Illegal request")
else:
# HTTP response message for file not found
pass
# Close the client and the server sockets
tcpCliSock.close()`

Unable to retrieve cert chain Python

I have this code just as a tester for another program that I am trying to write and this does not pull the cert.
import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s,
ca_certs="etc/ca_certs_file",
cert_reqs=ssl.CERT_REQUIRED)
ssl_socket.connect (('www.google.com', 443))
pprint.pprint(ssl_sock.getpeercert())
ssl_sock.close()
The code that I have for my other project can not grab the cert chain with the following code..
def get_peer_cert_chain(host, port):
try:
port = int(port)
ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD)
sock = OpenSSL.SSL.Connection(ctx, socket.socket())
SSLSocket.connect((host, port))
SSLSocket.do_handshake()
SSLSocket.getpeercert(binary_form=False)
return sock.get_peer_cert_chain()
except:
print('INFO: Unable to retrieve certificate chain from ' + str(host) + ':' + str(port))
exit(1)
def cert_chain_interpreter(host, port, chain):
if chain != None:
output_csv = open(args.output,"a")
writer = csv.writer(output_csv)
for cert in chain:
x509Name = OpenSSL.crypto.X509.get_subject(cert)
PKey = OpenSSL.crypto.X509.get_pubkey(cert)
issuer = OpenSSL.crypto.X509.get_issuer(cert)
writer_data = [host, port, OpenSSL.crypto.X509Name.get_components(x509Name), OpenSSL.crypto.X509.has_expired(cert), OpenSSL.crypto.X509.get_notBefore(cert), OpenSSL.crypto.X509.get_notAfter(cert), OpenSSL.crypto.X509Name.get_components(issuer), OpenSSL.crypto.X509.get_signature_algorithm(cert), OpenSSL.crypto.X509.get_serial_number(cert), OpenSSL.crypto.X509.get_version(cert), OpenSSL.crypto.PKey.bits(PKey)]
writer.writerow(writer_data)
output_csv.close()
This code prints out unable to retrieve certificate chain.

Categories

Resources