2019独角兽企业重金招聘Python工程师标准>>>

What is Packet Capturing

Packet capture is a computer networking term for intercepting a data packet that is crossing or moving over a specific computer network.Once a packet is captured, it is stored temporarily so that it can be analyzed. The packet is inspected to help diagnose and solve network problems and determine whether network security policies are being followed.

How can it be used

  • Development Testing & validating & Reverse engineer APP on API

  • Network Administration Seeing what traffic goes on in background,Looking for malicious traffic on networkData capturing is used to identify security flaws and breaches by determining the point of intrusion.

  • Troubleshooting Managed through data capturing, troubleshooting detects the occurrence of undesired events over a network and helps solve them. If the network administrator has full access to a network resource, he can access it remotely and troubleshoot any issues.

  • Security defcon Wall of Sheep.Hackers can also use packet capturing techniques to steal data that is being transmitted over a network, like Stealing credentials.When data is stolen, the network administrator can retrieve the stolen or lost information easily using data capturing techniques.

  • Forensics forensics for crime investigations.Whenever viruses, worms or other intrusions are detected in computers, the network administrator determines the extent of the problem. After initial analysis, she may block some segments and network traffic in order to save historical information and network data.

<!-- more -->

What is libpcap

libpcap flow involving data copy from kernel to user space.

//Compile with: gcc find_device.c -lpcap
#include <stdio.h>
#include <pcap.h>int main(int argc, char \*\*argv) {char \*device;char error_buffer[PCAP_ERRBUF_SIZE];//Find a devicedevice = pcap_lookupdev(error_buffer);if (device == NULL) {printf("Error finding device: %s\n", error_buffer);return 1;}printf("Network device found: %s\n", device);return 0;
}
#include <stdio.h>
#include <time.h>
#include <pcap.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>void print_packet_info(const u_char \*packet, struct pcap_pkthdr packet_header);int main(int argc, char \*argv[]) {char \*device;char error_buffer[PCAP_ERRBUF_SIZE];pcap_t *handle;const u_char *packet;struct pcap_pkthdr packet_header;int packet_count_limit = 1;int timeout_limit = 10000; /*In milliseconds*/device = pcap_lookupdev(error_buffer);if (device == NULL) {printf("Error finding device: %s\n", error_buffer);return 1;}/*Open device for live capture*/handle = pcap_open_live(device,BUFSIZ,packet_count_limit,timeout_limit,error_buffer);/*Attempt to capture one packet. If there is no network trafficand the timeout is reached, it will return NULL*/packet = pcap_next(handle, &packet_header);if (packet == NULL) {printf("No packet found.\n");return 2;}/*Our function to output some info*/print_packet_info(packet, packet_header);return 0;
}void print_packet_info(const u_char \*packet, struct pcap_pkthdr packet_header) {printf("Packet capture length: %d\n", packet_header.caplen);printf("Packet total length %d\n", packet_header.len);
}

Debug Tools

#Older versions of tcpdump truncate packets to 68 or 96 bytes.
#If this is the case, use -s to capture full-sized packets:
$ tcpdump -i <interface> -s 65535 -w <some-file>
# A packet capturing tool similar to TcpDump for Solaris
$ snoop -r -o arp11.snoop -q -d nxge0 -c 150000

tcpdump

tcpdump 是一个运行在命令行下的嗅探工具。它允许用户拦截和显示发送或收到过网络连接到该计算机的TCP/IP和其他数据包。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息,从而使用户能够进一步找出问题的根源。可以使用BPF来限制tcpdump产生的数据包数量。

snoop

snoop uses both the network packet filter and streams buffer modules to provide efficient capture of packets from the network. Captured packets can be displayed as they are received, or saved to a file for later inspection.

promiscuous mode

抓包工具需要工作在promiscuous mode(混杂模式)(superuser), 指一台机器的网卡能够接收所有经过它的数据流,而不论其目的地址是否是它。当网卡工作在混杂模式下时,网卡将来自接口的所有数据都捕获并交给相应的驱动程序。一般在分析网络数据作为网络故障诊断手段时用到,同时这个模式也被网络黑客利用来作为网络数据窃听的入口。

BPF

Berkeley Packet Filter,缩写BPF,是类Unix系统上数据链路层的一种接口,提供原始链路层封包的收发。BPF支持“过滤”封包,这样BPF会只把“感兴趣”的封包到上层软件,可以避免从操作系统内核向用户态复制其他封包,降低抓包的CPU的负担以及所需的缓冲区空间,从而减少丢包率。BPF的过滤功能是以BPF虚拟机机器语言的解释器的形式实现的,这种语言的程序可以抓取封包数据,对封包中的数据采取算术操作,并将结果与常量或封包中的数据或结果中的测试位比较,根据比较的结果决定接受还是拒绝封包。

Go Packet

Find Devices

package mainimport ("fmt""log""github.com/google/gopacket""github.com/google/gopacket/layers""github.com/google/gopacket/pcap"
)func main() {fmt.Println("----------Find all devices---------\n ")devices, err := pcap.FindAllDevs()if err != nil {log.Fatal(err)}// Print device informationfor _, device := range devices {for _, address := range device.Addresses {fmt.Println("- IP address: ", address.IP)fmt.Println("- Subnet mask: ", address.Netmask)}}/*- IP address:  45.33.110.101- Subnet mask:  ffffff00- IP address:  2600:3c01::f03c:91ff:fee5:45b6- Subnet mask:  ffffffffffffffff0000000000000000- IP address:  fe80::f03c:91ff:fee5:45b6- Subnet mask:  ffffffffffffffff0000000000000000- IP address:  127.0.0.1- Subnet mask:  ff000000- IP address:  ::1- Subnet mask:  ffffffffffffffffffffffffffffffff*/

Decoding Packet Layers

Capture Packet Workflow

  • Getting a list of network devices
  • Capturing packets from a network device
  • Analyzing packet layers
  • Using Berkeley Packet Filters
package mainimport ("fmt""log""net""github.com/google/gopacket""github.com/google/gopacket/layers""github.com/google/gopacket/pcap"
)func main(){handle, err := pcap.OpenLive("eth0", 65536, true, pcap.BlockForever)if err != nil {fmt.Printf("Error: %s\n", err)return}defer handle.Close()//Create a new PacketDataSourcesrc := gopacket.NewPacketSource(handle, layers.LayerTypeEthernet)//Packets returns a channel of packetsin := src.Packets()for {var packet gopacket.Packetselect {//case <-stop://returncase packet = <-in:arpLayer := packet.Layer(layers.LayerTypeARP)if arpLayer == nil {continue}arp := arpLayer.(*layers.ARP)if net.HardwareAddr(arp.SourceHwAddress).String() == "abc" {//Do something or don't}tcpLayer := packet.Layer(layers.LayerTypeTCP)if tcpLayer == nil {continue}tcp := tcpLayer.(*layers.TCP)//.......}}
}

Creating and Sending Packets

package mainimport ("github.com/google/gopacket""github.com/google/gopacket/layers""github.com/google/gopacket/pcap""log""net""time"
)var (device       string = "eth0"snapshot_len int32  = 1024promiscuous  bool   = falseerr          errortimeout      time.Duration = 30 * time.Secondhandle       *pcap.Handlebuffer       gopacket.SerializeBufferoptions      gopacket.SerializeOptions
)func main() {// Open devicehandle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)if err != nil {log.Fatal(err) }defer handle.Close()// Send raw bytes over wirerawBytes := []byte{10, 20, 30}err = handle.WritePacketData(rawBytes)if err != nil {log.Fatal(err)}// Create a properly formed packet, just with// empty details. Should fill out MAC addresses,// IP addresses, etc.buffer = gopacket.NewSerializeBuffer()gopacket.SerializeLayers(buffer, options,&layers.Ethernet{},&layers.IPv4{},&layers.TCP{},gopacket.Payload(rawBytes),)outgoingPacket := buffer.Bytes()// Send our packeterr = handle.WritePacketData(outgoingPacket)if err != nil {log.Fatal(err)}// This time lets fill out some informationipLayer := &layers.IPv4{SrcIP: net.IP{127, 0, 0, 1},DstIP: net.IP{8, 8, 8, 8},}ethernetLayer := &layers.Ethernet{SrcMAC: net.HardwareAddr{0xFF, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA},DstMAC: net.HardwareAddr{0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD},}tcpLayer := &layers.TCP{SrcPort: layers.TCPPort(4321),DstPort: layers.TCPPort(80),}// And create the packet with the layersbuffer = gopacket.NewSerializeBuffer()gopacket.SerializeLayers(buffer, options,ethernetLayer,ipLayer,tcpLayer,gopacket.Payload(rawBytes),)outgoingPacket = buffer.Bytes()
}

Application

  • qisniff
  • 新一代Ntopng网络流量监控—可视化和架构分析
  • 基于网络抓包实现kubernetes中微服务的应用级监控

转载于:https://my.oschina.net/zijingshanke/blog/1031479

基于Go Packet实现网络数据包的捕获与分析相关推荐

  1. 基于winpcap的网络数据包的捕获与分析

    这是我的毕业论文,战事不能够发上来!!很快就好!! 转载于:https://blog.51cto.com/33965/29268

  2. 利用tshark对网络数据包做进一步的分析

    在性能测试或者对大量网络数据包做数据分析的时候,靠人工方法显然行不通,研究调用wireshark 的API对数据包进行分析,太难.放弃了这条路.发现tshark可以直接过滤数据包导出你需要的heade ...

  3. 计算机网络ip数据包分析题,计算机网络课程设计_IP数据包的捕获及分析.doc

    CENTRAL SOUTH UNIVERSITY 计算机网络课程设计报告 目录 第一章 课程设计的目的与要求1 1.1 课程设计的目的1 1.2 课程设计的要求1 第二章 课程设计的内容3 2.1 课 ...

  4. 【php毕业设计】基于php+mysql+apache的网络数据包分析工具设计与实现(毕业论文+程序源码)——网络数据包分析工具

    基于php+mysql+apache的网络数据包分析工具设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于php+mysql+apache的网络数据包分析工具设计与实现,文章末尾附有本毕业设 ...

  5. 网络数据包捕获函数库Libpcap安装与使用(非常强大)

    1.Libpcap简介 Libpcap是Packet Capture Libray的英文缩写,即数据包捕获函数库.该库提供的C函数接口用于捕捉经过指定网络接口的数据包,该接口应该是被设为混杂模式.这个 ...

  6. 网络分析系列之一 网络数据包分析基础知识

    在高速发达的计算机网络世界,网络和系统运维者每天都可能面对成千上万的故障问题,从简单的终端病毒感染,到复杂的网络配置,甚至更为复杂的应用架构.当问题出现,我们永远也不可能立即解决所有的,而良好的知识储 ...

  7. 基于Python3+Scapy的数据包流量特征批量分析工具

    基于Python3+Scapy的网络数据包批量分析工具 项目源码 适用范围以及使用说明 背景 环境准备及运行说明 常见协议分析识别 TCP协议识别 UDP协议识别 输出TXT文档信息 SSL NAME ...

  8. 一个最简单的通过WireShark破解SSL加密网络数据包的方法

    原文地址: http://article.yeeyan.org/view/530101/444688 一般来说,我们用WireShark来抓取包进行分析是没有多大问题的.但这里有个问题是,如果你碰到的 ...

  9. 【转载】网络数据包分析 网卡Offload

    对于网络安全来说,网络传输数据包的捕获和分析是个基础工作,绿盟科技研究员在日常工作中,经常会捕获到一些大小远大于MTU值的数据包,经过分析这些大包的特性,发现和网卡的offload特性有关,本文对网卡 ...

  10. 网络数据包分析 网卡Offload

    对于网络安全来说,网络传输数据包的捕获和分析是个基础工作,科技研究员在日常工作中,经常会捕获到一些大小远大于MTU值的数据包,经过分析这些大包的特性,发现和网卡的offload特性有关,本文对网卡Of ...

最新文章

  1. Protege5.0.0入门学习
  2. Python3 除法取整取余,上下取整
  3. 最长不重复子串python_python经典算法题:无重复字符的最长子串
  4. Maven学习总结(27)——Maven自定义打包插件maven-assembly-plugin详解
  5. 绑定事件和解绑事件的方法
  6. 常用的渗透测试辅助工具
  7. 桌面高效便捷的多窗口调整管理工具 - AquaSnap
  8. java怎么引入矢量图标库,阿里巴巴矢量图标库Iconfont的使用方法
  9. UART、RS232、RS485协议简单总结
  10. 如何优化你的ERP库存管理系统
  11. 微信小程序-“授权失败”场景的优雅处理
  12. 俄亥俄州立大学宣布开放 Swift 编程和 App 开发课程
  13. C语言编程入门训练(一)
  14. 计算机网络线接法,网线水晶头接法图解 一分钟学会网线怎么接
  15. 双硬盘双系统安装win10和centos7
  16. IDE也卷了,微软杀入嵌入式IDE
  17. 网络安全-江湖高手专用的“隐身术”:图片隐写技术
  18. 建筑力学与结构【10】
  19. Ps素描效果引用说明
  20. 一次精彩的皮卡车降噪试验过程

热门文章

  1. 【2015.8.26】新的开始与纪念web开发
  2. 加快 DHTML 的一组技巧
  3. 两个年月下拉列表html,html年月日下拉联动菜单 年月日三下拉框联动
  4. 内核中断,异常,抢占总结篇
  5. Linux的软链接和硬链接
  6. Linux下PS命令详解 (转)
  7. Linux进程地址空间布局
  8. 使用jrtplib(RTP)传输H.264视频文件
  9. 如何解读vmlinux.lds.S文件
  10. 页面回收之shrink_zone的实现