Home > database >  Python3 Sockets - PermissionError: [Errno 1] Operation not permitted - running as root
Python3 Sockets - PermissionError: [Errno 1] Operation not permitted - running as root

Time:05-28

I'm having a hard time with this small socket program I have. The script runs fine one on Debian 11 server, but has the following error on an almost identical Debian 11 server.

root@box:/home/user/python# sudo python3 script.py
Traceback (most recent call last):
  File "/home/user/python/script.py", line 18, in <module>
    UDPClientSocket.sendto(bytesToSend, serverAddress) # Send using created UDP socket
PermissionError: [Errno 1] Operation not permitted

The file is owned by root and executable.

-rwxr-xr-x 1 root root 942 May 27 16:24 script.py

Here is the script, its pretty basic.

import socket
from Cryptodome.Cipher import AES

serverAddress   = ("192.168.1.3", 3493)  #Packet Destination - IP/Port pair
sourceAddress   = ("192.168.3.2", 54921) #Source Socket - IP/Port pair
bufferSize      = 1024

data=b"garbage"          # message to send to server
key=b"xxxxxxxxxxxxxxxx"                           # key to encrypt data
salt=b'xxxxxxxxxxxxxxxx'                        # salt
obj=AES.new(key, AES.MODE_CBC, salt)            # set crypt settings
bytesToSend=obj.encrypt(data)                   # encrypt the data

UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) # Create a UDP socket at client side
UDPClientSocket.bind((sourceAddress)) # Set local socket for source ip/port
UDPClientSocket.sendto(bytesToSend, serverAddress) # Send using created UDP socket

I am running as root, script is owned by root, what is wrong here?

Is there something I am doing in the script that is not allowed by the OS?

Updates:

Uname for the box it does run on: Linux box 5.10.0-14-amd64 #1 SMP Debian 5.10.113-1 (2022-04-29) x86_64 GNU/Linux

Uname for the box it does not run on: Linux box 5.10.0-13-amd64 #1 SMP Debian 5.10.106-1 (2022-03-17) x86_64 GNU/Linux

Python versions are the same, 3.9.2.

Interface listing for the box I am attempting to run my script on:

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: net1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 04:d9:f5:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    altname enp2s0
    inet 192.168.3.2/28 brd 192.168.3.15 scope global net1
       valid_lft forever preferred_lft forever

CodePudding user response:

I figured it out, mistake on my part.

Turns out it was the firewall. I have iptables with output filters, and forgot the iptables-save before the last reboot, so the output rule allowing the UDP sendto was denied.

Figured it out when I attempted ncat in udp mode and it failed with WRITE ERROR

I appreciate everyone's input though.

CodePudding user response:

Run this to update


pip install --upgrade setuptools pip

try again

  • Related