raw sockets made easy
Only in linux, sorry.
Rationale
If you need raw sockets in your python script, you have to run it as root or make it SUID root. Alternatively, you can use file capabilities to grant extended priviliges to a binary. Unfortunately, this only works on binaries and not on scripts, which means you have to set the capability on the python binary.
How does it work?
As mentioned above, you can only set capabilties on a binary. Therefore, this module has a helper binary, which will get the RAW_NET capability. This binary is spawned by the rawsocket python module every time you create a new raw socket. The helper uses its capability to create the socket and then passes the file descriptor back to the calling program.
In linux you can pass file descriptors through unix domain sockets by using the sendmsg() system call and SCM_RIGHTS. See also fd passing.
Installation
Use PIP:
pip install rawsocket
Or use the standard method:
python setup.py install
This will install the extension module and the helper binary. But it will not set the file capability on the helper binary. You have two choices:
Grant every user access to the helper (simple, not recommended):
setcap cap_net_raw+ep rawsocket-helper
Grant access only to users which are member of a specific group (recommended):
chmod 750 rawsocket-helper chown root:yourgroup rawsocket-helper setcap cap_net_raw+ep rawsocket-helper
Example
import socket, rawsocket
fd = rawsocket.rawsocket_fd()
s = socket.fromfd(fd, socket.AF_PACKET, socket.SOCK_RAW)
You see easy as pie ;)
License
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Metadata
Release files for rawsocket 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rawsocket-0.2.tar.gz | 5.6 kB | Without provenance | Details for rawsocket-0.2.tar.gz |
Release files / rawsocket-0.2.tar.gz
| Download URL | rawsocket-0.2.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
03222ed4f86eaa8aaa7c609fc69b3735eddf0ad98db4bbb5e907958600b8f85a
|
|
BLAKE2b-256 checksum How to use checksums |
00f28899d4ea1fce463f27c0f98b66a325822b735835c527c4a17d8251abd267
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3
|
Security for rawsocket 0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.