Skip to content

Instantly share code, notes, and snippets.

@DownWithUp
Created October 26, 2018 13:48
Show Gist options
  • Save DownWithUp/59c15f5b93ac35423fdc140eb34ac244 to your computer and use it in GitHub Desktop.
Save DownWithUp/59c15f5b93ac35423fdc140eb34ac244 to your computer and use it in GitHub Desktop.
Use an IOCTL to create a beep from Beep.sys
#include <Windows.h>
#include <stdio.h>
#include <winternl.h>
typedef struct _BEEP_SETTINGS {
ULONG ulFrequency;
ULONG ulDuration;
} BEEP_SETTINGS;
void main() {
// Prep string
UNICODE_STRING uszDeviceName;
OBJECT_ATTRIBUTES BeepObjectAttributes;
RtlInitUnicodeString(&uszDeviceName, L"\\Device\\Beep");
// Prep object
InitializeObjectAttributes(&BeepObjectAttributes, &uszDeviceName, 0, NULL, NULL);
// Get a handle on beep.sys
PIO_STATUS_BLOCK pOutStatus = 0;
HANDLE hDriver;
IO_STATUS_BLOCK IoStatus;
NTSTATUS ntOut = NtCreateFile(&hDriver, 0x3, &BeepObjectAttributes, &IoStatus, NULL, 0, 0x3, 0x3, 0, 0, 0);
if (ntOut == 0x0) {
DWORD dwIOCTL_BEEP = CTL_CODE(FILE_DEVICE_BEEP, 0, METHOD_BUFFERED, FILE_ANY_ACCESS);
BEEP_SETTINGS BeepSettings;
BeepSettings.ulDuration = 10000;
BeepSettings.ulFrequency = 500;
DWORD dwReturned = 0;
DeviceIoControl(hDriver, dwIOCTL_BEEP, &BeepSettings, sizeof(BEEP_SETTINGS), NULL, 0, dwReturned, NULL);
} // You should add an else statement
ExitProcess(0);
}
// I know this is super useful, so you're welcome!
@im-tesla
Copy link

im-tesla commented Feb 7, 2024

any idea how can we hook BeepOpen using another kernel driver?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment