Skip to content

Instantly share code, notes, and snippets.

View postmodern's full-sized avatar
🚧
working on Phase 2 (of Ronin)

Postmodern postmodern

🚧
working on Phase 2 (of Ronin)
View GitHub Profile
@postmodern
postmodern / if_else_vs_case_in_benchmark.rb
Last active June 13, 2024 00:32
Benchmarking if / else with kind_of? against case / in to match two values based on their classes. if / else + kind_of? appears to be 4x faster.
ruby 3.2.4 (2024-04-23 revision af471c0e01) [x86_64-linux]
Warming up --------------------------------------
if / else 888.000 i/100ms
case / in 259.000 i/100ms
Calculating -------------------------------------
if / else 8.856k (± 0.9%) i/s - 44.400k in 5.013974s
case / in 2.560k (± 3.0%) i/s - 12.950k in 5.064976s
@postmodern
postmodern / async_port_scanner.rb
Created March 8, 2024 05:22
Proof of concept connect() port scanner using Ruby's async gems.
require 'bundler/inline'
gemfile do
gem 'async-io', '~> 1.30'
end
require 'async'
require 'async/queue'
require 'async/io'
@postmodern
postmodern / file2bmp.sh
Created December 8, 2023 07:37
WIP code to convert a file into a BMP image, which can be screenshotted, then decoded in order to exfiltrate data from TryHackMe's network-less VMs
#!/usr/bin/env bash
## bmplib.sh v1.0
#
# Make your own BMP from scratch, no external dependencies.
# Useful in BitBar plugins.
#
# pixels=() # set pixels to empty or with background of same size
# # as will be declared in init_bmp
# curcol=(bb 66 44 aa) # set current color, BGRA, hex (%02x)
@postmodern
postmodern / benchmark.rb
Created September 18, 2023 04:04
Micro-benchmark to test `||=` and `[]=` vs. explicit initialize xor `[]=`
#!/usr/bin/env ruby
require 'benchmark'
keys = ('aa'..'zz').map(&:to_sym)
Benchmark.bm do |b|
n = 10_000
b.report('lazy initialize') do
@postmodern
postmodern / all_typoed_tlds.rb
Last active July 17, 2023 21:59
All TLDs and their valid typoed TLD counterparts
# gem install ronin-support
require 'ronin/support/network/tld'
def omit_each_char(tld)
return enum_for(__method__,tld) unless block_given?
tld.length.times do |i|
new_tld = tld.dup
new_tld[i] = ''
yield new_tld
@postmodern
postmodern / benchmark.rb
Created February 23, 2023 02:23
Micro-benchmark for `value != nil` vs. `!value.nil?`
#!/usr/bin/env ruby
require 'benchmark'
Benchmark.bm do |b|
n = 1_000_000
value1 = 1
value2 = nil
@postmodern
postmodern / benchmark.rb
Last active February 14, 2023 21:22
Ruby micro-benchmark for static vs. dynamic dispatch
#!/usr/bin/env ruby
require 'benchmark'
class StaticDispatch
def dispatch
if rand > 0.5
method1
else
@postmodern
postmodern / Gemfile
Created October 17, 2022 21:21
Testing infinite responses with HEAD requests
source 'https://rubygems.org/'
gem 'sinatra', '~> 2.0'
gem 'webrick'
gem 'thin'
gem 'puma'
gem 'unicorn'
@postmodern
postmodern / array_addition_benchmark.rb
Created August 1, 2022 00:45
Micro-benchmark to test different ways of adding Arrays
#!/usr/bin/env ruby
require 'benchmark'
Benchmark.bm do |b|
n = 1_000_000
array1 = [1,2,3,4,5,6]
array2 = [7,8,9,10,11,12,13,14]
array3 = [15,16,17,18,19,20]
@postmodern
postmodern / test.rb
Last active March 14, 2022 09:00
Discovered a weird ruby module constant scoping issue today.
TYPES: {:scope=>Namespace}
self::TYPES: {:scope=>Namespace::Mixin}