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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
import os,re,time,sys from os import path
"""
使用方法: python find_risky_root_run_scripts.py XXX (XXX为整数,指配置工具运行的秒数)
功能介绍: 1. 可以识别一段时间内所有root运行过的脚本,包括绝对路径和相对路径; 2. 排查以上脚本的属主权限、三组文件权限; 3. 遍历以上脚本的所有父目录,排查所有父目录的属主权限、三组目录权限。 4. 可以打印所有有目录漏洞的脚本列表及对应的漏洞目录,以及汇总呈现所有有漏洞目录的报表
"""
def get_dir_privilege(dir_name):
try: query_privilege_shellcmd = 'ls -al %s | awk "NR==2"'%(dir_name) privilege_result = os.popen(query_privilege_shellcmd).read().split()
u,g,o,owner,group = privilege_result[0][1:4],privilege_result[0][5:6],privilege_result[0][8:9],privilege_result[2],privilege_result[3] return {'u':u,'g':g,'o':o,'owner':owner,'group':group} except: return {}
def check_dir_vulnerable(dirname):
is_dir_vulnerable = False
dir_privilege = get_dir_privilege(dirname)
if dir_privilege and dir_privilege['owner'] != 'root': is_dir_vulnerable = True
if dir_privilege and dir_privilege['owner'] == 'root' and ( (dir_privilege['group'] == 'root' and dir_privilege['o'] != '-' ) or (dir_privilege['group'] != 'root' and (dir_privilege['g'] != '-' or dir_privilege['o'] != '-' )) ): is_dir_vulnerable = True
return is_dir_vulnerable
def get_one_root_script_dirs(full_path_to_filename):
all_one_root_script_dirs = set()
while full_path_to_filename != "/" and full_path_to_filename != ".": full_path_to_filename = os.path.dirname(full_path_to_filename) all_one_root_script_dirs.add(full_path_to_filename.strip())
return list(all_one_root_script_dirs)
def check_dirs_vulnerable(one_root_script):
is_dirs_vulnerable = False
one_root_script_dirs = set() one_root_script_dirs = get_one_root_script_dirs(one_root_script) for dir in one_root_script_dirs: is_dirs_vulnerable_tmp = False
is_dirs_vulnerable_tmp = check_dir_vulnerable(dir) if is_dirs_vulnerable_tmp: is_dirs_vulnerable = True
return is_dirs_vulnerable
def get_dirs_vulnerable(one_root_script):
is_dirs_vulnerable = False one_root_script_dirs_vulnerable = set()
one_root_script_dirs = set() one_root_script_dirs = get_one_root_script_dirs(one_root_script) for dir in one_root_script_dirs: is_dirs_vulnerable_tmp = False
is_dirs_vulnerable_tmp = check_dir_vulnerable(dir) if is_dirs_vulnerable_tmp: is_dirs_vulnerable = True one_root_script_dirs_vulnerable.add(dir.strip())
return one_root_script_dirs_vulnerable
def get_file_privilege(file_name):
try: query_script_privilege_shellcmd = 'ls -l %s'%(file_name) privilege_result = os.popen(query_script_privilege_shellcmd).read().split() u,g,o,owner,group = privilege_result[0][1:4],privilege_result[0][5:6],privilege_result[0][8:9],privilege_result[2],privilege_result[3] return {'u':u,'g':g,'o':o,'owner':owner,'group':group} except: return None
def check_file_vulnerable(filename):
is_file_vulnerable = False file_privilege = get_file_privilege(filename)
if file_privilege and file_privilege['owner'] != 'root': is_file_vulnerable = True
if file_privilege and file_privilege['owner'] == 'root' and ( (file_privilege['group'] == 'root' and file_privilege['o'] != '-' ) or (file_privilege['group'] != 'root' and (file_privilege['g'] != '-' or file_privilege['o'] != '-' )) ): is_file_vulnerable = True
return is_file_vulnerable
def get_all_root_running_script(scan_period): print '\033[5;32m[*] Collect all root running scripts in %d seconds ......\033[0m\n'%scan_period
root_running_script_list_all = set()
root_running_script_list_exists = set() root_running_script_list_no_exist = set()
waiting_reverse_loookup_list = set() waiting_reverse_loookup_list_debug = set()
query_root_running_script_by_shellcmd = 'ps -U root -u root u | egrep ".*\.py.*|.*\.sh.*" | grep -v egrep'
start_time = int(time.time()) while int(time.time()) - start_time <= scan_period: for i in re.findall(r"\S+\.sh|\S+\.py",os.popen(query_root_running_script_by_shellcmd).read()): if i.count('/') <= 1: if re.match('^[a-zA-Z.\-_]+$',i) or re.match('^[.][/]',i): waiting_reverse_loookup_list.add(i.split('/')[-1].strip()) elif( re.match('^[^/]',i)): waiting_reverse_loookup_list.add(i.split('/')[-1].strip()) else: root_running_script_list_all.add(i.strip())
if waiting_reverse_loookup_list: print '本程序抓取的采用相对路径运行的程序有:' print (waiting_reverse_loookup_list)
lookup_filenames = '|'.join(waiting_reverse_loookup_list)
if lookup_filenames: reverse_lookup_cmd = 'find / \( -path /proc -o -path /var \) -prune -o -print | grep -E "^*/(%s)$"'%(lookup_filenames) for i in os.popen(reverse_lookup_cmd).readlines(): root_running_script_list_all.add(i.strip())
if root_running_script_list_all: for script in root_running_script_list_all: if os.path.isfile(script): root_running_script_list_exists.add(script.strip()) else: root_running_script_list_no_exist.add(script.strip())
if root_running_script_list_no_exist: print("\033[34m\n") print("[*]Please check the following paths manully:") for one_root_script in root_running_script_list_no_exist: print(one_root_script) print("\033[0m")
return list(root_running_script_list_exists)
def find_risky_root_running_scripts(scan_period):
root_running_script_list = get_all_root_running_script(scan_period)
root_running_script_list_with_dirs_risk = set() root_running_script_list_with_file_risk = set()
for one_root_script in root_running_script_list:
is_script_dirs_vulnerable = False is_script_file_vulnerable = False
is_script_dirs_vulnerable = check_dirs_vulnerable(one_root_script) is_script_file_vulnerable = check_file_vulnerable(one_root_script)
if is_script_dirs_vulnerable: root_running_script_list_with_dirs_risk.add(one_root_script)
if is_script_file_vulnerable: root_running_script_list_with_file_risk.add(one_root_script)
risky_dirs_of_root_running_scripts = set() if root_running_script_list_with_dirs_risk: root_running_script_list_with_dirs_risk_sorted = sorted(root_running_script_list_with_dirs_risk) print("\033[1;31m\n\n") print("[*]root_running_script_list_with_dirs_risk_sorted, and their dirs:") for script in root_running_script_list_with_dirs_risk_sorted: print("\n\n%s:" %script) risky_dirs_of_root_running_script = get_dirs_vulnerable(script) risky_dirs_of_root_running_script_sorted = sorted(risky_dirs_of_root_running_script, reverse=True) if risky_dirs_of_root_running_script_sorted: for dir in risky_dirs_of_root_running_script_sorted: print (dir) risky_dirs_of_root_running_scripts.add(dir.strip()) print("\033[0m") if risky_dirs_of_root_running_scripts: risky_dirs_of_root_running_scripts_sorted = sorted(risky_dirs_of_root_running_scripts) print("\033[1;31m\n\n") print("[*]risky_dirs_of_root_running_scripts_sorted:") for dir in risky_dirs_of_root_running_scripts_sorted: print (dir) print("\033[0m")
if root_running_script_list_with_dirs_risk: root_running_script_list_with_dirs_risk_sorted = sorted(root_running_script_list_with_dirs_risk) print("\033[1;31m\n") print("[*]root_running_script_list_with_dirs_risk_sorted:") for one_root_script in root_running_script_list_with_dirs_risk_sorted: print(one_root_script) print("\033[0m")
if root_running_script_list_with_file_risk: root_running_script_list_with_file_risk_sorted = sorted(root_running_script_list_with_file_risk) print("\033[1;31m\n") print("[*]root_running_script_list_with_file_risk_sorted:") for one_root_script in root_running_script_list_with_file_risk_sorted: print(one_root_script) print("\033[0m")
if __name__ == '__main__': find_risky_root_running_scripts(int(sys.argv[1])) print '\n'
|