What you want to do is prohibited, as it will be a gateway to viruses, registrars and other malwares.
However, if you want some code to work away from the OS, you should look at the system management mode (SMM).
SMM is the execution mode of x86 processors orthogonal to standard secure modes. SMM allows the BIOS to completely suspend the OS on all CPUs at the same time and enter SMM mode to perform some BIOS services. Switching to SMM mode is happening right now on your x86 machine when you read this Stackoverflow answer. It starts either:
- hardware : a dedicated system control interrupt line (SMI #), very similar to IRQ,
- : through I / O access to the location-specific logic of the motherboard (port 0xb2 is shared).
SMM services are called SMM handlers, and, for example, sensor values โโare very often retrieved using an SMM call to the SMM handler.
SMM handlers are configured during the DXE phase of initializing the UEFI firmware in SMRAM, an area reserved for SMM handlers. See the following diagram:

SMM drivers are sent by the SMM core during the DXE phase. So additional SMI handlers can be registered in the DXE phase. At the end of the DXE phase, when you no longer need to send SMM drivers, SMRAM will be blocked (as recommended). When the SMRAM is locked, no additional SMM drivers can be sent, so additional SMI handlers can be registered. For example, the SMM driver that registers the SMI handler cannot be loaded from the EFI shell or added as DriverOption in the UEFI boot manager.
source: tianocore
This means that the code of your SMM handler must be present in the BIOS image, which means rebuilding the BIOS with the addition of a handler. This is complicated, but there are tools that provide both a DXE environment and embedded SMM handler code in a PE executable, as well as other tools for adding a DXE driver to an existing BIOS sample. However, not all BIOS manufacturers are supported. This is risky if your flash chip is not in the socket and you can reprogram it from the outside.
But first you need to check if SMRAM is blocked on your system. If you're lucky, you can add your own SMM handler directly to SMRAM. It is not easy, but doable.
Note The SMM processors inside the BIOS are OS independent, so it starts even if the burglar installs a new operating system, which is what you need. However, being outside the OS has huge drawbacks: you need to implement a driver for the network interface in your SMM processor (only for polling, without interruption!) And wlan 802.11, DHCP and IP support for connecting to Wi-Fi, and your data will be redirected to an external host on the Internet. How do you determine your Wi-Fi SSID and password? Well, you can wait for the OS to initialize the network adapter for you, but you will need to save / restore the full state of the network host controller between calls. Not a small or simple project.