欢迎光临!
若无相欠,怎会相见

Python学习之Unix密码破解器

学习 ing

最近在看这本书《python绝技:运用python成为顶级黑客》,从这本书的封面可以知道这本书来自原国内的漏洞平台–乌云,可惜的是,该平台自2016年7月20日就挂上了升级维护的页面。从segmentfault的相关文章了解,乌云可能关闭了。

不说乌云了,该书的第一个程序就是Unix密码破解器,理所当然,我也是要自己写出来。

问题

一般来说,编写代码出问题是经常的事儿。结果,我在导入crypt模块的时候就出问题了。也就是 import crypt 出问题了。

>>> import crypt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named crypt

那么,我就查看本机的python环境到底安装的有没有 crypt 模块。

C:\Users\Darker
λ python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help("modules")

Please wait a moment while I gather a list of all available modules...

BaseHTTPServer      argparse            imghdr              shelve
Bastion             array               imp                 shlex
CGIHTTPServer       ast                 importlib           shutil
Canvas              asynchat            imputil             signal
ConfigParser        asyncore            inspect             site
Cookie              atexit              io                  smtpd
Dialog              audiodev            itertools           smtplib
DocXMLRPCServer     audioop             json                sndhdr
FileDialog          base64              keyword             socket
FixTk               bdb                 lib2to3             sqlite3
HTMLParser          binascii            linecache           sre
MimeWriter          binhex              locale              sre_compile
Queue               bisect              logging             sre_constants
ScrolledText        bsddb               macpath             sre_parse
SimpleDialog        bz2                 macurl2path         ssl
SimpleHTTPServer    cPickle             mailbox             stat
SimpleXMLRPCServer  cProfile            mailcap             statvfs
SocketServer        cStringIO           markupbase          string
StringIO            calendar            marshal             stringold
Tix                 cgi                 math                stringprep
Tkconstants         cgitb               md5                 strop
Tkdnd               chunk               mhlib               struct
Tkinter             cmath               mimetools           subprocess
UserDict            cmd                 mimetypes           sunau
UserList            code                mimify              sunaudio
UserString          codecs              mmap                symbol
_LWPCookieJar       codeop              modulefinder        symtable
_MozillaCookieJar   collections         msilib              sys
__builtin__         colorsys            msvcrt              sysconfig
__future__          commands            multifile           tabnanny
_abcoll             compileall          multiprocessing     tarfile
_ast                compiler            mutex               telnetlib
_bisect             contextlib          netrc               tempfile
_bsddb              cookielib           new                 test
_codecs             copy                nmap                textwrap
_codecs_cn          copy_reg            nntplib             this
_codecs_hk          csv                 nt                  thread
_codecs_iso2022     ctypes              ntpath              threading
_codecs_jp          curses              nturl2path          time
_codecs_kr          datetime            numbers             timeit
_codecs_tw          dbhash              opcode              tkColorChooser
_collections        decimal             operator            tkCommonDialog
_csv                difflib             optparse            tkFileDialog
_ctypes             dircache            os                  tkFont
_ctypes_test        dis                 os2emxpath          tkMessageBox
_elementtree        distutils           parser              tkSimpleDialog
_functools          doctest             pdb                 toaiff
_hashlib            dumbdbm             pickle              token
_heapq              dummy_thread        pickletools         tokenize
_hotshot            dummy_threading     pip                 trace
_io                 easy_install        pipes               traceback
_json               email               pkg_resources       ttk
_locale             encodings           pkgutil             tty
_lsprof             ensurepip           platform            turtle
_md5                errno               plistlib            types
_msi                exceptions          popen2              unicodedata
_multibytecodec     filecmp             poplib              unittest
_multiprocessing    fileinput           posixfile           urllib
_osx_support        fnmatch             posixpath           urllib2
_pyio               formatter           pprint              urlparse
_random             fpformat            profile             user
_sha                fractions           pstats              uu
_sha256             ftplib              pty                 uuid
_sha512             functools           py_compile          virtualenv
_socket             future_builtins     pyclbr              virtualenv_support
_sqlite3            gc                  pydoc               warnings
_sre                genericpath         pydoc_data          wave
_ssl                getopt              pyexpat             weakref
_strptime           getpass             quopri              webbrowser
_struct             gettext             random              whichdb
_subprocess         glob                re                  winsound
_symtable           gzip                repr                wsgiref
_testcapi           hashlib             rexec               xdrlib
_threading_local    heapq               rfc822              xml
_tkinter            hmac                rlcompleter         xmllib
_warnings           hotshot             robotparser         xmlrpclib
_weakref            htmlentitydefs      runpy               xxsubtype
_weakrefset         htmllib             sched               yolk
_winreg             httplib             select              zipfile
abc                 idlelib             sets                zipimport
aifc                ihooks              setuptools          zlib
antigravity         imageop             sgmllib
anydbm              imaplib             sha

结果,就是没有crypt模块。无奈,上网查找解决方案,在问答网站 stackoverflow.com 上知道了,crypt模块是Unix独有的,在Windows平台上没有,所以,就不纠结了。

代码

# -*- coding: utf-8 -*-
# @Time    : 2017/11/14 20:25
# @Author  : Darker
# @Site    : www.liangz.org
# @File    : crypt.py
# @Software: PyCharm

"""
暴力破解UNIX的密码,需要输入字典文件和UNIX的密码文件
"""
import crypt


def testPass(cryptPass):
    salt = cryptPass[0:2]
    dictfile = open('dictionary.txt', 'r') #以只读方式打开字典文件
    for word in dictfile.readlines():
        word = word.strip('\n') #保留原始的字符,不去空格
        cryptWord = crypt.crypt(word, salt)
        if cryptPass == cryptWord:
            print('Found passed : ', word)
            return
        print('Password not found !')
        return


def main():
    passfile = open('passwords.txt', 'r') #读取密码文件
    for line in passfile.readlines():
        user = line.split(':')[0]
        cryptPass = line.split(':')[1].strip('')
        print("Cracking Password For :", user)
        testPass(cryptPass)


if __name__ == '__main__':
    main()

以上就是本次内容。

赞(0) 打赏
转载请注明:飘零博客 » Python学习之Unix密码破解器
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临