Home > Back-end >  Find all strings in a binary file using c# or vb.net
Find all strings in a binary file using c# or vb.net

Time:02-19

I've to analyze some binary files and extract strings from them, here a sample in hex:

11 32 84 02 19 37 40 82 31 08 40 81 23 47 08 10 23 84 70 12 83 04 21 39 74 97 21 93 78 94 17 23 97 E9 27 37 29 71 93 79 12 76 12 93 66 18 62 76 61 27 48 65 6C 6C 6F 20 57 6F 72 6C 64 FF 13 2F 4F 21 3F 4F 21 34 F1 23 F4 F2 1F 34 2F 13 4F 21 F3 4F 1F 23 4F 12 F4 3F 21 F3 4F 12 F3 4F 12 3F 4F 12 3F 4F 23 1F 4F 12 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 66 69 6C 65 12 48 71 27 34 97 12 73 49 17 23 94 71 29 73 47 26 13 42 76 47 26 47 23 41 36 71 63 74 12 63 47 12 01 23 41 22 10 31 32 33 34 35 36 37 38 39 72 34 85 71 63 04

the result I need is that:

Hello World
This is a test file
123456789r4

of course this is only a sample, and string is not in a fixed position. Any advice to do that?

CodePudding user response:

It is not clear how you define a string, but

Imports System.Text

Module Module1

    Function HexToBytes(s As String) As IEnumerable(Of Byte)
        Return s.Split(" "c).Select(Function(h) Convert.ToByte(h, 16))
    End Function

    Sub Main()

        Dim s = "11 32 84 02 19 37 40 82 31 08 40 81 23 47 08 10 23 84 70 12 83 04 21 39 74 97 21 93 78 94 17 23 97 E9 27 37 29 71 93 79 12 76 12 93 66 18 62 76 61 27 48 65 6C 6C 6F 20 57 6F 72 6C 64 FF 13 2F 4F 21 3F 4F 21 34 F1 23 F4 F2 1F 34 2F 13 4F 21 F3 4F 1F 23 4F 12 F4 3F 21 F3 4F 12 F3 4F 12 3F 4F 12 3F 4F 23 1F 4F 12 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 66 69 6C 65 12 48 71 27 34 97 12 73 49 17 23 94 71 29 73 47 26 13 42 76 47 26 47 23 41 36 71 63 74 12 63 47 12 01 23 41 22 10 31 32 33 34 35 36 37 38 39 72 34 85 71 63 04"
        Dim sb = New StringBuilder()
        Dim strings As New List(Of String)

        For Each b In HexToBytes(s)
            If b >= 32 AndAlso b < 127 Then
                sb.Append(Chr(b))
            ElseIf sb.Length > 0 Then
                If sb.Length > 4 Then
                    strings.Add(sb.ToString())
                End If
                sb.Clear()
            End If
        Next

        Console.WriteLine(String.Join(vbCrLf, strings))

        Console.ReadLine()

    End Sub

End Module

Outputs

bva'Hello World
/O!?O!4
This is a test file
q)sG&
BvG&G#A6qct
123456789r4

So you just need to adjust it to the required criteria.

  • Related