This repository has been archived by the owner on May 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathMsfConsole.py
152 lines (127 loc) · 4.72 KB
/
MsfConsole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import time
import socket
import string
from metasploit.msfrpc import MsfRpcClient
from metasploit.msfrpc import MsfRpcError
from ssl import SSLError
#
class MsfConsole:
# Objects
client = None
console = None
# Variables
console_id = ""
def __init__(self, username, password, port, host, ssl):
self.username = username
self.password = password
self.port = port
self.host = host
self.ssl = ssl
# Connect to the msfrpcd server
def connect(self):
print "[*] Connecting to server:\n Host => %s,\n Port => %s,\n User => %s,\n " \
"Pwd => %s,\n SSL => %s\n" % (self.host, self.port, self.username, '*' * len(self.password), self.ssl)
# Login to msfrpcd server
try:
kwargs = {'username': self.username, 'port': self.port, 'server': self.host, 'ssl': self.ssl}
self.client = MsfRpcClient(self.password, **kwargs)
print "[+] Successfully connected"
except SSLError, msg:
print "[-] SSL error: " + str(msg)
print "[-] You probably have installed the wrong pymetasploit version try installing it from here: /~https://github.com/allfro/pymetasploit.git"
return False
except socket.error, msg:
print "[-] Couldn't connect to server: " + str(msg)
return False
except MsfRpcError:
print "[-] Login failed. Wrong username or password"
return False
# Create console and id
self.console = self.client.consoles.console()
self.console_id = self.console.cid
print "[*] Console id: " + self.console_id
# Read msf banner
self.read_output()
return True
# Read the output from msfconsole
def read_output(self):
try:
timer = 0
while timer <= 5:
# Request information from msfrpcd
resource = self.client.call('console.read', self.console_id)
# Check for printable information
if len(resource['data']) > 1:
print resource['data']
break
# If msf command still running try requesting again
if resource['busy']:
time.sleep(0.2)
timer += 0.2
continue
# If resource is not busy quit reading
else:
break
if timer >= 3:
print "[-] Server response timed out"
return False
return True
except KeyError:
print "[-] Has the console been destroyed ? "
print resource if 'resource' in locals() else "Couldn't print error"
return False
# Load resource file and execute every command
def load_resource(self, path_to_resource):
# Read resource file
try:
print "[*] Reading resource file..."
infile = open(path_to_resource, 'r')
commands = infile.readlines()
infile.close()
except IOError:
print "[-] Path to resource file not found"
return False
# Loop through every command and execute it
print "[*] Number of commands to execute: " + str(len(commands))
for line in commands:
self.console.write(line)
self.read_output()
print "[+] Finished executing resource script"
# List created jobs
self.list_jobs()
return True
# List running jobs
def list_jobs(self):
# Request list of running jobs
resource = self.client.jobs.list
# If no error occurred
if "error" not in resource:
print "[+] Listing jobs..."
print resource
return True
# If error occurred
elif "error" in resource:
print "[-] An error has occurred in listing jobs.\n"
print resource
return False
# Execute command in msfconsole
def exec_command(self, command):
self.console.write(command)
self.get_path()
self.read_output()
return True
# Disconnect from msfconsole
def disconnect(self):
print "[*] Closing session..."
self.console.destroy()
self.client.client.close()
# Get current msfconsole path
def get_path(self):
# Request data from server
resource = self.client.call('console.list')
# Filter the path out of it
for console in resource['consoles']:
if console['id'] == self.console_id:
s = console['prompt']
extracted_path = ''.join(c for c in s if c in string.printable)
return extracted_path