PDA

View Full Version : مشکل در packet capture



hirad000
چهارشنبه 12 فروردین 1394, 17:34 عصر
سلام دوستان
در برنامه ای می خوام پکت کپچر رو تست کنم .نمیدونم چرا هیچ دیوایسی رو قبول نمیکنه؟ تو لینوکس این رو اجرا می کنم. مشکل از سیستم عامل یا چیز دیگه ای؟
اگر میشه حتما کمک کنید.مرسی

import pcapy
import dpkt

class network_monitor:
def __init__ (self):
pass

def start (self):
# TODO: specify a device or select all devices
# dev = pcapy.findalldevs()[0]
dev = 'en1'
p = pcapy.open_live(dev, 65536, False, 1)
p.loop(-1, self.handle_packet)

def handle_packet (self, header, data):
eth = dpkt.ethernet.Ethernet (data)
# print "%04X" % eth.type
if eth.type == dpkt.ethernet.ETH_TYPE_IP:
ip = eth.data
ip_data = ip.data
if isinstance (ip_data, dpkt.udp.UDP):
udp = ip_data
if udp.sport == 137:
nb = dpkt.netbios.NS(udp.data)

print "NetBIOS:"
for q in nb.qd:
print 'qd:', dpkt.netbios.decode_name(q.name)
for a in nb.an:
print 'an:', dpkt.netbios.decode_name(a.name)
for n in nb.ns:
print 'ns:'. dpkt.netbios.decode_name(n.name)
print ''
if udp.dport == 5353:
mdns = dpkt.dns.DNS (udp.data)
print "MDNS:"
print mdns.qd
print mdns.an
print mdns.ns


def main():
network_monitor ().start ()

if __name__=="__main__":
main ()

ارورش اینه که : p = pcapy.open_live(dev, 65536, False, 1)
PcapError: en1: You don't have permission to capture on that device (socket: Operation not permitted)
>>>

n.nowroozi
پنج شنبه 13 فروردین 1394, 12:07 عصر
مشکل دسترسی داری! با یوزر روت برنامه رو اجرا کن حل میشه

hirad000
پنج شنبه 13 فروردین 1394, 18:06 عصر
مشکل دسترسی داری! با یوزر روت برنامه رو اجرا کن حل میشه

با یوزر روت وارد میشم. لینوکسم یوزر دیگه ای نداره.بازم حق دسترسی نمیده

n.nowroozi
جمعه 14 فروردین 1394, 08:49 صبح
با sudo اجرا کن.. یوزرت روت نیست.

hirad000
جمعه 14 فروردین 1394, 10:47 صبح
با sudo اجرا کن.. یوزرت روت نیست.

با سودو اجرا کنم یعنی چی ؟!
من همین برنامه ای که گذاشتم اینجا رو ی بار با خود پایتون اجرا کردم که بهم حق دسترسی نمیده.با ترمینال دستور cap.py/. میزنم از برنامم ایراد میگیره در صورتی که ایراد نداره. میگه :
./cap.py: line 1: import: command not found
./cap.py: line 2: import: command not found
./cap.py: line 4: class: command not found
./cap.py: line 5: syntax error near unexpected token `('
./cap.py: line 5: ` def __init__ (self):'

دستور ifconfig-a رو میزنم خروجیم اینه :
eth0 Link encap:Ethernet HWaddr 00:0c64fe
inet addr:192.168.160.130 Bcast:192.168.160.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe64:64fe/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9521 errors:0 dropped:0 overruns:0 frame:0
TX packets:5286 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9629771 (9.6 MB) TX bytes:628540 (628.5 KB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1131 errors:0 dropped:0 overruns:0 frame:0
TX packets:1131 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:172600 (172.6 KB) TX bytes:172600 (172.6 KB)

مشکلی نداره؟
چون ی برنامه ی مشابه دارم که وقتی تو ترمینال اجرا می کنم میگه : bash: ./test.py: Permission denied

n.nowroozi
جمعه 14 فروردین 1394, 12:28 عصر
sudo python test.py
خب شیوه اجراتون غلطه چون برای فایل پایتونتون شبنگ (http://stackoverflow.com/questions/15587877/run-a-python-script-in-terminal-without-the-python-command) نگذاشتید لینوکس اون رو به عنوان فایل bash داره اجرا میکنه و این باعث میشه که ارور بده.

hirad000
پنج شنبه 27 فروردین 1394, 00:31 صبح
sudo python test.py
خب شیوه اجراتون غلطه چون برای فایل پایتونتون شبنگ (http://stackoverflow.com/questions/15587877/run-a-python-script-in-terminal-without-the-python-command) نگذاشتید لینوکس اون رو به عنوان فایل bash داره اجرا میکنه و این باعث میشه که ارور بده.


'''Packet sniffer in python using the pcapy python library

Project website

http://oss.coresecurity.com/projects/pcapy.html

'''

import socket
from struct import *
import datetime
import pcapy
import sys

def main(argv):
#list all devices
devices = pcapy.findalldevs()
print devices

#ask user to enter device name to sniff
print "Available devices are :"
for d in devices :
print d

dev = raw_input("Enter device name to sniff : ")

print "Sniffing device " + dev

'''
open device
# Arguments here are:
# device
# snaplen (maximum number of bytes to capture _per_packet_)
# promiscious mode (1 for true)
# timeout (in milliseconds)
'''
cap = pcapy.open_live(dev , 65536 , 1 , 0)

#start sniffing packets
while(1) :
(header, packet) = cap.next()
#print ('%s: captured %d bytes, truncated to %d bytes' %(datetime.datetime.now(), header.getlen(), header.getcaplen()))
parse_packet(packet)

#Convert a string of 6 characters of ethernet address into a dash separated hex string
def eth_addr (a) :
b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
return b

#function to parse a packet
def parse_packet(packet) :

#parse ethernet header
eth_length = 14

eth_header = packet[:eth_length]
eth = unpack('!6s6sH' , eth_header)
eth_protocol = socket.ntohs(eth[2])
print 'Destination MAC : ' + eth_addr(packet[0:6]) + ' Source MAC : ' + eth_addr(packet[6:12]) + ' Protocol : ' + str(eth_protocol)

#Parse IP packets, IP Protocol number = 8
if eth_protocol == 8 :
#Parse IP header
#take first 20 characters for the ip header
ip_header = packet[eth_length:20+eth_length]

#now unpack them :)
iph = unpack('!BBHHHBBH4s4s' , ip_header)

version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF

iph_length = ihl * 4

ttl = iph[5]
protocol = iph[6]
s_addr = socket.inet_ntoa(iph[8]);
d_addr = socket.inet_ntoa(iph[9]);

print 'Version : ' + str(version) + ' IP Header Length : ' + str(ihl) + ' TTL : ' + str(ttl) + ' Protocol : ' + str(protocol) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr)

#TCP protocol
if protocol == 6 :
t = iph_length + eth_length
tcp_header = packet[t:t+20]

#now unpack them :)
tcph = unpack('!HHLLBBHHH' , tcp_header)

source_port = tcph[0]
dest_port = tcph[1]
sequence = tcph[2]
acknowledgement = tcph[3]
doff_reserved = tcph[4]
tcph_length = doff_reserved >> 4

print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Sequence Number : ' + str(sequence) + ' Acknowledgement : ' + str(acknowledgement) + ' TCP header length : ' + str(tcph_length)

h_size = eth_length + iph_length + tcph_length * 4
data_size = len(packet) - h_size

#get data from the packet
data = packet[h_size:]

print 'Data : ' + data

#ICMP Packets
elif protocol == 1 :
u = iph_length + eth_length
icmph_length = 4
icmp_header = packet[u:u+4]

#now unpack them :)
icmph = unpack('!BBH' , icmp_header)

icmp_type = icmph[0]
code = icmph[1]
checksum = icmph[2]

print 'Type : ' + str(icmp_type) + ' Code : ' + str(code) + ' Checksum : ' + str(checksum)

h_size = eth_length + iph_length + icmph_length
data_size = len(packet) - h_size

#get data from the packet
data = packet[h_size:]

print 'Data : ' + data

#UDP packets
elif protocol == 17 :
u = iph_length + eth_length
udph_length = 8
udp_header = packet[u:u+8]

#now unpack them :)
udph = unpack('!HHHH' , udp_header)

source_port = udph[0]
dest_port = udph[1]
length = udph[2]
checksum = udph[3]

print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum)

h_size = eth_length + iph_length + udph_length
data_size = len(packet) - h_size

#get data from the packet
data = packet[h_size:]

print 'Data : ' + data

#some other IP packet like IGMP
else :
print 'Protocol other than TCP/UDP/ICMP'

print

if __name__ == "__main__":
main(sys.argv)





Traceback (most recent call last): File "/home/faeze/Desktop/pc.py", line 154, in <module> main(sys.argv) File "/home/faeze/Desktop/pc.py", line 29, in main cap = pcapy.open_live(dev , 65536 , 1 , 0)PcapError: any: You don't have permission to capture on that device (socket: Operation not permitted)>>>



با این روش هم اجرا کردم ولی بهم حق دسترسی نمیده. شما این برنامه رو اجرا می کنید،اوکی هست؟