SHOW:
|
|
- or go back to the newest paste.
1 | # Import a couple of modules | |
2 | ||
3 | import paramiko # For High level networking | |
4 | import os # Basic shell usage | |
5 | import getpass # Allows user input without echoing | |
6 | import sys # Allows basic system commands | |
7 | import time # Works with local time | |
8 | ||
9 | ||
10 | # Initialise the class as an object. Create a couple of functions inside. | |
11 | # __init__ is present in every class, Instantiation which occurs before any function is called | |
12 | # Even if __init__ properties arent specified, it still exists. | |
13 | ||
14 | class startUP(object): | |
15 | ||
16 | def __init__(self): | |
17 | ||
18 | execComm = execCommand() | |
19 | ||
20 | ||
21 | class execCommand(object): | |
22 | ||
23 | # Set a couple of global class variables and clear the shell | |
24 | ||
25 | ||
26 | os.system(['clear', 'cls'][os.name == 'nt']) | |
27 | ||
28 | _host = raw_input("\n\n[!] Please Enter Remote IP: ") | |
29 | _port = raw_input("[!] Please Enter Target Port: ") | |
30 | _port = int(_port) | |
31 | _uname = raw_input("[!] Please Enter Target Username: ") | |
32 | _passwd = getpass.getpass("[!] Please Enter Target Password: ") | |
33 | ||
34 | ||
35 | # Example of multi line printing | |
36 | ||
37 | def __init__(self): | |
38 | print(''' | |
39 | __ __ | |
40 | ( ) _ - _ ( ) | |
41 | ~~ ~~ | |
42 | ( 0 0 ) | |
43 | --------ooO----( )----Ooo-------- | |
44 | """ ( ) """ | |
45 | ( ) | |
46 | \ / | |
47 | ~~O~~ | |
48 | \n''') | |
49 | ||
50 | # Print a few options to screen, pairing data structures together with the zip function creating a menu | |
51 | ||
52 | print('\n\nJoin me in the sewers...\nIt may be time to search for RATs\n\n') | |
53 | ||
54 | self._Item = ['Menu', '1', '2', '0'] | |
55 | self._Desc = ['Job List\n\n', 'Bad Samaritan', 'Retrieval Job\n', 'Great Escape\n'] | |
56 | ||
57 | for q, a in zip(self._Item, self._Desc): | |
58 | print('{0}: The {1}'.format(q, a)) | |
59 | ||
60 | ||
61 | # Ask for user input and set the received value as an integer. | |
62 | ||
63 | self._Choice = raw_input('Tell Me What To Do..: ') | |
64 | self._Choice = int(self._Choice) | |
65 | ||
66 | # Function Variables | |
67 | ||
68 | self._List = str('') | |
69 | self. _Str = str(self._List) | |
70 | self._UI = str('') | |
71 | ||
72 | # Basic if, else if and else sequence. Various different functions are called depending on response | |
73 | ||
74 | try: | |
75 | ||
76 | if self._Choice == 1: | |
77 | execCommand.paraMiko(self) | |
78 | ||
79 | elif self._Choice == 2: | |
80 | execCommand.SFTP(self) | |
81 | ||
82 | # System Exit exits the script | |
83 | else: | |
84 | paramiko.SFTPClient().close() | |
85 | paramiko.SSHClient().close() | |
86 | raise SystemExit | |
87 | ||
88 | except Exception, e: | |
89 | print e | |
90 | ||
91 | def paraMiko(self): | |
92 | ||
93 | # Save time by defining the paramiko client. Set logging file local host side. | |
94 | # Append host keys to known_host file using paramikos auto add policy. | |
95 | # Connect to the host using the specified global class variables. | |
96 | # If system allows it, call for a pseudo terminal. Grab banner from the server. | |
97 | # Start bash string with a parenthases. Start the loop function. | |
98 | # Setting a timeout of 2 seconds to allow buffer to fill (Banner grab) | |
99 | ||
100 | try: | |
101 | ||
102 | self.ssh = paramiko.SSHClient() | |
103 | paramiko.util.log_to_file('/tmp/parakito.log') | |
104 | self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
105 | self.ssh.connect(execCommand._host, execCommand._port, username=execCommand._uname, | |
106 | password=execCommand._passwd, timeout=2) | |
107 | self._Chan = self.ssh.invoke_shell() | |
108 | print ('\n[+] Connected\n') | |
109 | ||
110 | self._Data = self._Chan.recv(2014) | |
111 | print '[+] Host Message Of The Day\n' + str(self._Data) | |
112 | ||
113 | self._Str += '(' | |
114 | execCommand.loop(self) | |
115 | ||
116 | ||
117 | except Exception, e: | |
118 | print e | |
119 | finally: | |
120 | self.ssh.close() | |
121 | ||
122 | def loop(self): | |
123 | ||
124 | # Question loop starts here, within its own function. | |
125 | ||
126 | try: | |
127 | self._UI = raw_input("SSH > ") | |
128 | execCommand.Verify(self) | |
129 | ||
130 | except ValueError: | |
131 | print("Apologies, I Wasnt Expecting That. Try Again...") | |
132 | execCommand.loop(self) | |
133 | ||
134 | def Verify(self): | |
135 | ||
136 | # This function loops the question, adding each input to the string (self._str) | |
137 | # If Disconnect is present in the return, we print the concatenated string output | |
138 | # It then asks us if we are ready to proceed, if so it will execute the bash string | |
139 | # To the server. '> /home/notalog.txt' is a way of saying, save all output to this file. | |
140 | ||
141 | try: | |
142 | if self._UI == str('Disconnect'): | |
143 | ||
144 | self._Str += 'uptime) > /home/notalog.txt' | |
145 | print('\n\n'+self._Str) | |
146 | ||
147 | self._Req = raw_input('\n[!] Fingers Trembling, Ready To Send? [Execute/Stop]: ') | |
148 | if self._Req == str("Execute"): | |
149 | print('\n[+] Issuing Commands To Server..\n') | |
150 | execCommand.Push(self) | |
151 | else: | |
152 | execCommand.loop(self) | |
153 | else: | |
154 | ||
155 | self._Str += str((self._UI))+ '; ' | |
156 | execCommand.loop(self) | |
157 | ||
158 | ||
159 | except Exception, e: | |
160 | print e | |
161 | ||
162 | def Push(self): | |
163 | ||
164 | # Standard (input, output, error) are declared as result of the execute command function | |
165 | # In paramiko. Executing the class variable self._Str as the command to be executed. | |
166 | # Wait for user input, then go to main menu. | |
167 | ||
168 | try: | |
169 | ||
170 | self._stdin, self._stdout, self._stderr = self.ssh.exec_command(self._Str) | |
171 | raw_input('\nShall We Proceed?..') | |
172 | execCommand() | |
173 | ||
174 | ||
175 | except Exception, e: | |
176 | print e | |
177 | self.ssh.close() | |
178 | ||
179 | ||
180 | ||
181 | ||
182 | def SFTP(self): | |
183 | ||
184 | # Using the transport function from paramiko using the global class variables host, port | |
185 | # Connecting using the global class variables uname and password | |
186 | ||
187 | # Setting the local and remote paths. SFTP.get takes two arguments, a send and a receive | |
188 | # This sequence can be reversed when using the SFTP.put method to upload a file instead of download. | |
189 | # The prog bar calculates transfer time and displays this in the interpreter | |
190 | ||
191 | def progBar(transferred, toBeTransferred): | |
192 | print("\n[+] Transferred: {0}\tOut Of: {1}".format(transferred, toBeTransferred)) | |
193 | ||
194 | try: | |
195 | ||
196 | self._T = paramiko.Transport((execCommand._host, execCommand._port)) | |
197 | self._T.connect(username=execCommand._uname, password = execCommand._passwd) | |
198 | self._SFTP = paramiko.SFTPClient.from_transport(self._T) | |
199 | ||
200 | self._remotepath='/home/notalog.txt' | |
201 | self._localpath='/home/Spawn/Desktop/SFTP/notalog.txt' | |
202 | ||
203 | ||
204 | self._SFTP.get(self._remotepath,self._localpath, callback=progBar) | |
205 | print('\n[+] File Successfully Sent') | |
206 | raw_input("\n[!] Ready To Proceed?") | |
207 | self._SFTP.close() | |
208 | startUP() | |
209 | ||
210 | except Exception, e: | |
211 | print e | |
212 | ||
213 | try: | |
214 | # Classes are initialised by defining their name within a function | |
215 | ||
216 | s = startUP() | |
217 | ||
218 | except KeyboardInterrupt: | |
219 | ||
220 | # Keyboard interrupt; Control - C. | |
221 | # Using the time module to implement delays in messages | |
222 | # Raw input at the end waiting for enter press | |
223 | ||
224 | paramiko.SSHClient().close() | |
225 | print('\n\n[+] Connection Terminated By User, Closing SSH Connection..') | |
226 | time.sleep(0.5) | |
227 | print('\n[+] SSH Connection Terminated Successfully..') |