Python Check system suspicious user

Source: Internet
Author: User

Recently, a server was attacked, and an attacker replaced a system command on the server, such as a ps,ss,netstat,lsof file. How to do the troubleshooting will not say. The main purpose of this article is to write a script that examines the users of the system, checks which system users have a home directory, which users do not have a home directory, and which users can log on to the system.


Our environment here is a bit chaotic, some server Web users can log on the system, some systems but can not log on the system, is not standardized, so write a script to do the check. Next, directly on the code, the main use of the PWD and Spwd module,

# encoding: utf8# written by lavenliu at 20170211import pwdimport  spwdsys_users = {}usr_no_passwd = []usr_has_passd = []users_entry =  pwd.getpwall () For entry in users_entry:    sys_users[entry.pw_name]  = entry.pw_shellfor username in sys_users.keys ():     pass_entry  = spwd.getspnam (username)     if pass_entry.sp_pwd ==  '! '  or pass_entry.sp_pwd ==  ' * ':         usr_no_ Passwd.append (Pass_entry.sp_nam)     else:         Usr_has_passd.append (Pass_entry.sp_nam) print  "These users have home directory:" for  user, home in sys_users.items ():    if home ==  '/sbin/ Nologin ':     &nbsP;   continue    else:        print   "%15s: %s"  %  (user, home) printprint  "these users can login  System: "For user in usr_has_passd:    print user


The result of the execution is:

# python chkuser.py These users have home Directory:sync:/bin/sync shutdown:/sbin/shutdown ha LT:/sbin/halt Root:/bin/bashthese users can login System:root


From the execution results, only the root user can log in to the system at this time.

This article is from the "solid-state Drive" blog, make sure to keep this source http://lavenliu.blog.51cto.com/5060944/1901043

Python Check system suspicious user

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.