简体   繁体   English

运行Python代码抓取所有用户权限的live.network流量

[英]Run Python Code to capture live network traffic accessing all users rights

I am working on a virtual Machine by Oracle in Linux and my Linux version is 20.04 LTS and python version is 3.8.我正在 Linux 中的 Oracle 虚拟机上工作,我的 Linux 版本是 20.04 LTS,python 版本是 3.8。 I have a python code which is used to capture live.network traffic and save in a pcap file.我有一个 python 代码,用于捕获 live.network 流量并保存在 pcap 文件中。 The issue I am facing is that I want to run this code without any root or administrative or without sudo command.我面临的问题是我想在没有任何 root 或管理或没有 sudo 命令的情况下运行此代码。

test.py:测试.py:

import time
from scapy.all import *
import uuid

# IP address to capture packets for
ip_address = "0.0.0.0"

start_time = time.time()
end_time = start_time + 10  # Capture for 10 seconds

# Create an empty list to store the packets
packets = []

def packet_callback(packet):
    if IP in packet:
        packets.append(packet)

# Create a capture filter to capture only packets from the specified IP address 
capture_filter = "ip host " + ip_address

sniff(prn=packet_callback, timeout=end_time-start_time, filter= capture_filter)

# Write all the packets to a single pcap file
filename = "/path_to_capture/capture_" + str(uuid.uuid1()) + ".pcap"
wrpcap(filename, packets)
print(len(packets))

I run my code in terminal by writing the following cmd:我通过编写以下 cmd 在终端中运行我的代码:

sudo python3 test.py

By running this command, my code executes and capture the.network traffic and save in a pcap file.通过运行此命令,我的代码将执行并捕获 .network 流量并保存在 pcap 文件中。 But I want to execute my code without sudo or root privileges by writing a simple cmd:但是我想通过编写一个简单的 cmd 来在没有 sudo 或 root 权限的情况下执行我的代码:

python3 test.py

Kindly provide me the solution to resolve this issue.请为我提供解决此问题的解决方案。

I think that you can use the 'setcap' command in linux.我认为您可以在 linux 中使用“setcap”命令。

I have a experience of only 'C' language.我只有'C'语言的经验。

See the below link.请参阅以下链接。 (I'm not sure about Python) (我不确定 Python)

https://medium.com/@badbot/safe-packet-capture-python-without-sudo-b08c4c4e531 https://medium.com/@badbot/safe-packet-capture-python-without-sudo-b08c4c4e531

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM