您的位置:首页 > 编程语言 > Python开发

python-1-破解unix密码/etc/shadow

2017-07-04 16:11 1076 查看
1、unix系统密码文件/etc/shadow

其中第二字段为加密后的密码(*开头为不能登录的用户,!!开头为过期的用户,也不能登录)

第二字段格式

$id$salt$encrypted

id代表hash算法:

ID算法
$1$MD5
$2a$Blowfish
$5$SHA-256
$6$SHA-512

另一种salt概念:

这个Salt用于对明文加密

Salt={$id$固定字数随机字符$}

(普通字符的固定字数为2,含有特殊字符的固定字数也是一个随机数,特殊字符/.等)

2、方法或函数

a)索引

start_index=lst.find("$")                      #获取第一个$字符的索引值
finish_index=lst.rfind("$")                    #获取最后一个$字符出现的位置的索引值(从右往左查询)
Salt=lst[start_index:finish_index+1]           #获取加密salt字符串


b)方法

sys.argv()             #获得参数的个数
str.startswith(str)    #检查字符串是否以指定字符开头


3、代码

import crypt
import sys

def testPass(cryptPass):

#dictfile=open('dictionary.txt','r')
start_index=cryptPass.find("$")
finish_index=cryptPass.rfind("$")
salt=cryptPass[start_index:finish_index+1]

try:
dictfile=open(sys.argv[2],'r')
except Exception,e:
print "Error ! "+str(e)
exit(0)

for word in dictfile.readlines():
word=word.strip('\n')
cryptWord=crypt.crypt(word,salt)
if cryptWord==cryptPass:
print "[+] Found Password: " +word+ " \n"
return
print('[-] Password not found!')
return

def main():

if len(sys.argv)==3:
try:
shadowfile=open(sys.argv[1])
except Exception,e:
print "Error !" +str(e)
exit(0)
else:
print "Usage:python ShadowCracker.py [shadow file] [dictionary file]"
exit(0)

passfile=open('passwords.txt','r')

for line in passfile.readlines():
user=line.split(':')[0]
cryptPass=line.split(':')[1].strip('\n')
print "[*] Cracking Password For: " +user
testPass(cryptPass)

if __name__=='__main__':
main()


4、问题

a)语句错误,在第一个word不匹配时就过早判断password not found,不能完整的遍历字典

for word in dictfile.readlines():
word=word.strip('\n')
cryptWord=crypt.crypt(word,salt)
if cryptWord==cryptPass:
print "[+] Found Password: " +word+ " \n"
return
else:
print('[-] Password not found!')
return


b)参数传递进函数,最好直接读取/etc/shadow,避免cat到另外文件出错导致无法破解或者salt值错误、加密的密码不对等问题。

5、 优化

a)在代码中加入异常判断

b)排除无法登录的用户

if not (cryptPass.startswith('*') or cryptPass.startswith('!')):


c)重定向日志

d)改进为局域网型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: