Improved, refactored, and new version of the previously leaked source code of the Babuk ransomware by VXUG.
-
In the
find_files_networkfunction:- The
netResparameter is expected to be a valid pointer toNETRESOURCEW, but there is no validation for a null pointer. - The condition
if (netRes = (LPNETRESOURCEW)_halloc(cbBuffer))contains an assignment (=) instead of a comparison (==), which may lead to unintended behavior. - There is a potential buffer overflow issue when allocating memory for
netResusing_halloc. The size ofcbBuffer(16KB) is used, but there is no guarantee that the allocated memory can hold that amount of data.
- The
-
In the
enum_sharesfunction:- The
addrparameter is expected to be a valid pointer to a null-terminated wide string, but there is no validation for a null pointer. - The
NetShareEnumfunction is called with a cast to(LPWSTR), which assumes that theaddrparameter is modifiable, but this may not always be the case. - The
lstrlenWfunction is used to determine the length of thep->shi1_netnamestring, but there is no validation for a null pointer or uninitializedppointer.
- The
-
In the
_processDrivefunction:- The
driveLetterparameter is assumed to be a valid drive letter, but there is no validation for incorrect or unsupported values. - There is a potential buffer overflow issue when allocating memory for
driveBufferusing_halloc. The buffer size should be 6 characters ("\\\\?\\X:"), but it is allocated with 7 characters, which may lead to unintended behavior. - The condition
if (driveType != DRIVE_REMOTE)suggests that only local drives are processed, but there is no check for other drive types like network drives or removable drives.
- The
-
In both
find_files_recursiveandfind_paths_recursive, a fixed-size bufferlocalDirof 32768WCHARelements is used for constructing file and directory paths. If the path length exceeds the buffer size, it can lead to a buffer overflow. Proper bounds checking should be implemented to ensure the buffer is not overflowed. -
Unchecked Heap Allocation: The functions
_hallocand_hfreeare used for memory allocation and deallocation. However, there is no explicit check for successful memory allocation. If the allocation fails and returns NULL, the code does not handle this situation. This can lead to undefined behavior if subsequent operations assume the memory is valid. It's important to check the return value of_hallocand handle allocation failures appropriately. -
Incorrect String Length Calculation: In
find_files_recursive, theWriteFilefunction is called with the length of theransom_notestring calculated usinglstrlenA. However,lstrlenAreturns the length in bytes, not the number of characters for wide strings. This can lead to incorrect file writing if the string contains non-ASCII characters. The appropriate function to use for wide strings islstrlenWorwcslen. -
Misuse of
lstrcmpiW: In several places, the functionlstrcmpiWis used for case-insensitive string comparison. However, it is used incorrectly. The comparison is made betweenfd.cFileName + iand the extension strings, but it should be comparing the extension string with the result oflstrcmpiW. The correct usage would belstrcmpiW(fd.cFileName + i, L".exe") == 0,lstrcmpiW(fd.cFileName + i, L".dll") == 0, etc. -
Potential Infinite Loop: In the
lilBabukfunction, when popping items from theque_pqueue, the loop conditionwhile (TRUE)is used without an explicit termination condition. This can result in an infinite loop if there is an error or if the queue does not reach a state where no more items are available. -
Unclear Error Handling: Some error conditions are handled by outputting debug information, but the impact on program flow is not clear. For example, in
find_files_recursiveandfind_paths_recursive, whenFindFirstFileWfails, debug information is printed, but the behavior afterward is not specified. It is important to handle errors consistently and provide appropriate feedback or terminate execution if necessary. -
Lack of Error Checking: Several API calls, such as
CreateFileW,WriteFile,FindFirstFileW, etc., are not checked for errors. It is crucial to verify the return values of these functions and handle any potential errors that may occur.