溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

無法回應(yīng)的ARP請求包導(dǎo)致的網(wǎng)站緩慢問題排錯

發(fā)布時間:2020-07-29 14:12:26 來源:網(wǎng)絡(luò) 閱讀:5573 作者:yoke88 欄目:網(wǎng)絡(luò)安全

問題

訪問一個網(wǎng)站,從本地訪問很快,但是從客戶端訪問大概要等待3秒的樣子。在服務(wù)器放上靜態(tài)網(wǎng)頁,在客戶端訪問則返回時間很快。

排錯步驟

  • 在客戶端訪問問題網(wǎng)站,在客戶端用wireshark抓包

    1. 用tcp 三次握手及客戶端請求與服務(wù)器返回的ACK來判斷是否存在線路或者服務(wù)器忙問題,發(fā)現(xiàn)不是。348-349 顯示出服務(wù)器響應(yīng)很快。 349-498 之間用了3.28秒,說明這是服務(wù)器應(yīng)用的問題。

    無法回應(yīng)的ARP請求包導(dǎo)致的網(wǎng)站緩慢問題排錯

  • 讓開發(fā)人員調(diào)查服務(wù)器端的應(yīng)用,開發(fā)人員說之前有個小功能可以抓取客戶端MAC地址,但是看到抓的包,應(yīng)該不是用的客戶端的代碼,因為第一個web頁響應(yīng)就3秒多,要是客戶端代碼那也是后續(xù)的JS或者資源加載較慢。

  • 不管三七二十一,在服務(wù)器端也抓了下包。過濾下arp 和http的包看看,過濾后發(fā)現(xiàn)有三個ARP請求,但是沒有對應(yīng)回應(yīng)。另外仔細(xì)看ARP請求的具體內(nèi)容也不對,服務(wù)器用ARP請求去解析客戶端的MAC地址應(yīng)該是不對的,原因是服務(wù)器和客戶端不在一個網(wǎng)段,正常的跨網(wǎng)段的ARP請求是同一個網(wǎng)段才會用的,如果跨網(wǎng)段那應(yīng)該去解析路由器的MAC地址。所以這些ARP請求有問題。

無法回應(yīng)的ARP請求包導(dǎo)致的網(wǎng)站緩慢問題排錯

  • 開發(fā)人員注釋掉了客戶端ARP地址查詢的代碼。訪問速度瞬間提升了。
  • 開發(fā)人員同時注意到客戶端ARP地址查詢的結(jié)果為00-00-00-00-00-00,和我們的服務(wù)器上的抓包結(jié)果一致,因為去請求一個跨網(wǎng)段IP地址的MAC,所以目標(biāo)地址不會收到,因為ARP廣播會在路由器端終止。

揭開真相

  • 開發(fā)人員給了我服務(wù)器端的代碼C#
    ``` c#
    [DllImport("Iphlpapi.dll")]
    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport("Ws2_32.dll")]
    private static extern Int32 inet_addr(string ip);

    public string getClientMac(string userip)
    {
        if (string.IsNullOrEmpty(userip)) return null;
        //string userip = Request.UserHostAddress;
        string strClientIP = userip.ToString().Trim();
        Int32 ldest = inet_addr(strClientIP);
        Int32 lhost = inet_addr("");
        Int64 macinfo = new Int64();
        Int32 len = 6;
        int res = SendARP(ldest, 0, ref macinfo, ref len);
        string mac_src = macinfo.ToString("X");
        //if (mac_src == "0")
        //{
        //    ip = userip;
        //}
    
        while (mac_src.Length < 12)
        {
            mac_src = mac_src.Insert(0, "0");
        }
    
        string mac_dest = "";
    
        for (int i = 0; i < 11; i++)
        {
            if (0 == (i % 2))
            {
                if (i == 10)
                {
                    mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                }
                else
                {
                    mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                }
            }
        }
    
        return mac_dest;
    }

* 按照代碼邏輯的話,服務(wù)器應(yīng)該是用了一次SendARP 調(diào)用,但是為什么會有三個ARP請求產(chǎn)生,而且不同的ARP請求包之間的等待時間不一。所以為了驗證這個SendARP的調(diào)用的實際操作,我用powershell 寫了上面一個sendARP 調(diào)用,然后用wireshark抓包。
``` powershell
Function Send-Arp { 
param( 
    [string]$DstIpAddress, 
    [string]$SrcIpAddress = 0 
) 

$signature = @" 
[DllImport("iphlpapi.dll", ExactSpelling=true)] 
   public static extern int SendARP(  
       uint DestIP, uint SrcIP, byte[] pMacAddr, ref int PhyAddrLen); 
"@ 

    Add-Type -MemberDefinition $signature -Name Utils -Namespace Network 

    try { 
        $DstIp = [System.Net.IPAddress]::Parse($DstIpAddress) 
        $DstIp = [System.BitConverter]::ToInt32($DstIp.GetAddressBytes(), 0) 
    } catch { 
        Write-Error "Could not convert $($DstIpAddress) to an IpAddress type.  Please verify your value is in the proper format and try again." 
        break 
    } 

    if ($SrcIpAddress -ne 0) { 
        try { 
            $SrcIp = [System.Net.IPAddress]::Parse($SrcIpAddress) 
            $SrcIp = [System.BitConverter]::ToInt32($SrcIp.GetAddressBytes(), 0) 
        } catch { 
            Write-Error "Could not convert $($SrcIpAddress) to an IpAddress type.  Please verify your value is in the proper format and try again." 
            break 
        } 
    } else { 
        $SrcIp = $SrcIpAddress 
    } 

    $New = New-Object PSObject -Property @{ 
        IpAddress = $DstIpAddress 
        PhysicalAddress = '' 
        Description = '' 
        ArpSuccess = $true 
    } | Select-Object IpAddress,PhysicalAddress,ArpSuccess,Description 

    $MacAddress = New-Object Byte[] 6 
    $MacAddressLength = [uint32]$MacAddress.Length 

    $Ret = [Network.Utils]::SendARP($DstIp, $SrcIp, $MacAddress, [ref]$MacAddressLength) 

    if ($Ret -ne 0) { 
        $New.Description =  "An error was returned from SendArp() with error code:  $($Ret)" 
        $New.ArpSuccess = $false 
    } else { 
        $MacFinal = @() 
        foreach ($b in $MacAddress) { 
            $MacFinal += $b.ToString('X2') 
        } 

        $New.PhysicalAddress = ($MacFinal -join ':') 
    } 

    Write-Output $New 
} 
  • 使用powershell 來解析一個跨網(wǎng)段的目標(biāo)IP地址,然后緊接著ping目標(biāo)主機(jī),這樣可以根據(jù)ping包的開始時間得出sendARP 的結(jié)束時間。
    powershell 命令如下:

    send-arp serverIP ;ping serverIP
  • 抓包過濾ARP以及ICMP來驗證,SendARP函數(shù)會發(fā)送三個ARP包,可能也會等待超時,因為沒有ARP包回應(yīng),這個測試的時間大概也在3.1秒左右,符合問題現(xiàn)象。
    無法回應(yīng)的ARP請求包導(dǎo)致的網(wǎng)站緩慢問題排錯

最后總結(jié):

  1. 在服務(wù)器上本機(jī)訪問非??欤且驗榉?wù)器使用ARP請求查本機(jī),應(yīng)該會很快有回應(yīng)。如果其他客戶端和服務(wù)器在同一個網(wǎng)段,估計也不會慢。

  2. 客戶端慢是因為服務(wù)器在返回給客戶端http信息時,先用ARP請求跨網(wǎng)段的客戶端IP,但是不會有ARP回應(yīng),因為路由的原因,客戶端看不到服務(wù)器的ARP請求,而SendARP函數(shù)的超時時間大概為3.1秒,所以跨網(wǎng)段的客戶端收到服務(wù)器的一個HTTP響應(yīng)在3.28秒左右。同樣單純在客戶端抓包只能分析出服務(wù)器應(yīng)用有問題,但是說不出具體的問題。

  3. 靜態(tài)網(wǎng)頁快是因為,靜態(tài)網(wǎng)頁不執(zhí)行服務(wù)器端代碼,所以不會執(zhí)行ARP查詢。

  4. 另外開發(fā)人員也應(yīng)該熟悉常見的網(wǎng)絡(luò)協(xié)議,像這次的代碼就會僅僅在特定場景下工作,如果網(wǎng)站是面向互聯(lián)網(wǎng)的話,那這個代碼將不會起到作用,反而影響性能
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI