简体   繁体   中英

Python bind cmd.exe to port

How do I bind cmd.exe onto a port in Python? I want to do the same thing as Netcats "-e" argument. So the equivilent in Netcat would be:

netcat -l -p 8080 -e cmd.exe

But I want to code it myself in Python, without using Netcat. So how is this done? Are there any functions/modules that can do this? How can I convert the process (cmd.exe) and make it a server so it runs on a port?

  1. Listen to a port
  2. Read the input
  3. Pipe it to cmd.exe
  4. Send back the output

Something along the lines of this, except you would have to change it into running on Windows (this example runs fine on Linux):

#!/usr/bin/env python

import socket
import subprocess

s = socket.socket(socket.AF_INET)
s.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
s.bind(("", 9999))
s.listen(1)
(conn, address) = s.accept()

p = subprocess.Popen(["/bin/bash"], 
                     stdin=conn, stdout=conn, stderr=conn)

If you run this program, and then in another terminal use netcat to connect to port 9999, you'll have a bash shell to play with. Be careful not to let the whole internet get access to this port, that would give anyone instant shell access on your machine:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM