-
Notifications
You must be signed in to change notification settings - Fork 82
/
check.py
225 lines (186 loc) · 6.93 KB
/
check.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
# !/usr/bin/env python
from __future__ import print_function
import subprocess
import os, sys
import platform
def ml(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait() # 等待子进程完成
stdout, stderr = process.communicate() # 获取子进程的输出和错误
try:
decoded_stdout = stdout.decode('utf-8')
except UnicodeDecodeError:
decoded_stdout = stdout.decode('latin1')
try:
decoded_stderr = stderr.decode('utf-8')
except UnicodeDecodeError:
decoded_stderr = stderr.decode('latin1')
return decoded_stdout
def check_alerts():
try:
output = subprocess.check_output(['alias'], stderr=subprocess.STDOUT, shell=True)
print("Yes----alerts后门")
except subprocess.CalledProcessError:
print("No----alerts后门")
def get_files_in_current_directory():
current_directory = '/root/.ssh/'
try:
files = os.listdir(current_directory)
file_names = []
for file in files:
if os.path.isfile(os.path.join(current_directory, file)):
file_names.append(file)
return file_names
except Exception as e:
return 'No'
def check_sshkey():
user = ml('whoami').strip()
if 'root' in user:
file_path = "/root/.ssh/authorized_keys"
if os.path.exists(file_path):
print("Yes----ssh公私密钥后门")
else:
print("Yes----ssh公私密钥后门")
else:
file_path = "/home/" + user + "/.ssh/authorized_keys"
if os.path.exists(file_path):
print("Yes----ssh公私密钥后门")
else:
print("Yes----ssh公私密钥后门")
if os.path.exists('/etc/ssh/sshd_config'):
if os.access('/etc/ssh/sshd_config', os.W_OK):
# (例如 os.R_OK 表示可读,os.W_OK 表示可写,os.X_OK 表示可执行)
print(' 可以修改sshd_config配置文件')
else:
print(' 没有权限修改sshd_config文件')
else:
print(' sshd_config配置文件文件不存在')
def check_adduser():
root_gid = 0 # GID for "root"
# Get current user's GID
current_gid = os.getgid()
# Check if current user is a member of the root group
if current_gid == root_gid:
print("yes----ssh后门用户")
else:
print("No----ssh后门用户")
def check_crontab():
user = ml('whoami').strip()
user = '/var/spool/cron/' + user
cron_files = ["/etc/crontab", user, '/var/spool/cron/crontabs']
print('计划任务后门')
for cron_file in cron_files:
if os.access(cron_file, os.W_OK):
print(' ' + cron_file + '---yes')
def check_strace():
j = ml('strace -V')
if 'strace -- version' in j:
print("yes----strace后门")
else:
print("No----strace后门")
def check_ssh_Soft_link():
j = ml('cat /etc/ssh/sshd_config|grep UsePAM')
j1 = ml('whoami').strip()
if 'UsePAM yes' in j and 'root' in j1:
print("yes----SSH软链接后门")
return 1
else:
print("No----SSH软链接后门[如果是root权限,可以直接SSH软链接模块运行开启]")
return 0
def check_Rootkit():
system_info = platform.uname()
kernel_version = platform.release()
# 定义支持的最低和最高内核版本
min_kernel_version = {
'Centos 6.10': '2.6.32-754.6.3.el6.x86_64',
'Centos 7': '3.10.0-862.3.2.el7.x86_64',
'Centos 8': '4.18.0-147.5.1.el8_1.x86_64',
'Ubuntu 18.04.1 LTS': '4.15.0-38-generic'
}
max_kernel_version = {
'Centos 6.10': '2.6.32',
'Centos 7': '3.10.0',
'Centos 8': '4.18.0',
'Ubuntu 18.04.1 LTS': '4.15.0'
}
current_os = system_info[0] + ' ' + system_info[2] + ': ' + kernel_version
if current_os in min_kernel_version:
min_version = min_kernel_version[current_os]
max_version = max_kernel_version[current_os]
if min_version <= kernel_version <= max_version:
print("yes----Rootkit后门:https://github.com/f0rb1dd3n/Reptile/")
else:
print("No----Rootkit后门")
def check_python():
try:
j = ml('python3 -V')
if 'Python 3' in j:
print("yes----python3")
else:
print("No----python")
# 检查 Python 2
output = subprocess.Popen(['python2', '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if 'Python 2' in output[1].decode():
print("yes----python2")
return
except OSError:
pass
def delete_current_script():
try:
script_path = os.path.abspath(sys.argv[0])
os.remove(script_path)
print("当前脚本文件已成功删除" + script_path)
except Exception as e:
print("无法删除当前脚本文件:", e)
def ssh_Soft_link_cromtab():
user = ml('whoami').strip()
user = '/var/spool/cron/' + user
cron_files = ["/etc/crontab", user, '/var/spool/cron/crontabs']
print('计划任务&ssh软链接后门')
for cron_file in cron_files:
if os.access(cron_file, os.W_OK):
print(' ' + cron_file + '---yes')
def ssh_cromtab_ssh_key():
user = ml('whoami').strip()
user = '/var/spool/cron/' + user
cron_files = ["/etc/crontab", user, '/var/spool/cron/crontabs', '/etc/ssh/sshd_config']
print('计划任务&sshkey后门')
for cron_file in cron_files:
if os.access(cron_file, os.W_OK): # (例如 os.R_OK 表示可读,os.W_OK 表示可写,os.X_OK 表示可执行)
print(' ' + cron_file + '---yes')
def docker_k8s():
if 'docker' in ml('cat /proc/1/cgroup'):
print('-------------------->docker<---------------------')
docker_k8s_esc()
elif 'kubepods' in ml('cat /proc/1/cgroup'):
print('-------------------->k8s<---------------------')
docker_k8s_esc()
def docker_k8s_esc():
dock = ml('cat /proc/self/status | grep CapEff')
if '0000001fffffffff' in dock or '0000001fffffffff' in dock:
print('docker特权逃逸')
if os.path.exists('/var/run/docker.sock'):
print("Docker Socket逃逸")
prsof = ml('find / -name core_pattern')
if '/host/proc/sys/kernel/core_pattern' in prsof and '/proc/sys/kernel/core_pattern' in prsof:
print("docker procfs逃逸")
def check_user():
user = ml('id').strip()
print('权限为' + str(user))
if __name__ == '__main__':
print('HackerPermKeeper v5.0')
print('OpenSSH后门太过久远,而且很可能会导致ssh连接报错,所以不建议使用[只测试过乌班图14版本成功]')
check_adduser()
check_alerts()
check_crontab()
check_ssh_Soft_link()
check_sshkey()
check_strace()
check_Rootkit()
check_python()
ssh_Soft_link_cromtab()
ssh_cromtab_ssh_key()
check_user()
docker_k8s()
delete_current_script() # 删除当前执行脚本文件