Results 1 to 20 of 20

Thread: signature search

  1. #1

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    signature search

    i want search in anthor array
    for example
    dim a as string ,b () as byte
    read one file into b
    a="1234????5678??12"
    search a in b, is find return offset .
    who can have a good idea thanks
    sorry my poor English$

  2. #2

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: signature search

    Here's another simple approach that works best on small files:

    Code:
    Option Explicit
    
    Private Sub Main()
        Dim BinaryFile() As Byte, Signature() As Byte
        Dim FN As Integer, Pos As Long, SigLen As Long
    
        FN = FreeFile
    
        Open "BinaryFile.bin" For Binary Access Read As FN
            ReDim BinaryFile(0& To LOF(FN) - 1&) As Byte
            Get FN, , BinaryFile()
        Close FN
    
        Signature() = StrConv("1234????5678??12", vbFromUnicode)
        SigLen = UBound(Signature) + 1&
        Pos = 1& - SigLen
    
        Do: Pos = InStrB(Pos + SigLen, BinaryFile(), Signature())
            If Pos Then Debug.Print Pos Else Exit Do
        Loop
    End Sub   'NOTE: InStrB() returns a 1-based offset!
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: signature search

    I doubt that either of those solutions will match the pattern.
    I'm assuming some regex type matching is wanted, where the ? in the string match any character in that position.
    I think neither "Like" nor InStrB will do wildcard character matching for the question mark characters in the pattern string.

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: signature search

    Well, the search using InStrB could be modified like the following:

    1. Find "1234".
    2. If found, skip 4 bytes ("????").
    3. If next 4 bytes are "5678"
    4. then skip 2 bytes ("??").
    5. If next 2 bytes are "12"
    6. then return offset found in step 1.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: signature search

    Pure-vb binary version:
    Code:
    Option Explicit
     
    Private Sub Form_Load()
        Dim A() As Byte, P() As Byte, M() As Byte, N As Long
        
        ReDim A(99)                     ' array
        ReDim P(4)                      ' sequence
        ReDim M(4)                      ' mask
        
        For N = 0 To 99: A(N) = Rnd * 10: Next
        
        A(90) = 1: A(91) = 2: A(92) = 3: A(93) = 4: A(94) = 5
        
        P(0) = 1: P(1) = 2: P(2) = 3: P(3) = 4: P(4) = 5    ' // Set signature
        M(0) = 1: M(1) = 0: M(2) = 0: M(3) = 1: M(4) = 1    ' // Set mask
        
        MsgBox Format(FindSignature(A, P, M), "0;not found;0")
        
    End Sub
    
    Private Function FindSignature( _
                     A() As Byte, _
                     P() As Byte, _
                     M() As Byte) As Long
        Dim N As Long, Z As Long
        
        N = 0: Z = 0
        
        Do While N <= UBound(A)
            If A(N) = P(Z) Or M(Z) = 0 Then
                Z = Z + 1
                If Z > UBound(P) Then
                    FindSignature = N - UBound(P)
                    Exit Function
                End If
            Else
                If Z Then
                    N = N - Z + 1
                    Z = 0
                End If
            End If
            N = N + 1
        Loop
        
        FindSignature = -1
        
    End Function
    You should add a mask where zero implies ?.

  7. #7

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    thanks.i know~

  8. #8

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    good idea, �� I want to write a packer, may be I need to use the feature code positioning, so the need to support fuzzy search!

  9. #9

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    Quote Originally Posted by Bonnie West View Post
    Well, the search using InStrB could be modified like the following:

    1. Find "1234".
    2. If found, skip 4 bytes ("????").
    3. If next 4 bytes are "5678"
    4. then skip 2 bytes ("??").
    5. If next 2 bytes are "12"
    6. then return offset found in step 1.
    I know, but not very fast if I search in a big file

  10. #10
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: signature search

    Quote Originally Posted by xxdoc View Post
    I know, but not very fast if I search in a big file
    You are/were writing a packer, then why you 'need' to search a big file, usually header part and couple of kB after that is enough.

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: signature search

    First read some articles about data compression.
    Huffman tables, sliding dictionaries and such things come to mind

  12. #12
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: signature search

    Yeah right, first learn how (exe etc.) packer applications are working and introduce NT PE-file header construction to yourself also and what comes to data compression 'usually' signature bytes are in the very beginning of compressed packages.

    https://en.wikipedia.org/wiki/List_of_file_signatures

    So in plain english if one is packing executable or what ever file, then one might want to put signature in the beginning of compressed part. New PE header, which contain extractor (few kilobtyes), then compressed original file starting with signature bytes.
    Last edited by Tech99; Jan 27th, 2016 at 05:59 AM.

  13. #13

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    i want write a Universal patch generator !

  14. #14

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    i want write a Universal patch generator !thanks
    i will bu used KMP to search

  15. #15

  16. #16

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    your ider is very good。
    Last edited by xxdoc; Jan 28th, 2016 at 07:55 AM.

  17. #17

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    ����thank
    Last edited by xxdoc; Jan 28th, 2016 at 07:56 AM.

  18. #18
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,687

    Re: signature search

    Don't use a string to work with binary data.
    You should use a mask to set the significant bytes.
    For example you have a file, and you want to find the place where the signature is placed:
    Name:  sign_1.PNG
Views: 320
Size:  7.0 KB
    The red rectangles are fixed data, and therebetween is placed the variable data. You should set the mask:
    Code:
    10110111
    ,
    i.e. find 1'st byte, 2'd byte is ignored, 3'rd and 4'th is considered, etc.
    Read the file to a byte array and set the signature with mask:
    Code:
    Option Explicit
     
    Private Sub Form_Load()
        Dim A() As Byte, P() As Byte, M() As Byte, f As Integer
        
        f = FreeFile
        Open App.Path & "\bin" For Binary As f
        ReDim A(LOF(f) - 1)
        Get f, , A
        Close f
        
        ReDim P(7): ReDim M(7)
        
        P(0) = &HFF:    P(2) = &H29:    P(3) = &H54:    P(5) = &H61:    P(6) = &H12:    P(7) = &H54
        M(0) = 1:       M(2) = 1:       M(3) = 1:       M(5) = 1:       M(6) = 1:       M(7) = 1
        
        MsgBox Format(Hex$(FindSignature(A, P, M)), "0;not found;0")
        
    End Sub
    
    Private Function FindSignature( _
                     A() As Byte, _
                     P() As Byte, _
                     M() As Byte) As Long
        Dim N As Long, Z As Long
        
        N = 0: Z = 0
        
        Do While N <= UBound(A)
            If A(N) = P(Z) Or M(Z) = 0 Then
                Z = Z + 1
                If Z > UBound(P) Then
                    FindSignature = N - UBound(P)
                    Exit Function
                End If
            Else
                If Z Then
                    N = N - Z + 1
                    Z = 0
                End If
            End If
            N = N + 1
        Loop
        
        FindSignature = -1
        
    End Function
    Attached Files Attached Files

  19. #19

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    Thanks !

  20. #20

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    47

    Re: signature search

    your answer very Good~,thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width