Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
marcogrcr / Non-Interactive-Process-Windows-Service.cs
Last active September 3, 2019 22:49
How to run a non-interactive process as another user in a Windows Service using C#? [proof-of-concept]
/**
* This application converts a text message to uppercase by spawning a child process as another user and saves it to a file.
*
* Usage: app.exe MESSAGE FILE_PATH
* or
* Usage: app.exe DOMAIN USERNAME PASSWORD MESSAGE FILE_PATH
*
*
* Setup instructions:
*
@marcogrcr
marcogrcr / sigv4_using_http_client.py
Last active May 23, 2024 10:48
Send request with SigV4 in python using boto3
from boto3.session import Session
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
from botocore.credentials import Credentials
from http.client import HTTPConnection, HTTPSConnection
import json
import os
from urllib.parse import urlparse
def sigv4_request(
@marcogrcr
marcogrcr / pointer-binary-heap.js
Last active December 22, 2020 05:49
Pointer binary heap: A JavaScript binary heap implementation with pointers instead of arrays
/** Represents a node from a binary heap. */
class BinaryHeapNode {
/**
* Initializes a new instace of the `BinaryHeapNode` class.
* @param {number} value
* @param {*} ref
*/
constructor(value, ref) {
this.value = value;
this.ref = ref;
@marcogrcr
marcogrcr / 1-stateless-percentage.js
Created March 10, 2023 08:39
Stateless percentage: enable a feature for a percentage of customers without keeping state
const { createHash } = require("node:crypto");
/**
* Returns a random value betwen 0 and 99 associated with an input value that's
* quasi-evenly distributed.
*
* This function is useful for incrementally rolling out a feature to customers
* based on a percentage.
*
* @param value {string} The input value to derive the output from.
@marcogrcr
marcogrcr / event-target-polyfill.js
Created March 27, 2023 19:47
A simplified EventTarget polyfill for unit testing
/**
* Simplified `EventTarget` polyfill.
* @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
*/
class EventTarget {
/** @type {Map<string, { capture: boolean, listener: Function, once: boolean }[]>} */
_listenersMap = new Map();
/**
* @param {string} type
@marcogrcr
marcogrcr / Get-DiskBlockSize.ps1
Created July 14, 2023 02:33
Get disk allocation block size
# Source: https://www.alitajran.com/get-allocation-unit-size-powershell/
Get-CimInstance -ClassName Win32_Volume | Select-Object Name, FileSystem, Label, BlockSize | Sort-Object Name | Format-Table -AutoSize
@marcogrcr
marcogrcr / unexpected-type-checks.ts
Last active July 18, 2023 20:43
TypeScript unexpected type checks
// Even with the strictest settings, the TypeScript compiler may have looser type checking than what you would expect.
interface Data {
req1: number;
req2: number;
opt1?: number;
opt2?: number;
}
function logData(data: Data): void {
@marcogrcr
marcogrcr / AmbiguousFunctionalArgumentOverload.kt
Last active July 18, 2023 20:44
Ambiguous functional argument overload
import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Supplier
// Take these ambiguous overloads:
fun fn(consumerLike: (String) -> Unit) = consumerLike("").also { println("consumer-like") }
fun fn(consumer: Consumer<String>) = consumer.accept("").also { println("consumer") }
@marcogrcr
marcogrcr / aws-mfa-credentials.py
Created October 2, 2023 03:55
Invokes AWS STS GetSessionToken using the AWS CLI and stores the temporary credentials in ~/.aws/credentials
#!/usr/bin/env python
import json, os, subprocess
from argparse import ArgumentParser
from configparser import ConfigParser
from pathlib import Path
# 1. parse args
parser = ArgumentParser(
prog='mfa-aws-credentials',
description='Invokes AWS STS GetSessionToken using the AWS CLI and stores the temporary credentials in ~/.aws/credentials'
@marcogrcr
marcogrcr / child-src-data-proof-of-concept.mjs
Last active October 13, 2023 17:49
Content-Security-Policy: `child-src data:` for allowing `data:` URLs in workers but mitigate malicious `<iframe>`/`<frame>` elements
import { createServer } from "node:http";
const CSP =
"default-src 'none'; child-src data:; frame-src https://www.example.com; script-src 'nonce-abc'; worker-src data:";
const INDEX_PAGE = `
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>