regexdllgrep32bit-64bitfile-search

What regex strings can distinguish files containing "PE null null L" from "PE null null d"


I need a quick and easy way to know how many dlls are 32-bit and how many are 64-bit in a given directory. I was about to write a PowerShell script when I thought of a much simpler solution. I've shown below that my idea can work but I need a little regex help to make it work properly.

It has been demonstrated that a dll file can be opened in Notepad to reveal the bitness (32 or 64) simply by checking the character after the first "PE". The letters "L" and "d" imply 32-bit and 64-bit respectively reference. Notepad++ or a hex editor will more accurately show there are actually 2 null characters between the "PE" and the other character as shown in the image below copied from Notepad++.

Screenshots of dlls opened in Notepad++

Unfortunately some of my directories contain hundreds of dlls so it's not practical to open them one at a time with Notepad or any other utility. There are, however powerful "grep-capable" file search utilities that can search a directory for files containing a specified search string. Moreover, some of these can do regular expression (regex) searches. Since I know the unique strings that differentiate 32-bit and 64bit dlls (shown above), such a file search utility should be able to quickly inventory the types of dlls in any directory. The best such file search utility in my opinion is grepWin which can be downloaded and installed for free.

My first attempt was the regex search string ".PE("\x00")*" which can be broken down as follows.

break downs of regex string ".PE("\x00")*"

The image below shows results of a search done using grepWin and the search string ".PE("\x00")*" for a specified directory that had 276 dll files in it. It shows that 276 of the 276 dlls found contained "PE" followed by multiple null characters. It also shows that actually thousands of matches were found. This is because the regex search continued after the first match and found many more matches in larger files that inevitably appear "randomly".

enter image description here

The table below shows search results from regex strings "PE.{2}L" and "PE.{2}d" proposed by O-O-O. These search strings find all the files but unfortunately some of the dll files are being counted twice because the sum of the 32-bit and 64-bit dlls exceeds the total number of dll files in the directory.

files found containing "PE.{2}L" and "PE.{2}d"

The screen shots below of the search results using "PE.{2}L" and "PE.{2}d" show that the matches exceed the number of files found meaning that the regex searches are going beyond the first match.

grepWin details using "PE.{2}L" and "PE.{2}d"

So I only need to know how to modify these regex search strings to stop searching 3 characters after the first "PE" is found. I know this can be done using the ".*?" modifiers but I haven't been able to get it to work. Here is my question.

• How can these search strings be modified to stop reading 3 characters after the first "PE" is found?

Any regex search strings can be verified by searching any directory of dlls with grepWin. To be correct, the search strings must produce an equal number of matches as files unlike the examples shown above. This will verify that the search stopped after the first match was found.


Solution

  • This can't be true:

    From a technical point of view

    We can further restrict a regular expression to not overly match too many false positives and to not ignore things we should also check (it helps knowing how a Portable Executable's layout looks like):

    The better regex

    If your target software crashes (although it praises its regex support, like grepWin) then

    Explanation:

    1. starting at the begin of the file (actually only the start of a "line")

    2. characters M and Z (Mark Zbikowski)

    3. any character for at least 62 times, but at max 2046 times (a text editor like Notepad++ might complain that our regex would be too complex, that's why we also define a maximum)

    4. characters P and E (Portable Executable)

    5. bytes 00 00

    6. the CPU architecture:

      • bytes \x64\x86 for 64 bit (AMD), or
      • bytes \x4c\x01 for 32 bit (Intel 386 or later).

      Don't rely on opticals only (d and L), because then you ignore half of the value and just risk more false positives).

    7. any character for exactly 18 times

    8. byte 0b

    9. either byte 01 or 02

    Successfully tested

    Notepad++ regex matching DOS and NT headers

    The big advantage here is that this regex most likely only matches once instead of multiple times, especially in DLLs. However, since executables have no "end" mark they can carry any format of data afterwards. Unbound to the intention (good = self extracting archives, bad = viruses) there's hardly a way to exclude those - if we're lucky our ^ helps us.