我正在开发一个允许选择单个文件的小工具。它将计算SHA2 hash
并在简单的GUI中显示它,然后获取该值并检查该哈希是否列在黑名单文本文件中。如果它被列出,那么它会将其标记为脏,如果不是,它将把它作为干净传递。
但是,经过数小时的谷歌搜索并筛选了许多在线资源后,我决定让我们提出建议和帮助。
那说虽然我的程序工作但我似乎遇到了问题,因为无论我做什么,它只会读取我的“黑名单”的第一行并拒绝阅读整个列表或者实际上逐行查看是否有匹配。
无论我有100或1 SHA2 hash
。
例如,如果我要将5个文件添加到所谓的黑名单中。通过预先计算他们的SHA2 value
。然后,无论我的小工具将只标记一个被列入黑名单的文件作为匹配。
然而,当我使用reset
按钮并选择一个不同的(也列入黑名单)文件时,它将其传递为干净而不是。据我所知,它始终是第一个SHA2 hash
它似乎标志并忽略其他人。我个人认为该程序甚至没有检查超出第一个哈希。
现在黑名单文件非常简单。 *例如:
1afde1cbccd2ab36f90973cb985072a01ebdc64d8fdba6a895c855d90f925043
2afde1cbccd2ab36f90973cb985072a01ebdc64d8fdba6a895c855d90f925043
3afde1cbccd2ab36f90973cb985072a01ebdc64d8fdba6a895c855d90f925043
4afde1cbccd2ab36f90973cb985072a01ebdc64d8fdba6a895c855d90f925043
....等等。 因此,您可以看到列出的这些虚假示例哈希没有任何详细信息。 现在我的程序假设从所选文件中计算哈希值。
实施例: somefile.exe(或任何扩展名) 它的5KB大小和SHA2值将是:
3afde1cbccd2ab36f90973cb985072a01ebdc64d8fdba6a895c855d90f925043
你可以看到我从示例列表中获取了第三个哈希吗?
现在,如果我选择somefile.exe
进行扫描,那么它会将其传递为干净。虽然被列入黑名单。所以,如果我将此哈希移动到第一个位置。然后我的小程序正确标记它。
如此长的故事简短我认为我的代码出现了一些可怕的错误,即使它似乎有效。 无论如何这是我到目前为止所得到的:
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports MetroFramework.Forms
Public Class Fsmain
Function SHA256_SIG(ByVal file_name As String)
Return SHA256_engine("SHA-256", file_name)
End Function
Function SHA256_engine(ByRef hash_type As String, ByRef file_name As String)
Dim SIG
SIG = SHA256.Create()
Dim hashValue() As Byte
Dim filestream As FileStream = File.OpenRead(file_name)
filestream.Position = 0
hashValue = SIG.ComputeHash(filestream)
Dim hash_hex = PrintByteArray(hashValue)
Stream.Null.Close()
Return hash_hex
End Function
Public Function PrintByteArray(ByRef array() As Byte)
Dim hex_value As String = ""
Dim i As Integer
For i = 0 To array.Length - 1
hex_value += array(i).ToString("x2")
Next i
Return hex_value.ToLower
End Function
Private Sub Browsebutton_Click(sender As Object, e As EventArgs) Handles Browsebutton.Click
If SampleFetch.ShowDialog = DialogResult.OK Then
Dim path As String = SampleFetch.FileName
Selectfile.Text = path
Dim Sample As String
Sample = SHA256_SIG(path)
SignatureREF.Text = SHA256_SIG(path)
Using f As System.IO.FileStream = System.IO.File.OpenRead("blacklist.txt")
Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
While Not s.EndOfStream
Dim line As String = s.ReadLine()
If (line = Sample) Then
Result.Visible = True
SignatureREF.Visible = True
Result.Text = "Dirty"
Resetme.Visible = True
RemoveMAL.Visible = True
Else
Result.Visible = True
SignatureREF.Visible = True
Result.Text = "Clean"
Resetme.Visible = True
RemoveMAL.Visible = False
End If
End While
End Using
End Using
End If
End Sub
Private Sub Fsmain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Result.Visible = False
SignatureREF.Visible = False
Resetme.Visible = False
RemoveMAL.Visible = False
End Sub
Private Sub Resetme_Click(sender As Object, e As EventArgs) Handles Resetme.Click
Selectfile.Text = Nothing
SignatureREF.Text = Nothing
Result.Visible = False
SignatureREF.Visible = False
Resetme.Visible = False
RemoveMAL.Visible = False
End Sub
Private Sub RemoveMAL_Click(sender As Object, e As EventArgs) Handles RemoveMAL.Click
Dim ask As MsgBoxResult = MsgBox("Would you like to remove the Dirty file?", MsgBoxStyle.YesNo, MessageBoxIcon.None)
If ask = MsgBoxResult.Yes Then
System.IO.File.Delete(Selectfile.Text$)
Else
MsgBox("You sure you want to keep this file?")
Dim filepath As String = IO.Path.Combine("c:\Dirty\", "Dirty.txt")
Using sw As New StreamWriter(filepath)
sw.WriteLine(" " & DateTime.Now)
sw.WriteLine(" " & Selectfile.Text)
sw.WriteLine(" " & SignatureREF.Text)
sw.WriteLine(" " & Result.Text)
sw.WriteLine("-------------------")
sw.Close()
End Using
End If
End Sub
End Class
所以,如果你们中的任何一个人都能看到它并指出错误,甚至可以提出一个很好的解决方案。
答案 0 :(得分:0)
如果您有一个String
并且想要测试它是否与文本文件的行匹配,那么您可以使用这个简单的单行:
If IO.File.ReadLines(filePath).Contains(myString) Then
答案 1 :(得分:0)
使程序正常工作最简单的方法是测试是否验证了已定义的条件。如果满足该条件,则终止测试。
使用布尔变量,报告测试结果并相应地采取行动。
Using
声明负责处理StreamReader
。
您可以通过这种方式修改程序:
Private Sub Browsebutton_Click(sender As Object, e As EventArgs) Handles Browsebutton.Click
If SampleFetch.ShowDialog <> DialogResult.OK Then Exit Sub
Dim Sample As String = SHA256_SIG(SampleFetch.FileName)
SignatureREF.Text = Sample
Dim IsDirty As Boolean = False
Using _Reader As StreamReader = New StreamReader("blacklist.txt", True)
While _Reader.Peek() > 0
Dim _Line As String = _Reader.ReadLine()
If (_Line = Sample) Then
IsDirty = True
Exit While
End If
End While
End Using
If IsDirty Then
'(...)
RemoveMAL.Visible = True
Result.Text = "Dirty"
Else
'(...)
RemoveMAL.Visible = False
Result.Text = "Clean"
End If
End Sub