【问题标题】:Python Shellexecute windows api with Ctypes over TCP/IPPython Shellexecute windows api 与 Ctypes over TCP/IP
【发布时间】:2017-01-11 10:15:00
【问题描述】:

我对通过 TCP/IP 协议运行 Windows API 有疑问。 例如,我想把远程机器的 cmd.exe 带到其他机器上(像 Netcat,完全模拟 TCP/IP 上的 cmd.exe)。我在网上搜索了用 python 来做,但找不到任何有用的东西。我可以使用 subprocess 和其他 python 功能来做到这一点,但它缺乏用户界面问题。我使用了这种代码:

import socket
import subprocess
import ctypes

HOST = '192.168.1.22'
PORT = 443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

while 1:
    #send initial cmd.exe banner to remote machine 
    s.send(ctypes.windll.Shell32.ShellExecuteA(None,"open","cmd.exe",None,None,0))
    #accept user input
    data = s.recv(1024)
    #pass it again to Shellexecute api to execute and return output to remote machine, fully simulating original CMD over TCP/IP


s.close()

看这张图: Running CMD over TCP/IP using NetCat tool

【问题讨论】:

    标签: python windows network-programming tcp-ip netcat


    【解决方案1】:

    根据您的描述,我不确定您的需求是什么,但我会尽力帮助您:)。

    如果您只想在远程计算机上激活 CMD.exe 并打开一个端口,您可能希望查看一个“服务器脚本”,该脚本将执行传递给它的任何命令。 即

    class MyTCPHandler(socketserver.BaseRequestHandler):
        """
        The RequestHandler class for our server.
    
        It is instantiated once per connection to the server, and must
        override the handle() method to implement communication to the
        client.
        """
    
        def handle(self):
            # self.request is the TCP socket connected to the client
            self.data = self.request.recv(1024).strip()
            print("{} wrote:".format(self.client_address[0]))
            print(self.data)
            error_code = os.system(self.data.decode('UTF-8'))
            # just send back the error code so we know if it executed correctly
            self.request.sendall(error_code)
    
    if __file__ is '__main__':
        HOST, PORT = "localhost", 9999
    
        # Create the server, binding to localhost on port 9999
        server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
    
        # Activate the server; this will keep running until you
        # interrupt the program with Ctrl-C
        server.serve_forever()
    

    *参考 :https://docs.python.org/3.4/library/socketserver.html 这个就不用说了,真的不安全……

    如果你真的想把它传递给服务器端的python,我想我会把socket上收到的所有东西都放在一个tmp文件中,然后在它上面执行python。像 Ansible 一样。

    如果这对你有帮助,请告诉我!

    【讨论】:

    • 我想通过网络连接运行 CMD。像反向或绑定 cmd shell。连接成功后,其中一台计算机必须控制属于另一台计算机的 cmd.exe。我想编写脚本模拟交互式 cmd.exe 通过网络连接属于其他计算机。可能,通过网络使用 shellexecute 之类的 windows api 是解决方案,但我是 python 新手,不能为自己编写:)
    • hum...,将 SSH 连接到 Windows 并使用 cmd 作为“默认”shell 是否适合您的情况? winscp.net/eng/docs/guide_windows_openssh_server 之后您可以使用 Python 中的 paramiko 库 :) jessenoller.com/blog/2009/02/05/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多