Skip to content

Instantly share code, notes, and snippets.

View samuelharmer's full-sized avatar

Samuel Harmer samuelharmer

View GitHub Profile
@samuelharmer
samuelharmer / Use-Impersonation.ps1
Created September 16, 2020 12:17 — forked from idavis/Use-Impersonation.ps1
Impersonate a user and execute a script block as that user
param( [ScriptBlock] $scriptBlock )
<#
.SYNOPSIS
Impersonates a user and executes a script block as that user. This is an interactive script
and a window will open in order to securely capture credentials.
.EXAMPLE
Use-Impersonation.ps1 {Get-ChildItem 'C:\' | Foreach { Write-Host $_.Name }}
This writes the contents of 'C:\' impersonating the user that is entered.
#>
@samuelharmer
samuelharmer / bash_history.sh
Last active September 16, 2020 08:56 — forked from ckabalan/best_bash_history.sh
The Best Bash History Settings Ever
# /etc/profile.d/bash_history.sh
# Save 5,000 lines of history in memory
HISTSIZE=10000
# Save 2,000,000 lines of history to disk (will have to grep ~/.bash_history for full listing)
HISTFILESIZE=2000000
# Append to history instead of overwrite
shopt -s histappend
# Ignore redundant or space commands
HISTCONTROL=ignoreboth
# Ignore more
#!/usr/bin/bash
which ansible >/dev/null 2>&1
if [ $? -ne 0 ];
then
echo "Installing Ansible..."
sleep 5
pushd .
cd ~
pacman -S libyaml-devel python2 tar libffi libffi-devel gcc pkg-config make openssl-devel openssh libcrypt-devel --noconfirm --needed
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
@samuelharmer
samuelharmer / reportlab_list_formatting.py
Created June 17, 2020 11:13
Custom formatting of reportlab lists
#!/usr/bin/env python3
import unicodedata
from pathlib import Path
from reportlab.platypus import SimpleDocTemplate, Paragraph, ListFlowable, Spacer
from reportlab.lib.styles import getSampleStyleSheet
output = Path.cwd() / Path(__file__).with_suffix(".pdf").name
doc = SimpleDocTemplate(str(output))
@samuelharmer
samuelharmer / one_flask.py
Created May 12, 2020 07:15 — forked from mikefromit/one_flask.py
a one file flask app for trying stuff
# THIS PROJECT IS AN EXAMPLE APP. SOME CODE MAY NOT BE ACTUALLY USEFUL
# FOR DEMONSTRATION PURPOSES ONLY
# YOUR MILEAGE MAY VARY
# Requirements are Flask, Flask-WTF, Flask-SQLAlchemy
import os
from flask import (Flask,
Blueprint,
redirect,
@samuelharmer
samuelharmer / server.py
Created February 13, 2020 11:08 — forked from mdonkers/server.py
Simple Python 3 HTTP server for logging all GET and POST requests
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
class S(BaseHTTPRequestHandler):
@samuelharmer
samuelharmer / Logging.md
Last active February 7, 2020 12:27
Software Development, Python, Logging

Logging

Logging can become as complicated as your imagination permits in Python. There are modules in the standard library that cater for all scenarios I have encountered and probably many more I have not.

The documentation has extensive sections on logging. The key materials (in rough order of reading) being:

@samuelharmer
samuelharmer / cleanup.cmd
Created November 14, 2019 14:56
Inception Data
:LoopStart
MOVE "a\Application Data" "b"
RMDIR /S /Q "a"
MOVE "b" "a"
GOTO LoopStart
@samuelharmer
samuelharmer / leaky.py
Last active August 6, 2018 06:30
Isolating file handle leaks
#!/usr/bin/env python
# Based on https://stackoverflow.com/a/23762447/594137
# Snippet prints not only what handles are open but where
# the REG (regular) handles are on disk, helping to narrow
# down what's causing them in your code. Won't work on
# Windows due to using `/proc`.
def fd_stats():
BEGIN TRANSACTION;
DROP TABLE IF EXISTS File;
CREATE TABLE File (hash_digest CHAR(32) PRIMARY KEY NOT NULL);
INSERT INTO File
(hash_digest) -- word which produces sha256 digest
VALUES
('288971671685b8da56623362c82e1ead68186c5150a35e3b35b5ef74cd7ceebc'), -- onion
('28897ea1b742813bdd54a7cad5f43af5b2b2198419184b33ce5f5c61eeea90aa'), -- eavesdrop
('2898a07b2cf23dda8530b14b6aa522e67b002886d170c02219acc3598fdb50f3'), -- virus