linux raw限制端口訪出,使用Linux raw socket時需要注意的一些問題

本文的copyleft歸gfree.wind@gmail.com所有,使用GPL發布,可以自由拷貝,轉載。但轉載請保持文檔的完整性,注明原作者及原鏈接,嚴禁用于任何商業用途。

作者:gfree.wind@gmail.com

博客:linuxfocus.blog.chinaunix.net

一般情況下,我們使用raw socket都是用于發送icmp包或者自己定義的包。最近我想用raw socket

自己去創建TCP的包,并完成3次握手。

在這個過程中,發現了幾個問題:

1. 發現收不到對端返回的ACK,但是通過tcpdump卻可以看到。這個通過修改創建socket的API參數得以糾正。

原來創建socket使用如下參數s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)。因為linux的raw socket不會把

不會把UDP和TCP的分組傳遞給任何原始套接口。——見《UNIX網絡編程》28.3.

所以為了讀到ACK包,我們創建的raw socket需要建立在數據鏈路層上。

s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP))

這樣該socket就可以讀取到所有的數據包。當然這樣的數據包無疑是非常龐大的,那么我們可以

使用bind和connect來過濾數據包。bind可以指定本地的IP和端口,connect可以指定對端的IP和端口。

2. 在修改完創建socket的API,我發現還有一個問題,到現在依然沒有找到合適的解決方法。

通過鏈路層的raw socket,我們可以收到對端的ACK。但是因為linux內核也會處理TCP的握手過程,

所以,當它收到ACK的時候,會認為這是一個非法包,會直接發送一個RST給對端。這樣我們拿到了這個ACK,

也沒有實際的用處了——因為這個連接已經被RST了。

我想,內核之所以會直接發送RST,也許是因為我們創建的是raw socket,但是發送的確是TCP包,

當對端返回ACK時,內核根本不認為我們已經打開了這個TCP端口,所以會直接RST掉這個連接。

那么問題就在于,我們如何告訴內核,我們打開了TCP的這個端口。

我想過一個方法,除了一個raw socket,再創建一個真正的TCP socket。但是細想起來,這條路應該行不通。

在網上搜了半天,總算找到一個比較詳細的解釋了。原因給我猜想的相差不遠,當我們使用raw

socket來發送sync包時,內核的TCP協議棧并不知道你發送了sync包,所以當對端返回SYNC/ACK時,內核首先要處理這個包,發現它是一

個SYNC/ACK,然而協議棧卻不知道前面的sync,所以就認為這個包是非法的,于是就會發送RST來中止連接。

原文如下:

This is one of the most frequently asked question by someone who is

experimenting with raw sockets and TCP/IP. It is known that the

'IP_HDRINCL' socket option allows you to include the IP header along

with the data. Since TCP encapsulates the IP header, we can also build

a TCP packet and send it over a network. But the problem is, a TCP

connection can never be established this way. The scenario is as

follows:

A TCP connection is always made by a three-way handshake.

So, initially you send a 'SYN' packet to the remote machine. If it is

actively listening on the port, you get a 'SYN/ACK' packet. So far so

good. But before you can respond, your machine sends an 'ACK/RST'

packet and connection attempt is ended. For the connection to be

complete, instead of the 'RST' packet, your machine should be sending

an 'ACK' to the remote machine.

The difference lies where the

connection is exactly made. Although the programs are communicating

after the connection is complete, the TCP connection is never between

two programs but rather between the TCP stacks of the two machines.

Here 'stack' means a layer of programs that communicates between each

other. TCP stack stands for the protocol driver or the actual network

transport protocol. Now lets look at exactly what happens when you send

a 'SYN' packet...

Since you are using raw sockets ('SOCK_RAW') and

not TCP/Stream sockets ('SOCK_STREAM') the TCP stack has no information

about what you are doing at program level. And since the 'IP_HDRINCL'

allows you to build any type of IP packet and send it along with the

data, you can build a 'SYN' packet and send it to the TCP server

program which is actively listening. But the point is that the 'SYN'

packet is being sent from your program and not the stack. In other

words the TCP stack of your machine has no idea how of sending the

'SYN' packet.

On the other side the 'SYN' packet is received by the

stack at the remote machine and not exactly by the program. As with the

case of the arrival of any 'SYN' packet, the stack at the remote

machine responds with a 'SYN/ACK' packet. This packet is now received

by the TCP stack of your machine. In other words, the incoming TCP

packet ('SYN/ACK') will be processed by the stack. Since it has no

information of the previous sent 'SYN' packet, it responds with a 'RST'

packet, as in the case of any improper or unacceptable packet for a

connection.

So the difference between sending and receiving a TCP

packet using raw sockets is, the former is not processed while the

latter is processed by the TCP stack of your machine.

該解釋來自于,

感謝Mr Andreas Masur 和Mr Mathew Joy. Thanks Mr Andreas Masur and Mr Mathew Joy.

作者:gfree.wind@gmail.com

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275519.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275519.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275519.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

讀完 Vue 發布源碼,小姐姐回答了 leader 的提問,并優化了項目發布流程~

大家好,我是若川。這是 源碼共讀 第三期活動,紀年小姐姐的第三次投稿。紀年小姐姐學習完優化了自己的項目發布流程,而且回答了leader對她的提問,來看看她的思考和實踐。第三期是 Vue 3.2 發布了,那尤雨溪是怎么發布 Vu…

小程序背景圖片的坑

本人是前端菜鳥一個,比小白還要白,這完全是自己的經驗總結,并不是要給各位分享什么寶貴經驗哈,各位大佬不喜勿噴,不然會打擊到我的哈哈因為公司要求做幾個小程序的頁面,我不得不拾起丟棄了幾個月的小程序開…

SimpleAdapter類使用方法

SimpleAdapter的構造函數是&#xff1a; public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) 官方說明了其各個參數含義&#xff0c;我這里根據自己的理解解釋下&#xff1a; 第一個context&…

小程序 富文本自適應屏幕_自適應文本:跨屏幕尺寸構建可讀文本

小程序 富文本自適應屏幕Many of you may already know about responsive web design. Cited from Wikipedia, responsive web design (RWD) is an approach to web design that makes web pages render well on a variety of devices and windows or screen sizes. The respon…

Vue、React 之間如何實現代碼移植?

大家好&#xff0c;我是若川。面對前端最火的兩個框架&#xff0c;學 React 還是 Vue &#xff1f;這可能是每個前端人都曾糾結過的問題。不過&#xff0c;現在你不用糾結了——因為很多公司都是兩個框架都有大量的應用&#xff0c;取決于不同團隊的技術選型&#xff0c;特別是…

linux mariadb 亂碼,配置mariadb遠程訪問權限,解決數據庫亂碼問題

配置mariadb遠程訪問權限&#xff1a;1)登錄數據庫:# mysql -uroot -p2)配置授權數據庫用戶遠程訪問權限&#xff0c;%表示所有遠程IP&#xff0c;也可以指定IP。WITH GRANT OPTION表示mysql數據庫的grant表中重新加載權限數據&#xff1a;GRANT ALL PRIVILEGES ON *.* TO 用戶…

平面設計師和ui設計師_游戲設計師的平面設計

平面設計師和ui設計師Design is a very ancient practice, but graphic design really found its core principles post World War One. Games are also very ancient but video games are still finding their feet. I think graphic design has a few things to teach people…

從零開發一個命令行腳手架工具 等

大家好&#xff0c;我是若川。今天周末&#xff0c;話不多說&#xff0c;這一次花了幾小時精心為大家挑選了20余篇好文&#xff0c;供大家閱讀學習。本文閱讀技巧&#xff0c;先粗看標題&#xff0c;感興趣可以都關注一波&#xff0c;絕對不虧。前端宇宙小編就職于某大廠&#…

linux的HAL庫函數,STM32 HAL庫 IIC 協議庫函數

/* 第1個參數為I2C操作句柄第2個參數為從機設備地址第3個參數為從機寄存器地址第4個參數為從機寄存器地址長度第5個參數為發送的數據的起始地址第6個參數為傳輸數據的大小第7個參數為操作超時時間 */HAL_I2C_Mem_Write(&hi2c2,salve_add,0,0,PA_BUFF,sizeof(PA_BUFF),0x10)…

pku acm 2140 Herd Sums http://acm.pku.edu.cn/JudgeOnline/problem?id=2140

2140代碼短小精悍&#xff1a;#include<stdio.h> int main() { int cnt0,i; long s; scanf("%ld",&s); for(i1;(i1)*i/2<s;i)if((s-(i-1)*i/2)%i0)cnt; printf("%d\n",cnt); return 0; }轉載于:https://www.cnblogs.com/Chinese-Coder-C…

java合成海報的工具類

2019獨角獸企業重金招聘Python工程師標準>>> package io.renren.common.utils;import cn.hutool.core.lang.Console; import io.renren.modules.oss.cloud.OSSFactory;import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.a…

a說b說謊b說c說謊說d說_說謊的眼睛及其同伙

a說b說謊b說c說謊說d說The eye is a complex and temperamental organ. By the end of this article, designers will have a better understanding of how the eye works with the brain, how it deconstructs images that the brain stitches back up again, and how the two…

一名運營,自學一年前端,成功入職杭州某獨角獸企業,他的面試經驗和學習方法等分享...

大家好&#xff0c;我是若川。這是我的微信群里小伙伴年年 的投稿。他是19年畢業&#xff0c;之前做的是運營相關的工作&#xff0c;在我的交流群里非常活躍&#xff0c;自學一年前端&#xff0c;目前成功轉行入職杭州一家獨角獸企業。相信他的文章能帶給大家一些啟發和激勵。0…

linux下svn relocate,如何進行svn?relocate?操作

1。進入工作復本&#xff03;> cd ~/test2。查看倉庫地址(URL)&#xff03;> svn info路徑&#xff1a;.地址(URL)&#xff1a;http://192.168.28.1/repos/test檔案庫 UUID&#xff1a;a81f9bed-3506-0410-b369-e50476f75162修訂版&#xff1a;44節點種類&#xff1a;目錄…

教你怎么買虛擬空間(轉)

虛擬空間是什么?經常聽到站長們在群里問&#xff0c;哪里的虛擬空間好?哪里的虛擬空間性能好?哪里的虛擬空間便宜?虛擬空間是當今IDC行業的一個重要銷售產品&#xff0c;虛擬空間也是中國站長們建設網站中最常應用的網站載體。各種數據說明&#xff0c;虛擬空間的好壞能影響…

React筆記-事件分發

事件分發 之前講述了事件如何綁定在document上&#xff0c;那么具體事件觸發的時候是如何分發到具體的監聽者呢&#xff1f;我們接著上次注冊的事件代理看。當我點擊update counter按鈕時&#xff0c;觸發注冊的click事件代理。 function dispatchInteractiveEvent(topLevelTyp…

百度指數可視化_可視化指數

百度指數可視化Abstract:– Analysis of the visual representations of exponentials.– Proposals to solve current visualization issues.– Call to discussion to come up with a better visual representation convention.抽象&#xff1a; –分析指數的視覺表示形式。…

qemu+linux+x86+64,kvm 內部錯誤:無法找到適合 x86_64 的模擬器

本文將為您描述kvm 內部錯誤&#xff1a;無法找到適合 x86_64 的模擬器,教程操作方法:0x00 問題安裝完 KVM 之后&#xff0c;啟動管理工具報錯&#xff1a;內部錯誤&#xff1a;無法找到適合 x86_64 的模擬器于是查看 libvirtd 服務狀態&#xff0c;查看到以下內容&#xff1a;…

阿里云謙大佬:時間精力有限的情況下如何高效學習前端?

大家好&#xff0c;我是若川。最近組織了源碼共讀活動1個月&#xff0c;200人&#xff0c;一起讀了4周源碼&#xff0c;歡迎加我微信 ruochuan12 進群參與。今天分享一篇阿里云謙大佬的文章。昨天在群里也有小伙伴說到&#xff1a;大佬們是需要什么學什么&#xff0c;新手一般是…

JQuery小記

訪問dom元素 $代表整個dom tree $("#content") $("p") $("li .red") 字符串轉換為json對象 $.parseJSON ajax $.ajax({type: "post",url: "GetUser.ashx",success: function (data) {var t "";var json $.pars…