Skip to content

Commit 265ed28

Browse files
committed
auto change ssh passwd
1 parent 017d7ff commit 265ed28

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

change_ssh_pwd/change_ssh_pwd.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import paramiko
2+
import sys
3+
4+
ssh_clients = []
5+
timeout = 5
6+
new_password = "n3w_p4ssw0rd"
7+
8+
9+
def get_flag():
10+
pass
11+
12+
13+
class SSH_Client():
14+
def __init__(self, host, port, username, password):
15+
self.is_root = False
16+
self.host = host
17+
self.port = port
18+
self.username = username
19+
self.password = password
20+
self.ssh = paramiko.SSHClient()
21+
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
22+
self.ssh.connect(self.host, self.port, self.username, self.password, timeout=timeout)
23+
24+
def exec_command(self, command):
25+
stdin, stdout, stderr = self.ssh.exec_command(command)
26+
return stdin, stdout, stderr
27+
28+
def change_password(self):
29+
stdin, stdout, stderr = self.exec_command("passwd")
30+
if self.username != "root":
31+
stdin.write("%s\n" % self.password)
32+
stdin.write("%s\n" % new_password)
33+
stdin.write("%s\n" % new_password)
34+
stdout.read()
35+
if "success" in stderr.read().decode('utf-8'):
36+
self.password = new_password
37+
return True
38+
else:
39+
return False
40+
41+
def save_log(self, filename):
42+
with open(filename, "a+") as f:
43+
f.write("%s %s %s %s\n" % (self.host, self.port, self.username, self.password))
44+
45+
if __name__ == "__main__":
46+
if len(sys.argv) != 2:
47+
print("Usage:\npython %s [FILENAME]" % (sys.argv[0]))
48+
exit(1)
49+
filename = sys.argv[1]
50+
print(" [+] Loading file : %s" % filename)
51+
with open(filename) as f:
52+
for line in f:
53+
line = line.rstrip("\n")
54+
data = line.split(" ")
55+
host = data[0]
56+
port = int(data[1])
57+
username = data[2]
58+
password = data[3]
59+
print(" [+] Trying login : %s" % host)
60+
try:
61+
ssh_client = SSH_Client(host, port, username, password)
62+
except Exception as e:
63+
print(" [-] %s" % e)
64+
continue
65+
ssh_clients.append(ssh_client)
66+
print(" [+] Login finished. ")
67+
print(" [+} Got [%d] clients. " % len(ssh_clients))
68+
if len(ssh_clients) == 0:
69+
exit()
70+
print(" [+] Starting changing password. ")
71+
for ssh_client in ssh_clients:
72+
if ssh_client.change_password():
73+
print(" [+] %s (Success!)" % ssh_client.host)
74+
ssh_client.save_log("success.log")
75+
else:
76+
print(" [+] %s (Failed!)" % ssh_client.host)

change_ssh_pwd/target.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
192.168.1.1 22 root password
2+
192.168.1.2 22 root password
3+
192.168.1.3 22 root password
4+
192.168.1.4 22 root password
5+
192.168.1.5 22 root password

0 commit comments

Comments
 (0)