These determine the assumed/default size of instruction operands, and restricts which opcodes are available, and how they are used.
Modern operating systems, booted inside Real
mode,
#include <windows.h> | |
#include <subauth.h> | |
#include <stdio.h> | |
/* Windows structures */ | |
typedef struct _PEB_LDR_DATA { | |
BYTE Reserved1[8]; | |
PVOID Reserved2[3]; | |
LIST_ENTRY InMemoryOrderModuleList; | |
} PEB_LDR_DATA, *PPEB_LDR_DATA; |
// Copyright (C) 2022 Evan McBroom | |
// If you are using Visual Studio, you will need to disable the "Edit and Continue" feature. | |
// Prng based off of Parker Miller's | |
// "Multiplicative Linear Congruential Generator" | |
// https://en.wikipedia.org/wiki/Lehmer_random_number_generator | |
namespace mlcg { | |
constexpr uint32_t modulus() { | |
return 0x7fffffff; | |
} |
C++11 introduced the constexpr
keyword for defining a constant expression.
A constant expression is a variable or function that may be evaluated at compile time. This has many uses, including extending a switch statement to support full strings.
C++ only supports using an integer as the condition in a switch
statement and an integer that is known at compile time in a case
statement.
You can define a hash function and use it to convert a string to an integer to use in a switch
statement.
If you define that hash function as a constexpr
you can use it to convert a string literal to an integer to use in a case
statement as well.
I previously wrote about how to use macro metaprogramming to simplify using string literals in position independent code (PIC). The results are summarized in the below code snippet and the article can be read on GitHub.
void f() {
// Example 1: The Pic idiom for instantiating a string
char picString1[]{ 'a', 'b', 'c' };
A common programming idiom when writing position independent code (PIC) is to expand a string literal into its individual characters when instantiating a local variable.
void f() {
// Example 1: A normal instantiation with a string literal
char a[]{ "a long string" };
// Example 2: The Pic idiom for instantiating a string
Thank you to SpecterOps for supporting this research and to Duane and Matt for proofreading and editing! Crossposted on the SpecterOps Blog.
TLDR: You may use this header file for reliable compile time string encryption without needing any additional dependencies.
Programmers of DRM software, security products, or other sensitive code bases are commonly required to minimize the amount of human readable strings in binary output files. The goal of the minimization is to hinder others from reverse engineering their proprietary technology.
Common approaches that are taken to meet this requirement often add an additional maintenance burden to the developer and are prone to error. These approaches will be presented along with t
#include <Windows.h> | |
#include <atlstr.h> | |
DWORD GetPhysicalDriveSerialNumber(UINT nDriveNumber, CString& strSerialNumber) | |
{ | |
DWORD dwResult = NO_ERROR; | |
strSerialNumber.Empty(); | |
// Format physical drive path (may be '\\.\PhysicalDrive0', '\\.\PhysicalDrive1' and so on). | |
CString strDrivePath; |
#include <windows.h> | |
#include <memory> | |
#include <string> | |
//returns the serial number of the first physical drive in a std::string or an empty std::string in case of failure | |
//based on http://codexpert.ro/blog/2013/10/26/get-physical-drive-serial-number-part-1/ | |
std::string getFirstHddSerialNumber() { | |
//get a handle to the first physical drive | |
HANDLE h = CreateFileW(L"\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); | |
if(h == INVALID_HANDLE_VALUE) return {}; |
#include <string> | |
#include <vector> | |
#include <fstream> | |
#include <iostream> | |
#include <filesystem> | |
#include <Windows.h> | |
#include <winternl.h> | |
static_assert( sizeof( void* ) == 8 ); |