Python3 cannot find libdnet - Scapy port - python

I am trying to use the Python3 fork of Scapy in my project, but I am having trouble getting it running with all of its dependencies. I am currently running OSX Yosemite. In particular, it seems Python3 cannot find libdnet.so. I have Scapy working in Python2, so libdnet exists on my system - how do I get it working in Python3? Is there a supported version of libdnet for Python3? Error is as follows:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scapy/all.py", line 16, in <module>
from .arch import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scapy/arch/__init__.py", line 75, in <module>
from .bsd import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scapy/arch/bsd.py", line 12, in <module>
from .unix import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scapy/arch/unix.py", line 21, in <module>
from .pcapdnet import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scapy/arch/pcapdnet.py", line 22, in <module>
from .cdnet import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scapy/arch/cdnet.py", line 17, in <module>
raise OSError("Cannot find libdnet.so")
OSError: Cannot find libdnet.so
Thanks

I ran in to this same issue. I installed libdnet and libpcap with homebrew (e.g., in /usr/local/lib), but ctypes could never find them using find_library(). This is not a solution, but a hack to get scapy to import. I modified the following two files in my scapy installation to specify the full path to the libraries:
I changed find_library('dnet') in scapy/arch/cdnet.py to find_library('/usr/local/lib/libdnet')
I changed find_library('pcap') in scapy/arch/winpcapy.py to find_library('/usr/local/lib/libpcap')
Another less invasive idea is to just link these two libraries from /usr/local/lib to /usr/lib...

This is the code fragment that generates the exception.
from ctypes import *
from ctypes.util import find_library
_lib_name = find_library('dnet')
if not _lib_name:
raise OSError("Cannot find libdnet.so")
_lib=CDLL(_lib_name)
Apparently, python ctypes cannot find dnet library on your computer. Once you can get ctypes find dnet, it should work with scapy.
Also, usage of dnet is not mandatory. Try scapy with dnet disabled. You do not need it for parsing packets. And depending on the system, for some limited sending scapy can use pcap, too.
Please, file an issue on https://github.com/phaethon/scapy

Related

Python3 on Ubuntu giving errors on help() command

I used help() in the python3 shell on Ubuntu 14.04
I got this output
Please help , don't know whats wrong.
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/_sitebuiltins.py", line 98, in __call__
import pydoc
File "/usr/lib/python3.4/pydoc.py", line 65, in <module>
import platform
File "/home/omega/platform.py", line 2, in <module>
print("System : ",platform.uname().system)
AttributeError: 'module' object has no attribute 'uname'
>>>
The problem is that platform is the name of a stdlib module, which help uses. By creating a module of your own with the same name that occurs before the stdlib in your sys.path, you're preventing Python from using the standard one.
The fact that your own platform module tries to use the stdlib module of the same name just compounds the problem. That isn't going to work; your import platform inside that module is just importing itself.
The solution is to not collide names like this. Look at the list of the standard modules, and don't create anything with the same name as any of them if you want to use features from that module, directly or indirectly.
In other words: Rename your platform.py to something else, or put it inside a package.
File "/home/omega/platform.py", line 2, in <module>
print("System : ",platform.uname().system)
This is the problem, go to platform.py and fix it, it will be ok. It says, platform has not any method called uname you probably misstyped.

How to use .so modules from Python 2 in Python 3?

probably my question is obvious but I was not able to find an obvious decision.
There are Python 2.6+ extensions called audit and auparse. These are dynamical libraries distributed with audit-libs-python package:
[vitaly#thermaltake tmp]$ repoquery -lq audit-libs-python
/usr/lib64/python2.7/site-packages/_audit.so
/usr/lib64/python2.7/site-packages/audit.py
/usr/lib64/python2.7/site-packages/audit.pyc
/usr/lib64/python2.7/site-packages/audit.pyo
/usr/lib64/python2.7/site-packages/auparse.so
I would like to use this extension in the up-to-date Python interpreter because of suspicions about the incorrect handling of dynamic memory in python 2.6+. For some reason I cannot load them from Python 3.3:
[vitaly#thermaltake ~]$ python3.3
Python 3.3.2 (default, Mar 5 2014, 08:21:05)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/usr/lib64/python2.7/site-packages/")
>>> import auparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/lib64/python2.7/site-packages/auparse.so: undefined symbol: _Py_ZeroStruct
>>> import audit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/audit.py", line 28, in <module>
_audit = swig_import_helper()
File "/usr/lib64/python2.7/site-packages/audit.py", line 24, in swig_import_helper
_mod = imp.load_module('_audit', fp, pathname, description)
File "/usr/lib64/python3.3/imp.py", line 183, in load_module
return load_dynamic(name, filename, file)
ImportError: /usr/lib64/python2.7/site-packages/_audit.so: undefined symbol: PyInstance_Type
I would be glad if anyone could clarify the procedure of importing the modules of this kind into the modern Python interpreter. It's hard to believe that backward compatibility between 2nd and 3rd branches was broken in this case too. Thank you.
.so modules have to be compiled for each specific Python version - you can't even reuse an .so module built for Python 2.6 with Python 2.7.
When crossing over to Python 3 it gets worse, since there are some API changes, and the SO simply won't build unchanged from the .C file (with possible exceptions).
One workaround is to serve the functions you want to use in the 2.6 module with xmlrpc, then call then from a separate Python process runing Python 3.x - that should be the simplest way.

Undefined symbol "afinfo" when importing python-iptables package "iptc"

I am trying to utilize the python-iptables package to list iptables rules in a web app. When I add the iptc package to my environment, I get the below error. I used yum 'provides' to find where the libxtables.so.4 file comes from and found that the iptables and iptables-devel packages were the appropriate choice in CentOS 6.4 x64. I upgraded those packages but it did not change the error.
Does anyone have any suggestions about how I can resolve this?
pgrace#ny-misc01:~/repos/python-iptables/libxtwrapper$ python
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import iptc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.6/site-packages/iptc/__init__.py", line 10, in
from ip4tc import Table, Chain, Rule, Match, Target, Policy, IPTCError
File "/usr/lib64/python2.6/site-packages/iptc/ip4tc.py", line 11, in
from xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 744, in
class xtables(object):
File "/usr/lib64/python2.6/site-packages/iptc/xtables.py", line 757, in xtables
_xtables_afinfo = ct.c_void_p.in_dll(_lib_xtables, "afinfo")
ValueError: /lib64/libxtables.so.4: undefined symbol: afinfo
>>>
See this: https://github.com/ldx/python-iptables/issues/25
This is a known problem, old versions of libxtables declared afinfo as static, thus it is not accessible for python-iptables. There is a possible workaround, though - please keep an eye on the ticket, it will be updated as soon as there has been progress.
Another solution is to update iptables on your machine.
Disclaimer: I am the maintainer of python-iptables.
Update: this should be fixed now.

inconsistent behaviour of interactive session and script in Python

When I run the script, I had this ImportError:
$ python ~/Dropbox/code/py/ZoteroFindOrphanedFiles.py
Traceback (most recent call last):
File "/home/zane/Dropbox/code/py/ZoteroFindOrphanedFiles.py", line 1, in <module>
import sqlite3
File "/usr/lib/python3.2/sqlite3/__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "/usr/lib/python3.2/sqlite3/dbapi2.py", line 23, in <module>
import datetime
File "/usr/lib/python3.2/datetime.py", line 20, in <module>
import math as _math
File "/home/zane/Dropbox/code/py/math.py", line 3, in <module>
from nzmath.rational import Integer, Rational
ImportError: No module named nzmath.rational
But I don't have it when running the interactive session:
$ python
Python 3.2.3 (default, Apr 23 2012, 23:14:44)
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
Why is that?
Here is the problem:
import math as _math
File "/home/zane/Dropbox/code/py/math.py", line 3, in <module>
You have your own module called math.py, but this is the same as a standard Python module of the same name. This is not recommended.
The solution is to rename your math.py to something else, and don't forget to delete the math.pyc in the same directory (otherwise you'll still have the same problem).
You have a local file
/home/zane/Dropbox/code/py/math.py
which is getting imported instead of the standard lib math module.
The solution is to rename your /home/zane/Dropbox/code/py/math.py to something else.
The problem occurs when you call a script in the /home/zane/Dropbox/code/py directory since this adds this directory to the beginning of sys.path and thus this directory gets searched first when Python tries to import modules.
You have a file called math.py in the script's directory, and this shadows the stdlib math module. Rename the file.

Unable to import RRDtool in Python

I am trying to import RRDtool into Python as I want to access an RRD database using Python, but when I am trying to import rrdtool I am getting the following error.
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/opt/rrdtool-1.4.5/bin')
>>> import rrdtool
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rrdtool
My RRDtool is located in /opt/rrdtool-1.4.5/bin.
Well, the problem is solved just by executing the following command.
sudo apt-get install python-rrd
You probably need py-rrdtool, which you could get directly from the site or your package manager.
It is unlikely that Python modules are located inside a 'bin' folder. And importing files from some configured path requires that the referred path is a proper Python package. It means it must contain an __init__.py file.
You could use the RRDtool documentation for inspiration (man rrdpyton). Something
along the lines of
import sys
sys.path.append('/path/to/rrdtool/lib/python2.6/site-packages/')
import rrdtool
should do the trick.

Categories