python關於it審計中的應用

來源:互聯網
上載者:User

標籤:python關於it審計中的應用

公司面臨上市,為了滿足上市it審計要求,對系統密碼進行定製。

#!/bin/env pythonimport random,stringimport os,sys,re,paramikofrom optparse import OptionParserdef pssh(host,cmd):         user = ‘root‘         s = paramiko.SSHClient()         s.load_system_host_keys()         s.set_missing_host_key_policy(paramiko.AutoAddPolicy())         s.connect(host,22,user,password=‘test‘,timeout=5)         #cmd="ip a | grep inet | grep 192.168  | grep brd | awk -F/22 ‘{print $1}‘|awk ‘{print $2}‘"         stdin,stdout,stderr = s.exec_command(cmd)         cmd_result = stdout.read(),stderr.read()         for line in cmd_result:                         return line.strip("\n")         s.close()#print pssh(‘192.168.3.52‘,‘ls /root‘)def kssh(host,cmd):         user = ‘root‘         s = paramiko.SSHClient()         s.load_system_host_keys()         s.set_missing_host_key_policy(paramiko.AutoAddPolicy())         privatekeyfile = os.path.expanduser(‘~/.ssh/id_rsa‘)         mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)         # mykey=paramiko.DSSKey.from_private_key_file(privatekeyfile,password=‘061128‘)            s.connect(host,22,user,pkey=mykey,timeout=5)         #cmd=raw_input(‘cwd:‘)         #cmd="ip a | grep inet | grep 192.168  | grep brd | awk -F/22 ‘{print $1}‘|awk ‘{print $2}‘"         stdin,stdout,stderr = s.exec_command(cmd)         cmd_result = stdout.read(),stderr.read()         for line in cmd_result:                         return line.strip("\n")         s.close()def cmdreturn(ip,cmd):        try:                return kssh(ip,cmd)        except paramiko.AuthenticationException:                try:                        return pssh(ip,cmd)                except paramiko.AuthenticationException:                        return str(ip) + " passwd and key is fault"pwfile=open(‘/share/pwfile‘,‘w‘)ipsfile=open(‘/share/ipsfile‘,‘r‘)def chpwd(ip):        ostr=[‘_‘,‘%‘,‘@‘,‘!‘,‘-‘,‘=‘,‘+‘,‘$‘]        chars=string.ascii_letters+string.digits        xstr=[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘]        dstr=[‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘]        passwd=‘‘.join([random.choice(‘‘.join(xstr))])+ ‘‘.join([random.choice(‘‘.join(dstr))])+‘‘.join([random.choice(string.digits)])+‘‘.join([random.choice(‘‘.join(ostr))+‘‘.join([random.choice(chars) for i in range(12)])])#       passwd=random.choice(ostr)+‘‘.join([random.choice(chars) for i in  range(15)])+str(random.randint(0, 9))        chcmd="echo ‘root:"+passwd+"‘ | chpasswd"        cmdreturn(ip,chcmd)        pwfile.write(str(ip).strip() + "      "+str(passwd))        return str(ip).strip() + "      "+str(passwd)#       return chcmd+"\n"+str(ip).strip() + "   "+str(passwd)#echo "$i $passwd"#ssh [email protected]$i "echo ‘root:$passwd‘ | chpasswd"for ip in ipsfile:        print chpwd(ip)

系統檔案更改:

sed -i.orig ‘s,(password requisite pam_cracklib.so ).*,\1try_first_pass retry=3 minlen=16 dcredit=-1 ocredit=-1 lcredit=-1 ucredit=-1‘ /etc/pam.d/system-authsed -ri.orig ‘s,(PASS_MAX_DAYS).*,\1   90,g‘ /etc/login.defssed -ri ‘s,(PASS_MIN_LEN).*,\1    16,g‘ /etc/login.defs#####rollback#cp /etc/pam.d/system-auth.orig /etc/pam.d/system-auth#cp /etc/login.defs.orig /etc/login.defs

本文出自 “linux系統維護” 部落格,請務必保留此出處http://linuxadmin.blog.51cto.com/2683824/1674897

python關於it審計中的應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.