如何防止修改hosts文件使域名转向

'判断域名hosta、hostb是否存在hosts文件中,存在返回真,不存在返回假

Private Function ChkHosts(ByVal hosta As String, ByVal hostb As String) As Boolean

Const MAX_LEN = 255 '字符串最大长度
Dim buffer As String * MAX_LEN '存放结果的固定长度的字符串
Dim nLng As Long '字符串的实际长度
Dim dirstr As String '保存System32目录
Dim hostsStr As String '保存修改后的HOSTS文件
Dim linestr As String
Dim demo As String
On Error GoTo OpenErr
nLng = GetSystemDirectory(buffer, MAX_LEN) '获取System32目录 及字符长度
dirstr = Left(buffer, nLng) '取其中一部分
Open dirstr & "\drivers\etc\hosts" For Input As #1 '打开hosts文件
Do While Not EOF(1)
    Line Input #1, linestr
    If Asc(linestr & "#") <> 35 Then ' Asc("#") = 35 '字符串首字符是否为#号开头的注释或者空行
        demo = Trim(Mid(Trim(linestr), InStr(1, Trim(linestr), " ") + 1, Len(Trim(linestr)) - InStr(1, Trim(linestr), " ")))
        If demo = hosta Or demo = hostb Then '检查是否有域名
             Close #1 '关闭文件
             ChkHosts = True
            Exit Function
        End If
    End If
    hostsStr = hostsStr & linestr & vbCrLf
Loop
hostsStr = Mid(hostsStr, 1, Len(hostsStr) - 1) '去掉最后一个回车符
Close #1 '关闭文件

OpenErr:

ChkHosts = False

End Function

赞(0) 打赏
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

    暂无评论...