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
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:Y4er
import logging
import random
import string
import paramiko
logger = logging.getLogger("Logger")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y/%m/%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
def randomStr(size=16, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def changeSSHPwd(host, username, newpasswd='root', port=22, timeout=5):
'''
更改ssh root密码并返回链接会话对象
:param host: ip地址
:param username: root
:param newpasswd: 新密码
:param port: 端口默认22
:param timeout: 连接超时5s
:return:
'''
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, newpasswd, timeout=timeout)
logger.info("{} 链接成功.".format(host))
stdin, stdout, stderr = ssh.exec_command('id')
logger.info("当前用户权限:%s" % stdout.read().strip('\n'))
stdin, stdout, stderr = ssh.exec_command('echo {}:{}|chpasswd {}'.format(username, newpasswd, username))
logger.warning('尝试更改{}密码为:{}.'.format(host, newpasswd))
except Exception as e:
logging.error("{} ssh connect fail.{}".format(host, e))
exit(0)
return ssh
def check(session):
'''
显示一些基本信息
:param session: ssh会话
:param rootpass: 原root密码
:return:
'''
stdin, stdout, stderr = session.exec_command(
'''sudo cat /etc/passwd|grep -v nologin|awk -F ":" {'print $1"|"$3"|"$4"|"$6'}''')
logger.info("显示可疑用户\n" + stdout.read())
stdin, stdout, stderr = session.exec_command('''last -n 10|awk '{print $1}' ''')
logger.info("显示最近登录的10个用户\n" + stdout.read())
stdin, stdout, stderr = session.exec_command(
'''find / -iname "*upload*" |grep php ''')
logger.info("可疑上传文件的脚本\n" + stdout.read())
stdin, stdout, stderr = session.exec_command(
'''netstat -natlp |sed '1,2d'|awk -F " " {'print $4"|"$5"|"$6'} ''')
logger.info("所有开放的端口号\n本地主机|远程主机|状态\n" + stdout.read())
stdin, stdout, stderr = session.exec_command(
'''netstat -antulp | grep EST ''')
logger.info("查看已建立的网络连接及进程\n" + stdout.read())
stdin, stdout, stderr = session.exec_command(
'''find / -mtime 0 -name "*.php" ''')
logger.info("查找24小时内修改的文件\n" + stdout.read())
def bak(session, rootpass, newrootpass='root'):
'''
备份文件
:param session:ssh会话
:return:
'''
session.exec_command(
'''sudo cp /etc/passwd /tmp/passwd && sudo cp /etc/shadow /tmp/shadow ''')
logger.info("备份passwd和shadow到/tmp/")
stdin, stdout, stderr = session.exec_command(
'''mkdir /tmp/www/ && cp -R /var/www/html/ /tmp/www/ ''')
logger.info("备份/var/www/html/到/tmp/www/")
session.exec_command(
'''mkdir /tmp/database/ && mysqldump -uroot -p{} --all-databases > /tmp/database/all.sql'''.format(rootpass))
logger.info("备份MySQL数据库到/tmp/database/all.sql")
session.exec_command('''find / -iname "phpinfo.php"|xargs rm -rf''')
logger.warning("删除phpinfo.php")
session.exec_command('''find / -type d -iname "*phpmyadmin*"|xargs rm -rf''')
logger.warning("删除phpmyadmin")
session.exec_command('''mysqladmin -u root -p{} password {}'''.format(rootpass, newrootpass))
logger.warning("修改MySQL root账户密码为{}".format(newrootpass))
session.exec_command('''service mysql restart''')
def defend(session, ip):
'''
加固措施
:param session: ssh会话
:param ip: 你的ip或c段
:return:
'''
stdin, stdout, stderr = session.exec_command('''echo "sshd:{}" >> /etc/hosts.allow '''.format('ip'))
logger.warning("添加{}到/etc/hosts.allow".format(ip))
stdin, stdout, stderr = session.exec_command('''service ssh restart''')
stdin, stdout, stderr = session.exec_command(
'''mkdir -R /bin/zzrvtc/ && mv /bin/curl /bin/zzrvtc/curl && mv /bin/wget /bin/zzrvtc/wget && mv /bin/ls /bin/zzrvtc/ls && mv /bin/cd /bin/zzrvtc/cd&&mv /bin/ll /bin/zzrvtc/ll''')
logger.warning("移动curl wget cd ls ll命令到/bin/zzrvtc/下 {}".format(stdout.read()))
if __name__ == '__main__':
# 更改ssh密码为root
session = changeSSHPwd('192.168.24.128', 'root', 'root')
# check
check(session)
# 更改mysql密码为root
bak(session, 'root')
# 防御策略
defend(session,'192.168.24.128/24')
|