有關C#開發抓取、分析網路資料包的程式碼段
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[StructLayout(LayoutKind.Explicit)]
public struct IPHeader
{
[FieldOffset(0)] public byte ip_verlen;
[FieldOffset(1)] public byte ip_tos;
[FieldOffset(2)] public ushort ip_totallength;
[FieldOffset(4)] public ushort ip_id;
[FieldOffset(6)] public ushort ip_offset;
[FieldOffset(8)] public byte ip_ttl;
[FieldOffset(9)] public byte ip_protocol;
[FieldOffset(10)] public ushort ip_checksum;
[FieldOffset(12)] public uint ip_srcaddr;
[FieldOffset(16)] public uint ip_destaddr;
}
public class RawSocket
{
private bool error_occurred;//是否產生錯誤
public bool KeepRunning;//是否繼續進行
private static int len_receive_buf;//得到的資料流的長度
byte [] receive_buf_bytes;//收到的位元組
private Socket socket = null; //宣告套接字
const int SIO_R = unchecked((int)0x98000001);
const int SIO_1=unchecked((int)0x98000002);
const int SIO_2=unchecked((int)0x98000003);
public RawSocket()//建構函式
{
error_occurred=false;
len_receive_buf = 4096;
receive_buf_bytes = new byte[len_receive_buf];
}
public void CreateAndBindSocket(string IP)//建立並繫結套接字
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
socket.Blocking = false;//置socket非阻塞狀態
socket.Bind(new IPEndPoint(IPAddress.Parse(IP), 0));
if (SetSocketOption()==false) error_occurred=true;
}
public void Shutdown()
{
if(socket != null)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
private bool SetSocketOption()
{
bool ret_value = true;
try
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
byte []IN = new byte[4]{1, 0, 0, 0};
byte []OUT = new byte[4];
int ret_code = socket.IOControl(SIO_R, IN, OUT);//低級別操作模式
ret_code = OUT[0] + OUT[1] + OUT[2] + OUT[3];//把4個8位位元組合成一個32位整數
System.Windows.Forms.MessageBox.Show(ret_code.ToString());
if(ret_code != 0) ret_value = false;
}
catch(SocketException)
{
ret_value = false;
}
return ret_value;
}
public bool ErrorOccurred
{
get
{
return error_occurred;
}
}
//解析接收的資料包,形成PacketArrivedEventArgs時間資料類物件,並引發PacketArrival事件
unsafe private void Receive(byte [] buf, int len)
{
byte temp_protocol=0;
uint temp_version=0;
uint temp_ip_srcaddr=0;
uint temp_ip_destaddr=0;
short temp_srcport=0;
short temp_dstport=0;
IPAddress temp_ip;
//return;
PacketArrivedEventArgs e=new PacketArrivedEventArgs();
fixed(byte *fixed_buf = buf)
{
IPHeader * head = (IPHeader *) fixed_buf;
e.HeaderLength=(uint)(head->ip_verlen & 0x0F) << 2;
temp_protocol = head->ip_protocol;
switch(temp_protocol)
{
case 1: e.Protocol="ICMP:"; break;
case 2: e.Protocol="IGMP:"; break;
case 6: e.Protocol="TCP:"; break;
case 17: e.Protocol="UDP:"; break;
default: e.Protocol= "UNKNOWN"; break;
}
temp_version =(uint)(head->ip_verlen & 0xF0) >> 4;
e.IPVersion = temp_version.ToString();
temp_ip_srcaddr = head->ip_srcaddr;
temp_ip_destaddr = head->ip_destaddr;
temp_ip = new IPAddress(temp_ip_srcaddr);
e.OriginationAddress =temp_ip.ToString();
temp_ip = new IPAddress(temp_ip_destaddr);
e.DestinationAddress = temp_ip.ToString();
temp_srcport = *(short *)&fixed_buf[e.HeaderLength];
temp_dstport = *(short *)&fixed_buf[e.HeaderLength+2];
e.OriginationPort=IPAddress.NetworkToHostOrder(temp_srcport).ToString();
e.DestinationPort=IPAddress.NetworkToHostOrder(temp_dstport).ToString();
// if(e.DestinationAddress!="211.87.212.116"||int.Parse(e.DestinationPort)>1000)
// {
// return;
// }
e.PacketLength =(uint)len;
e.MessageLength =(uint)len - e.HeaderLength;
e.ReceiveBuffer=buf;
//把buf中的IP頭賦給PacketArrivedEventArgs中的IPHeaderBuffer
Array.Copy(buf,0,e.IPHeaderBuffer,0,(int)e.HeaderLength);
//把buf中的包中內容賦給PacketArrivedEventArgs中的MessageBuffer
Array.Copy(buf,(int)e.HeaderLength,e.MessageBuffer,0,(int)e.MessageLength);
}
//引發PacketArrival事件
OnPacketArrival(e);
}
public void Run()
{
IAsyncResult ar = socket.BeginReceive(receive_buf_bytes, 0, len_receive_buf, SocketFlags.None, new AsyncCallback(CallReceive), this);
}
private void CallReceive(IAsyncResult ar)
{
int received_bytes;
received_bytes = socket.EndReceive(ar);
Receive(receive_buf_bytes, received_bytes);
if (KeepRunning) Run();
}
public class PacketArrivedEventArgs : EventArgs
{
public PacketArrivedEventArgs()
{
this.protocol = "";
this.destination_port = "";
this.origination_port = "";
this.destination_address = "";
this.origination_address = "";
this.ip_version = "";
this.total_packet_length =0;
this.message_length =0;
this.header_length =0;
this.receive_buf_bytes=new byte[len_receive_buf];
this.ip_header_bytes=new byte[len_receive_buf];
this.message_bytes=new byte[len_receive_buf];
}
public string Protocol
{
get {return protocol;}
set {protocol=value;}
}
public string DestinationPort
{
get {return destination_port;}
set {destination_port=value;}
}
public string OriginationPort
{
get {return origination_port;}
set {origination_port=value;}
}
public string DestinationAddress
{
get {return destination_address;}
set {destination_address=value;}
}
public string OriginationAddress
{
get {return origination_address;}
set {origination_address=value;}
}
public string IPVersion
{
get {return ip_version;}
set {ip_version=value;}
}
public uint PacketLength
{
get {return total_packet_length;}
set {total_packet_length=value;}
}
public uint MessageLength
{
get {return message_length;}
set {message_length=value;}
}
public uint HeaderLength
{
get {return header_length;}
set {header_length=value;}
}
public byte [] ReceiveBuffer
{
get {return receive_buf_bytes;}
set {receive_buf_bytes=value;}
}
public byte [] IPHeaderBuffer
{
get {return ip_header_bytes;}
set {ip_header_bytes=value;}
}
public byte [] MessageBuffer
{
get {return message_bytes;}
set {message_bytes=value;}
}
private string protocol;
private string destination_port;
private string origination_port;
private string destination_address;
private string origination_address;
private string ip_version;
private uint total_packet_length;
private uint message_length;
private uint header_length;
private byte []receive_buf_bytes = null;
private byte []ip_header_bytes = null;
private byte []message_bytes = null;
}
public delegate void PacketArrivedEventHandler(
Object sender, PacketArrivedEventArgs args);
public event PacketArrivedEventHandler PacketArrival;
protected virtual void OnPacketArrival(PacketArrivedEventArgs e)
{
if (PacketArrival != null)
{
PacketArrival(this, e);
}
}
}
}
相關推薦
有關C#開發抓取、分析網路資料包的程式碼段
using System; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Wi
WireShark學習之抓取和分析HTTP資料包
1. 設定過濾條件 - 指定網路協議http 2. 開啟Chrome瀏覽器輸入網址 - 在瀏覽器輸入https://sspai.com/post/30292 3. 在抓獲得包中得到兩個資料包,分別是HTTP請求以及HTTP響應
使用tcpdump+wireshark抓包分析網路資料包
最近和學弟在除錯一個GPRS通訊模組,需求是通過GPRS模組通過http協議傳送資料到伺服器,但是http協議一直失敗,伺服器返回400,通過查詢http狀態碼得知,http400錯誤是請求無效,因為GPRS模組沒有實現http協議的封裝,需要在TCP協議的基礎上,手動拼裝http格式的報文.所以初步猜測是h
網路爬蟲中Fiddler抓取PC端網頁資料包與手機端APP資料包
1 引言 在編寫網路爬蟲時,第一步(也是極為關鍵一步)就是對網路的請求(request)和回覆(response)進行分析,尋找其中的規律,然後才能通過網路爬蟲進行模擬。瀏覽器大多也自帶有除錯工具可以進行抓包分析,但是瀏覽器自帶的工具比較輕量,複雜的抓包並不支援。且有時候需要編寫手機APP爬
Fiddler抓取手機APP程式資料包--過濾多餘的Sessions
1.下載並安裝Fiddler 下載地址:https://www.telerik.com/download/fiddler 2.設定Fiddler可監聽遠端通訊 前提條件:需要監聽的手機和Fiddler所在的電腦必須在同一個區域網下。 ①點選Tools ——>
Fiddler抓取Android手機的資料包
1. 電腦使用無線網連結網路,配置 Fiddler 允許監聽 https; 2. 配置 Fiddler 允許遠端連線,點選選單中點選 connections,選中 allow remote computers to connect,預設監 聽埠為 8888
使用BurpSuite抓取HTTPS網站的資料包
昨天面試,技術官問到了我如何使用BurpSuite抓取https網站的資料包,一時間沒能回答上來(尷尬!)。因為以前https網站的資料包我都是用Fiddler抓取的,Fiddlert自動幫我們配置好了證書,所以就沒用BurpSuite抓取過,今天特意去學習了下如何使用BurpSuite抓取http
Ubuntu(VMware)下用wireshark抓取802.11無線資料包 操作詳解
咱們從零開始:準備環境:VMware12.0 + Ubuntu16.04 (越新越好)辛酸之路:筆記本插上無線usb網絡卡,連線到Ubuntu,iwconfig識別不到網絡卡。下個官方驅動吧,這個Ralink很難找,放到Ubuntu編譯失敗。 然而同學卻能直接識別出網絡卡,沒
python&php數據抓取、爬蟲分析與中介,有網址案例
網絡 數據抓取 不定 pytho span article 抓取 取數據 data- 近期在做一個網絡爬蟲程序。後臺使用python不定時去抓取數據。前臺使用php進行展示 站點是:http://se.dianfenxiang.com python&a
Python網路爬蟲實戰:抓取和分析天貓胸罩銷售資料
本文實現一個非常有趣的專案,這個專案是關於胸罩銷售資料分析的。Google曾給出了一幅世界女性胸部尺寸分佈地圖 ,從地圖中可以明顯看出中國大部分地區呈現綠色(表示平均胸部尺寸為A罩杯),少部分地區呈現藍色(表示平均胸部尺寸為B罩杯) 現在李寧老師也來驗證一下這個
通過tcpdump抓取 指定 ip 埠 的網路資料,並通過wireshark分析網路資料,很實用
抓取來源ip port 埠的資料,tcp協議,並儲存到檔案 tcpdump -w dataSrc.pcap -i bond0 src net ip and port port 抓取目的ip port
android 開發--抓取網頁解析網頁內容的若干方法(網路爬蟲)(正則表示式)
網頁有兩種格式,一種是xml另一種是html,目前似乎好像大部分都是html格式的,檢視網頁格式的方法是在瀏覽器中右鍵-->檢視原始碼 一,XML解析的三大方法 (1) SAX: Simple API for XML SAX是一個解析速度快並且佔用記憶體少的XML解析
用C++抓取網路資料包--嗅探器的設計原理
嗅探器作為一種網路通訊程式,也是通過對網絡卡的程式設計來實現網路通訊的,對網絡卡的程式設計也是使用通常的套接字(socket)方式來進行。但是,通常的套接字程式只能響應與自己硬體地址相匹配的或是以廣播形式發出的資料幀,對於其他形式的資料幀比如已到達網路介面但卻不是發給此地址
用C++實現網路程式設計---抓取網路資料包的實現方法
做過網管或協議分析的人一般都熟悉sniffer這個工具,它可以捕捉流經本地網絡卡的所有資料包。抓取網路資料包進行分析有很多用處,如分析網路是否有網路病毒等異常資料,通訊協議的分析(資料鏈路層協議、IP、UDP、TCP、甚至各種應用層協議),敏感資料的捕捉等。下面我們就來看
grep -A -B -C 顯示抓取的前後幾行參數
參數 tween line 字串 抓取 his 站點 顯示 ces 我經常用grep找東西,比如用戶名和密碼。大部分站點和用戶名和密碼都是在一樣的,方便grep查找。有時,為了文本好看,我會放在多行。比如 wikipedia多個語言版本上有多個賬號,就放在wikipedia
c#實現抓取高清美女妹紙圖片
bds bottom share plugin color webclient client quest eve c#實現抓取高清美女妹紙圖片 代碼如下: private void DoFetch(int pageNum) { ThreadPool.QueueU
wget整站抓取、網站抓取功能
.net 工作 www. .html ack 保存 tps log tac wget -r -p -np -k -E http://www.xxx.com 抓取整站 wget -l 1 -p -np -k http://www.xxx.com 抓取第一級
爬蟲-day02-抓取和分析
https baidu gzip ace .text python htm conn code ###頁面抓取### 1、urllib3 是一個功能強大且好用的HTTP客戶端,彌補了Python標準庫中的不足 安裝: pip install urllib3
C# webrequest 抓取數據時,多個域Cookie的問題
save copyto ews IT -c 如何 date ken 開發者工具 最近研究了下如何抓取為知筆記的內容,在抓取筆記裏的圖片內容時,老是提示403錯誤,用Chorme的開發者工具看了下: 這裏的Cookie來自兩個域,估計為知那邊是驗證了token(登錄後才
Android網路資料包的抓和分析
1. 手機獲得root許可權; 2. push tcpdump到 /; 3. chmod 755 tcpdump; 4. 抓包: adb shell tcpdump -s 0 -w /sdcard/capture.pcap 5. pull資料包到pc,使用wireshark