I was experimenting with some hex scanner sources.
Following code works but is very slow:
Public Class frmMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFile.Click
Dim ArrayHold() As Byte
Dim Index As Integer = 0
Dim Str As New StringBuilder
Dim tStr As String = ""
Dim tempStr As String = ""
Dim IndexEnd As Integer = 0
Dim InputString As String = ""
OpenDia.Filter = "All Files|*.*"
If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim myStreamReader As StreamReader = Nothing
myStreamReader = File.OpenText(OpenDia.FileName)
InputString = myStreamReader.ReadToEnd()
ArrayHold = Encoding.Default.GetBytes(InputString)
Do
IndexEnd = Index + 9
For x As Integer = Index To IndexEnd
If x > UBound(ArrayHold) Then
tempStr = tempStr
Else
tStr = UCase(Convert.ToString(ArrayHold(x), 16))
If tStr.Length < 2 Then tStr = "0" & tStr
Str.Append(tStr)
tempStr = tempStr & Chr(ArrayHold(x))
End If
Next
Index = Index + 10
Loop While IndexEnd < UBound(ArrayHold)
If InStr(1, Str.ToString, "58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441", vbTextCompare) Then
Label1.Text = "Eicar-test-signature virus Detected!"
End If
End If
End Sub
End Class
To speed it up I can use this format in XML file:
?xml version="1.0"?>
<signatures>
<signature>
<name>Eicar-Test-Signature</name>
<hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
</signature>
<signature>
<name>Mid/Kakworm-Z</name>
<hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
</signature>
</signatures>
But I don't know how to read and implement XML files in VB.NET. Is it hard and can anyone help?
Okay, here is an example of how you can read your XML file:
Dim xml = <?xml version="1.0"?>
<signatures>
<signature>
<name>Eicar-Test-Signature</name>
<hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex>
</signature>
<signature>
<name>Mid/Kakworm-Z</name>
<hex>66732e4372656174655465787446696c652877642b276b616b2e72656727293b74322e77726974652827524547454449</hex>
</signature>
</signatures>
Dim dict As New Dictionary(Of String, String)
For Each signature As XElement In xml.Root.Elements
dict.Add(signature.<name>.Value, signature.<hex>.Value)
Next
Instead of having embedded XML in your code (as in above example), you would probably be using XDocument.Load
or XDocument.Parse
.
Neolisk covered the LINQ to XML part nicely. Here's a way to simplify your loop to one single For loop, using Step. Also, it uses the StreamReader
in a Using
block, which will ensure that the StreamReader
is properly closed and disposed of.
If OpenDia.ShowDialog = Windows.Forms.DialogResult.OK Then
Using myStreamReader As StreamReader = File.OpenText(openDia.FileName)
ArrayHold = Encoding.Default.GetBytes(myStreamReader.ReadToEnd())
End Using
Dim arrayLength As Integer = ArrayHold.Length - 1
For i As Integer = 0 To arrayLength Step 10
Str.Append(UCase(Convert.ToString(ArrayHold(i), 16).PadLeft(2, "0"c)))
Next
If dict.ContainsKey(Str.ToString()) Then
Label1.Text = dict(str.ToString())
End If
End If
This uses StringBuilder
without the extra strings and should give a minor boost in performance as well, though I'm betting your biggest hit was looping through the string you build one character at a time, checking each character.
Edit
Added code for checking the dictionary (based on Neolisk's answer for parsing the XML to a dictionary) for completeness.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.