-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-mapper.py
More file actions
executable file
·210 lines (164 loc) · 5.5 KB
/
ssh-mapper.py
File metadata and controls
executable file
·210 lines (164 loc) · 5.5 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
# This script helps enumerate ssh key networks. It does so by trying to log
# into each of the hosts in the `ssh-hosts.txt` file as each of the users in
# the `ssh-users.txt` file.
# Written by, John R., Dec. 2022
import os
import sys
import socket
import requests
import json
import time
import getopt
from utils import check_for_file
from paramiko import SSHClient, MissingHostKeyPolicy
# Remote SSH Host Class.
class SshHost:
def __init__(self):
self.lhostname = ""
self.rhostname = ""
self.lhost_pub_ip = ""
self.ssh_port = 22
self.users = []
def __str__(self):
str_repr = "###### SSH Host Object ###### \n"
str_repr += "Local Host: " + self.lhostname + "\n"
str_repr += "Remote Host: " + self.rhostname + "\n"
str_repr += "Local Public IP: " + self.lhost_pub_ip + "\n"
str_repr += "SSH Port: " + str(self.ssh_port) + "\n"
str_repr += "Valid Users(s): " + str(self.users) + "\n"
return str_repr
# Make SshHost class JSON serializable.
class SshHostEncoder(json.JSONEncoder):
def default(self, o):
return o.__dict__
# Class to hold optional arguments.
class Options:
def __init__(self):
self.pretty_print = ""
self.raw = ""
self.output_file = "ssh-map.json"
self.delay = .5
def print_help():
print("""
Usage:
ssh-mapper.py [options]
-h print this help menu
-p print output plain text
-r print output as raw json (mutes ticker)
-o <file_name> write to file, defaults to `ssh-map.json`
-d <delay> delay seconds between ssh attempts
""")
def main():
options = Options()
try:
opts, args_garbage = getopt.getopt(sys.argv[1:], "hpro:d:")
except getopt.GetoptError as err:
# Print help information and exit:
print(err) # will print something like "option -a not recognized"
print_help()
exit(2)
if args_garbage:
print("\nContains cmd line garbage:" + str(args_garbage) + "\n")
print_help()
exit(5)
for opt, arg in opts:
if opt == "-h":
print_help()
exit(1)
elif opt == "-p":
options.pretty_print = "yes"
elif opt == "-r":
options.raw = "yes"
elif opt == "-o":
options.output_file = arg
elif opt == "-d":
try:
options.delay = int(arg)
except ValueError:
print("Integers Only!")
print_help()
exit(9)
# Amount of time b/w ssh connection attempts.
delay_time = options.delay
hosts_and_ports = {}
# Read in contents of ssh-hosts.txt.
host_file_name = "ssh-hosts.txt"
check_for_file(host_file_name)
ssh_hosts_file = open(host_file_name, 'r')
while True:
# Get next line from file.
line = ssh_hosts_file.readline()
line = line.rstrip("\n")
# If line is empty end of file is reached.
if not line:
break
# Gather host and port info.
rhost = line.split(":")[0]
rport = line.split(":")[1]
hosts_and_ports[rhost] = rport
ssh_hosts_file.close()
# Read in contents of ssh-users.txt.
users_file_name = "ssh-users.txt"
check_for_file(users_file_name)
potential_users = []
ssh_users_file = open(users_file_name, 'r')
while True:
# Get next line from file.
line = ssh_users_file.readline()
line = line.rstrip("\n")
# If line is empty end of file is reached.
if not line:
break
potential_users.append(line)
ssh_users_file.close()
# Get WAN IP using ipinfo.io API.
r = requests.get("https://ipinfo.io/")
lhost_pub_ip = json.loads(r.text)['ip']
all_ssh_host_objs = []
lhost = os.uname()[1]
# Don't output ticker in raw json mode.
if options.raw != "yes":
print("Trying SSH Connections...", end="", flush=True)
for host in hosts_and_ports:
client = SSHClient()
client.set_missing_host_key_policy(MissingHostKeyPolicy())
new_host = SshHost()
new_host.lhostname = lhost
new_host.rhostname = host
new_host.lhost_pub_ip = lhost_pub_ip
new_host.ssh_port = int(hosts_and_ports[host])
valid_users = []
for user in potential_users:
if options.raw != "yes":
print(".", end="", flush=True)
time.sleep(delay_time)
try:
client.connect(new_host.rhostname, port=new_host.ssh_port,
username=user, look_for_keys=True)
valid_users.append(user)
except Exception as e:
pass
new_host.users = valid_users
all_ssh_host_objs.append(new_host)
client.close()
if options.raw != "yes":
print(".", flush=True)
# Is place for data.
data = []
for host_obj in all_ssh_host_objs:
if len(host_obj.users) > 0:
if options.pretty_print == "yes":
print(host_obj)
data.append(host_obj)
# Write json output to options.output_file.
f = open(options.output_file, "w")
json_data = json.dumps(data, indent=4, cls=SshHostEncoder)
f.write(json_data)
f.close()
if options.raw == "yes":
print(json_data)
else:
print("Results written to: " + options.output_file)
if __name__ == '__main__':
main()