Files
Arjun/core/utils.py

126 lines
3.4 KiB
Python
Raw Normal View History

2019-03-02 05:43:02 +05:30
import re
2019-03-03 16:17:06 +05:30
import json
2018-11-09 20:32:08 +05:30
import string
import random
import requests
import core.config
2018-11-09 20:32:08 +05:30
from core.colors import bad
def log(data, mode='', show=False):
suffix = '\n'
if mode == 'run':
suffix = '\r'
if not core.config.globalVariables['url_file']:
print (data, end=suffix)
else:
if show:
print (data, end=suffix)
def extractHeaders(headers):
sortedHeaders = {}
matches = re.findall(r'(.*):\s(.*)', headers)
for match in matches:
header = match[0]
value = match[1]
try:
if value[-1] == ',':
value = value[:-1]
sortedHeaders[header] = value
except IndexError:
pass
return sortedHeaders
def unityExtracter(arrayOfArrays, usable):
2019-03-03 16:17:06 +05:30
"extracts the value from single valued list from a list of lists"
remainingArray = []
for array in arrayOfArrays:
if len(array) == 1:
usable.append(array[0])
else:
remainingArray.append(array)
return remainingArray
2019-03-02 05:43:02 +05:30
def slicer(array, n=2):
2019-03-03 16:17:06 +05:30
"divides a list into n parts"
2019-03-02 05:43:02 +05:30
k, m = divmod(len(array), n)
return list(array[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(n))
2019-03-02 05:43:02 +05:30
2019-03-03 16:17:06 +05:30
def joiner(array, include):
"converts a list of parameters into parameter and value pair"
2019-03-02 05:43:02 +05:30
params = {}
for element in array:
params[element] = randomString(6)
2019-03-03 16:17:06 +05:30
params.update(include)
2019-03-02 05:43:02 +05:30
return params
2018-11-09 20:32:08 +05:30
def stabilize(url):
2019-03-03 16:17:06 +05:30
"picks up the best suiting protocol if not present already"
2018-11-09 20:32:08 +05:30
if 'http' not in url:
try:
requests.get('http://%s' % url) # Makes request to the target with http schema
url = 'http://%s' % url
except: # if it fails, maybe the target uses https schema
url = 'https://%s' % url
try:
requests.get(url) # Makes request to the target
except Exception as e: # if it fails, the target is unreachable
if 'ssl' in str(e).lower():
2018-11-10 05:29:33 +05:30
pass
2018-11-09 20:32:08 +05:30
else:
print ('%s Unable to connect to the target.' % bad)
quit()
return url
2019-03-02 05:43:02 +05:30
def removeTags(html):
2019-03-03 16:17:06 +05:30
"removes all the html from a webpage source"
2019-03-02 05:43:02 +05:30
return re.sub(r'(?s)<.*?>', '', html)
def lineComparer(response1, response2):
2019-03-03 16:17:06 +05:30
"compares two webpage and finds the non-matching lines"
2019-03-02 05:43:02 +05:30
response1 = response1.split('\n')
response2 = response2.split('\n')
num = 0
dynamicLines = []
for line1, line2 in zip(response1, response2):
if line1 != line2:
dynamicLines.append(num)
num += 1
return dynamicLines
2019-03-03 16:17:06 +05:30
def randomString(n):
"generates a random string of length n"
return ''.join(random.choice(string.ascii_lowercase) for i in range(n))
def e(string):
"utf encodes a string"
return string.encode('utf-8')
def d(string):
"utf decodes a string"
return string.decode('utf-8')
2018-11-09 20:32:08 +05:30
def flattenParams(params):
flatted = []
for name, value in params.items():
flatted.append(name + '=' + value)
return '?' + '&'.join(flatted)
2019-03-03 16:17:06 +05:30
def getParams(data):
params = {}
try:
params = json.loads(str(data).replace('\'', '"'))
return params
except json.decoder.JSONDecodeError:
if data.startswith('?'):
data = data[1:]
parts = data.split('&')
for part in parts:
each = part.split('=')
try:
params[each[0]] = each[1]
except IndexError:
params = None
return params