• VBA读取网络划分的数据


      对于目标检测,通过NMap可以得到IP地址信息,为XML文件,现在需要对应到具体的单位。

      这个工作很多单位都在做,比如搜索到一些存在高危端口或者漏洞的计算机,现在需要将信息下发到具体的单位方便整改,这就需要做一些准备工作。

      对于这个工作,想法很简单也最好实施:就准备一个数据表,准备单位名称、网络起始地址,具体对应的时候就可以通过IP网关信息和子网掩码找到对应的网络起始地址,这样就好对应了。想法是好的,问题是目前只有IP地址信息,没有网关地址和子网掩码就不能得到网络的首地址也就无法按这样的方法进行比较。

      可以按照下面的操作方法进行对比。

      ⑴准备一个Vlan信息表
        包含信息:单位名称、网络地址前三位、IP起始地址、IP结束地址

      ⑵准备一个数据表存储特殊例外的IP信息

        包括信息:单位名称、IP地址、MAC地址、主机名称、备注信息

      为了加快检索速度,需要将这些信息一次性读入内存,这样对比就很快,尽量减少重复信息,使用的方法是先定义结构体数组,然后在结构体数组中再定义结构体数组。

      声明:

    1. Option Explicit
    2. Public MyIPAddressIndex(5) As IPAddrIndex
    3. Public ArrCount(5) As Integer
    4. Public MyIPSpecialIP() As SpecialIP
    5. Public MyIPToHostName As IPToHostName
    6. Type IPAddressRangInfo
    7. unit As String '单位名称
    8. StartAddr As Integer '起始地址
    9. EndAddr As Integer '结束地址
    10. End Type
    11. Type SpecialIP
    12. unit As String '单位名称
    13. IPAddr As String 'IP地址
    14. MacAddr As String 'MAC地址
    15. HostName As String '主机名称
    16. Memo As String '备注
    17. End Type
    18. Type IPAddrIndex
    19. Prefix As String '网络地址前三位
    20. MyIPAddrRange() As IPAddressRangInfo 'IP地址范围信息
    21. End Type
    22. Type IPToHostName
    23. HostName As String '主机名称
    24. Unit As String '单位名称
    25. End Type

      读取信息:

    1. Sub ReadyNetworkData()
    2. Dim ws As Worksheet
    3. Dim PrefixCol As Range
    4. Dim UnitCol As Range
    5. Dim StartAddrCol As Range
    6. Dim EndAddrCol As Range
    7. Dim MaxRows As Integer
    8. Dim iFor As Integer
    9. Dim StrPrefix As String
    10. Dim StrCurrent As String
    11. Dim ICount As Integer
    12. Dim ISubCount As Integer
    13. Dim ArrIndex As Integer
    14. Dim StrAddrStart As String
    15. Dim StrAddrEnd As String
    16. Dim StrUnit As String
    17. Dim StrTemp As String
    18. Dim ITemp As Integer
    19. Dim UnitCol_B As Range
    20. Dim IPAddrCol As Range
    21. Dim MacAddrCol As Range
    22. Dim HontNameCol As Range
    23. Dim MemoCol As Range
    24. '读取Vlan信息表
    25. Set ws = Worksheets("VlanMarkings")
    26. MaxRows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    27. Set PrefixCol = ws.Range("H2:H" & Trim(str(MaxRows)))
    28. Set UnitCol = ws.Range("B2:B" & Trim(str(MaxRows)))
    29. Set StartAddrCol = ws.Range("I2:I" & Trim(str(MaxRows)))
    30. Set EndAddrCol = ws.Range("J2:J" & Trim(str(MaxRows)))
    31. StrPrefix = ""
    32. '读取有多少段地址
    33. ICount = 0
    34. For iFor = 1 To MaxRows - 1
    35. '读取IP地址
    36. StrCurrent = UCase(Trim(PrefixCol(iFor).Value))
    37. If StrPrefix <> StrCurrent Then
    38. MyIPAddressIndex(ICount).Prefix = StrCurrent
    39. StrPrefix = StrCurrent
    40. ICount = ICount + 1
    41. End If
    42. Next
    43. '每段有多少个细分范围
    44. For iFor = 0 To UBound(MyIPAddressIndex)
    45. StrTemp = MyIPAddressIndex(iFor).Prefix
    46. ITemp = WorksheetFunction.CountIf(ws.Range("H2:H" & Trim(str(MaxRows))), "=" + StrTemp)
    47. ArrIndex = GetArrIndex(StrTemp)
    48. ReDim MyIPAddressIndex(ArrIndex).MyIPAddrRange(0 To ITemp)
    49. Next
    50. 'GoTo L123
    51. '读取每段地址范围
    52. For iFor = 0 To 5
    53. ArrCount(iFor) = 0
    54. Next
    55. For iFor = 1 To MaxRows - 1
    56. '读取IP地址
    57. StrPrefix = UCase(Trim(PrefixCol(iFor).Value))
    58. ArrIndex = GetArrIndex(StrPrefix)
    59. StrAddrStart = UCase(Trim(StartAddrCol(iFor).Value))
    60. StrAddrEnd = UCase(Trim(EndAddrCol(iFor).Value))
    61. StrUnit = UCase(Trim(UnitCol(iFor).Value))
    62. MyIPAddressIndex(ArrIndex).MyIPAddrRange(ArrCount(ArrIndex)).StartAddr = StrAddrStart
    63. MyIPAddressIndex(ArrIndex).MyIPAddrRange(ArrCount(ArrIndex)).EndAddr = StrAddrEnd
    64. MyIPAddressIndex(ArrIndex).MyIPAddrRange(ArrCount(ArrIndex)).unit = StrUnit
    65. ArrCount(ArrIndex) = ArrCount(ArrIndex) + 1
    66. Next
    67. '读取IP地址信息表
    68. Set ws = Worksheets("SpecialMarkingsRes")
    69. MaxRows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    70. ReDim MyIPSpecialIP(0 To MaxRows - 1)
    71. Set UnitCol_B = ws.Range("B2:B" & Trim(str(MaxRows)))
    72. Set IPAddrCol = ws.Range("C2:C" & Trim(str(MaxRows)))
    73. Set MacAddrCol = ws.Range("D2:D" & Trim(str(MaxRows)))
    74. Set HontNameCol = ws.Range("E2:E" & Trim(str(MaxRows)))
    75. Set MemoCol = ws.Range("F2:F" & Trim(str(MaxRows)))
    76. For iFor = 1 To MaxRows - 1
    77. MyIPSpecialIP(iFor - 1).HostName = UCase(Trim(HontNameCol(iFor).Value))
    78. MyIPSpecialIP(iFor - 1).IPAddr = UCase(Trim(IPAddrCol(iFor).Value))
    79. MyIPSpecialIP(iFor - 1).MacAddr = UCase(Trim(MacAddrCol(iFor).Value))
    80. MyIPSpecialIP(iFor - 1).Memo = UCase(Trim(MemoCol(iFor).Value))
    81. MyIPSpecialIP(iFor - 1).unit = UCase(Trim(UnitCol_B(iFor).Value))
    82. ICount = ICount + 1
    83. Next
    84. L123:
    85. '验证读取的数据
    86. 'Dim Jfor As Integer
    87. 'For iFor = 0 To 5
    88. 'For Jfor = LBound(MyIPAddressIndex(iFor).MyIPAddrRange) To UBound(MyIPAddressIndex(iFor).MyIPAddrRange)
    89. 'Debug.Print Str(MyIPAddressIndex(iFor).MyIPAddrRange(Jfor).StartAddr) + "|" + Str(MyIPAddressIndex(iFor).MyIPAddrRange(Jfor).EndAddr) + "|" + MyIPAddressIndex(iFor).MyIPAddrRange(Jfor).Unit
    90. 'Next
    91. 'Next
    92. 'Debug.Print MaxRows - 1
    93. 'For iFor = 0 To UBound(MyIPSpecialIP) - 1
    94. ' Debug.Print MyIPSpecialIP(iFor).HostName + "|" + MyIPSpecialIP(iFor).IPAddr + "|" + MyIPSpecialIP(iFor).MacAddr + "|" + MyIPSpecialIP(iFor).Memo + "|" + MyIPSpecialIP(iFor).unit
    95. 'Next
    96. Debug.Print "完成"
    97. End Sub
    98. Function GetArrIndex(str As String) As Integer
    99. Dim iFor As Integer
    100. For iFor = 0 To 5
    101. If MyIPAddressIndex(iFor).Prefix = str Then
    102. GetArrIndex = iFor
    103. Exit For
    104. End If
    105. Next
    106. End Function
    107. Sub PrintIPAddressRang(ByVal IArrIndex As Integer)
    108. Dim JFor As Integer
    109. Debug.Print "前缀:" + MyIPAddressIndex(IArrIndex).Prefix
    110. For JFor = LBound(MyIPAddressIndex(IArrIndex).MyIPAddrRange) To UBound(MyIPAddressIndex(IArrIndex).MyIPAddrRange)
    111. Debug.Print str(MyIPAddressIndex(IArrIndex).MyIPAddrRange(JFor).StartAddr) + "|" + str(MyIPAddressIndex(IArrIndex).MyIPAddrRange(JFor).EndAddr) + "|" + MyIPAddressIndex(IArrIndex).MyIPAddrRange(JFor).Unit
    112. Next
    113. End Sub

      判断函数:

    1. Function GetArrIndex(ByVal str As String) As Integer
    2. Dim iFor As Integer
    3. For iFor = 0 To 5
    4. If MyIPAddressIndex(iFor).Prefix = str Then
    5. GetArrIndex = iFor
    6. Exit For
    7. End If
    8. Next
    9. End Function
    10. Function GetUserUnit(ByVal StrIpAddr As String) As String
    11. GetUserUnit = "未知"
    12. '获取IP地址的前三位
    13. Dim LastDotPos As Integer
    14. Dim Prefix As String
    15. Dim LastIP As Integer
    16. Dim iFor As Integer
    17. Dim ArrIndex As Integer
    18. For iFor = Len(StrIpAddr) To 1 Step -1
    19. If Mid(StrIpAddr, iFor, 1) = "." Then
    20. LastDotPos = iFor
    21. Exit For
    22. End If
    23. Next iFor
    24. '判断具体的使用单位
    25. Prefix = Mid(StrIpAddr, 1, LastDotPos - 1)
    26. LastIP = Val(Right(StrIpAddr, Len(StrIpAddr) - LastDotPos))
    27. '根据前三位确定具体的网段
    28. ArrIndex = GetArrIndex(Prefix)
    29. '在网段内进行比较
    30. For iFor = LBound(MyIPAddressIndex(ArrIndex).MyIPAddrRange) To UBound(MyIPAddressIndex(ArrIndex).MyIPAddrRange) - 1
    31. If LastIP >= MyIPAddressIndex(ArrIndex).MyIPAddrRange(iFor).StartAddr And LastIP <= MyIPAddressIndex(ArrIndex).MyIPAddrRange(iFor).EndAddr Then
    32. GetUserUnit = MyIPAddressIndex(ArrIndex).MyIPAddrRange(iFor).Unit
    33. 'Debug.Print Prefix + "||" + str(LastIP) + "||" + str(ArrIndex) + "||" + GetUserUnit
    34. Exit For
    35. End If
    36. Next
    37. End Function
    38. Function GetHostName(ByVal StrIpAddr As String) As IPToHostName
    39. Dim TempIPTOHostName As IPToHostName
    40. TempIPTOHostName.HostName = ""
    41. TempIPTOHostName.Unit = ""
    42. Dim iFor As Integer
    43. For iFor = LBound(MyIPSpecialIP) To UBound(MyIPSpecialIP)
    44. If MyIPSpecialIP(iFor).IPAddr = StrIpAddr Then
    45. TempIPTOHostName.HostName = MyIPSpecialIP(iFor).HostName
    46. TempIPTOHostName.Unit = MyIPSpecialIP(iFor).Unit
    47. GetHostName = TempIPTOHostName
    48. Exit For
    49. End If
    50. Next iFor
    51. End Function
    52. Sub JuedeIpToUnit()
    53. '根据IP地址得到单位名称
    54. Dim iFor As Integer
    55. Dim StrIpAddr As String
    56. Dim Unit As String
    57. Dim HostName As String
    58. Dim IRecord As Integer
    59. ReadyNetworkData
    60. StartRow = 2
    61. EndRow = 200
    62. Sheets("BeDetectedData").Activate
    63. '读取IP地址数据并进行判断
    64. For iFor = StartRow To EndRow
    65. Range("B" & Trim(str(iFor))).Select
    66. StrIpAddr = Trim(Selection.FormulaR1C1)
    67. Unit = GetUserUnit(StrIpAddr)
    68. HostName = GetHostName(StrIpAddr)
    69. '存入表中
    70. '所属单位
    71. Range("C" & Trim(str(IRecord))).Select
    72. Selection.FormulaR1C1 = Unit
    73. '主机名称
    74. Range("D" & Trim(str(IRecord))).Select
    75. Selection.FormulaR1C1 = HostName
    76. Next iFor
    77. MsgBox "完成!"
    78. End Sub

      有了上面的信息,就可以对比检索NMap保存的XML文件,将IP地址和具体单位一一对应起来了,剩下的工作就比较好做了。

      下面的代码是根据给定的IP得到这个IP地址对应的单位以及是否是网关地址、网络打印机、视频设备等(这个数据可以根据需要添加)。

    1. Sub JudgeData()
    2. Dim XmlFile As String
    3. Dim CurrentFilePath As String
    4. CurrentFilePath = Application.ActiveWorkbook.Path
    5. Dim XmlDoc As DOMDocument60
    6. Dim hostNodes As Object
    7. Dim iFor As Integer
    8. Dim hostnode As Object
    9. Dim addressNode As Object
    10. Dim StrIpAddr As String
    11. Dim Unit As String
    12. Dim HostName As String
    13. Dim IRecord As Integer
    14. IRecord = 219
    15. Set XmlDoc = New DOMDocument60
    16. XmlFile = CurrentFilePath + "\20231018-B.xml"
    17. ReadyNetworkData
    18. Sheets("BeDetectedData").Activate
    19. If XmlDoc.Load(XmlFile) = True Then
    20. '读取数据并进行判断
    21. Set hostNodes = XmlDoc.getElementsByTagName("host")
    22. '遍历host节点集合,获取IP地址
    23. For iFor = 0 To hostNodes.Length - 1
    24. Set hostnode = hostNodes(iFor)
    25. Set addressNode = hostnode.getElementsByTagName("address")(0)
    26. If addressNode.getAttribute("addrtype") = "ipv4" Then
    27. StrIpAddr = addressNode.getAttribute("addr")
    28. Unit = GetUserUnit(StrIpAddr)
    29. '存入表中
    30. IRecord = IRecord + 1
    31. 'IP地址
    32. Range("B" & Trim(str(IRecord))).Select
    33. Selection.FormulaR1C1 = StrIpAddr
    34. '所属单位
    35. Range("C" & Trim(str(IRecord))).Select
    36. Selection.FormulaR1C1 = Unit
    37. '主机名称
    38. MyIPToHostName.HostName = ""
    39. MyIPToHostName.Unit = ""
    40. MyIPToHostName = GetHostName(StrIpAddr)
    41. If MyIPToHostName.HostName <> "" And MyIPToHostName.Unit <> "" Then
    42. Range("D" & Trim(str(IRecord))).Select
    43. Selection.FormulaR1C1 = MyIPToHostName.HostName
    44. Range("C" & Trim(str(IRecord))).Select
    45. Selection.FormulaR1C1 = MyIPToHostName.Unit
    46. End If
    47. End If
    48. TempCount = TempCount + 1
    49. MsgBox "完成!"
    50. Else
    51. MsgBox "检查XML文件内容!"
    52. End If
    53. End Sub

      今天处理NMap输出的XML文件时遇到错误,我还以为是代码有问题,郁闷了很长时间,后面查明是NMap添加了无用信息导致,删除就好了。

  • 相关阅读:
    矩阵秩的定义和相关结论汇总
    EIP-3664合约研究笔记03--装备属性随机生成算法
    5G+北斗高精度定位系统适用于哪些行业领域?
    12月2日:thinkphp中数据库完结
    [PyTorch][chapter 60][强化学习-2-有模型学习2]
    人工智能轨道交通行业周刊-第21期(2022.10.31-11.6)
    2023平台工程崭露头角,AI 带来新机遇与挑战
    uniapp制作安卓原生插件踩坑
    mysql性能优化之数据类型(持续更新)
    【QML】QML中的动画总结
  • 原文地址:https://blog.csdn.net/dawn0718/article/details/133791583