【问题标题】:How to get a mac address from scanning a local network when Firewall is blocking ping防火墙阻止ping时如何通过扫描本地网络获取mac地址
【发布时间】:2020-10-28 12:20:46
【问题描述】:

对于我目前正在用 Delphi 编写的安全产品。我正在使用以下解决方案来使用 ARP 表从设备获取 MAC 地址以检测网络上的内容。

How to find MAC addresses from arp -a scan

我只是执行一系列 Ping 命令来填充 ARP 表并从 ARP 表中读取结果。

但是,当计算机上的防火墙阻止 Ping 时。有时 MAC 仍然暴露在 ARP 中,但并非总是如此。检测网络上的所有设备并从中获取 MAC 地址的更好解决方案是什么?

//-----------------------------------------------------------------------------
{ ARP-table lists relations between remote IP and remote MAC-address.
 NOTE: these are cached entries;when there is no more network traffic to a
 node, entry is deleted after a few minutes.
}
//-----------------------------------------------------------------------------
procedure Get_ARPTable( aList: TStrings; aDeviceList : TObjectList<TNetworkDevice>);
var
  lIPNetRow    : TMibIPNetRow;
  lTableSize   : DWORD;
  lNumEntries  : DWORD;
  lErrorCode   : DWORD;
  lIdx         : Integer;
  lPBuf        : PAnsiChar;
  lPhysAddr    : TMACAddress;
  lMacAddr2Str : string;
  ldwAddr      : string;
  ldwType      : string;
  lNewDevice   : TNetworkDevice;
begin
  if not LoadIpHlp then Exit;
  if not Assigned( aList ) then Exit;
  if not Assigned( aDeviceList) then Exit;

  Get_LocalNetworkDevices(aDeviceList);

  aList.Clear;
  lTableSize := 0;
  lErrorCode := GetIPNetTable( Nil, @lTableSize, False );
  //
  if lErrorCode = ERROR_NO_DATA then
  begin
    aList.Add( ' ARP-cache empty.' );
    EXIT;
  end;
  // get table
  GetMem( lPBuf, lTableSize );
  lNumEntries := 0;
  try
  lErrorCode := GetIpNetTable( PTMIBIPNetTable( lPBuf ), @lTableSize, False );
  if lErrorCode = NO_ERROR then
  begin
    lNumEntries := PTMIBIPNetTable( lPBuf )^.dwNumEntries;

    if lNumEntries > 0 then
    begin
      Inc( lPBuf, SizeOf( DWORD ) );
      for lIdx := 1 to lNumEntries do
      begin
        lIPNetRow := PTMIBIPNetRow( lPBuf )^;

        lMacAddr2Str := MacAddr2Str( lIPNetRow.bPhysAddr, lIPNetRow.dwPhysAddrLen );
        lPhysAddr := lIPNetRow.bPhysAddr;
        ldwAddr := IPAddr2StrTrunc(lIPNetRow.dwAddr);
        ldwType := ARPEntryType[lIPNetRow.dwType];

        lNewDevice := SeekDevice(aDeviceList, lMacAddr2Str);

        if Assigned(lNewDevice) then
        begin
          lNewDevice.IP := ldwAddr;
          lNewDevice.IsNew := False;
          lNewDevice.EntryType :=  ARPEntryType[lIPNetRow.dwType];
          if (lNewDevice.EntryType = 'Dynamic') or
             (lNewDevice.EntryType = 'Static') then
                 lNewDevice.SetStamp;
        end
        else
        begin
          lNewDevice := TNetworkDevice.Create;
          lNewDevice.IP := ldwAddr;
          lNewDevice.EntryType  := ARPEntryType[lIPNetRow.dwType];
          lNewDevice.AddOrUpdate(lMacAddr2Str);
          lNewDevice.SetFirstSeen;
          lNewDevice.SetStamp;
          lNewDevice.State := dtRogue;
          lNewDevice.IsNew := True;
          aDeviceList.Add(lNewDevice);
        end;

        with lIPNetRow do
        begin
          aList.Add( Format( '%8x | %12s | %16s| %10s',
                           [dwIndex, MacAddr2Str( bPhysAddr, dwPhysAddrLen ),
                           IPAddr2Str( dwAddr ), ARPEntryType[dwType]
                           ]));
        end;
        Inc( lPBuf, SizeOf( lIPNetRow ) );
      end;
    end
    else
      aList.Add( ' ARP-cache empty.' );
  end
  else
    aList.Add( SysErrorMessage( lErrorCode ) );

  // we _must_ restore Pointer!
  finally
      Dec( lPBuf, SizeOf( DWORD ) + lNumEntries * SizeOf( lIPNetRow ) );
      FreeMem( lPBuf );
  end;
end;

【问题讨论】:

    标签: ping delphi-2010 mac-address


    【解决方案1】:

    为了补充 ping,您可以尝试在任何端口上建立 TCP 连接,例如端口 80 (HTTP)。当然,大多数情况下您都会遇到连接被拒绝,但您仍然可以在 ARP 表中找到一个条目。

    根据防火墙的不同,您也可能完全得到响应。然后你必须尝试另一个端口。如果启用了网络共享,我建议应该打开 135(RPC)或 445 或 139(两个 SMB)。如果启用,RDP 将使用 3389 中的另一个。

    这仅适用于小型(C 类,256 个地址)或中等(B 类,16K 地址)的子网。无论如何,这需要很多时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 2014-09-22
      • 1970-01-01
      相关资源
      最近更新 更多