Checking Hard Drive Size

Created the Monday 11 March 2019. Updated 3 years, 6 months ago.

Not so many user machine are using less than 80Go, by requesting the drive size, malware can determined a virtual environment.


Technique Identifier

U1312


Code Snippets

Thomas Roccia

Description

Python snippet to detect the drive size with GetDiskFreeSpaceExW

import ctypes
import math

# Convert octets
def convert_size(size_bytes):
    if size_bytes == 0:
        return "0B"
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
    i = int(math.floor(math.log(size_bytes, 1024)))
    p = math.pow(1024, i)
    s = round(size_bytes / p, 2)
    return "%s %s" % (s, size_name[i])


# Get disk size with API GetDiskFreeSpaceExW
def disk_size(path):
    PULARGE_INTEGER = ctypes.POINTER(ctypes.c_ulonglong)
    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
    kernel32.GetDiskFreeSpaceExW.argtypes = (ctypes.c_wchar_p,) + (PULARGE_INTEGER,) * 3

    _, total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong(), ctypes.c_ulonglong()
    success = kernel32.GetDiskFreeSpaceExW(path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
    size = convert_size(total.value)
    print "The size of the disk is: ", size


if __name__ == '__main__':
    disk_size("C:/")

Additional Resources

External Links

Subscribe to our Newsletter


The information entered into this form is mandatory. It will be subjected to computer processing. It is processed by computer in order to support our users and readers. The recipients of the data will be : contact@unprotect.it.

According to the Data Protection Act of January 6th, 1978, you have at any time, a right of access to and rectification of all of your personal data. If you wish to exercise this right and gain access to your personal data, please write to Thomas Roccia at contact@unprotect.it.

You may also oppose, for legitimate reasons, the processing of your personal data.