Skip to content

Instantly share code, notes, and snippets.

View g-andrade's full-sized avatar

Guilherme Andrade g-andrade

  • Dash Games
  • Lisbon, Portugal
  • 04:07 (UTC +01:00)
View GitHub Profile
@g-andrade
g-andrade / tunnel_s2s.py
Created October 23, 2017 17:29
TCP server-to-server tunnel
import select
import socket
import sys
tcp_ip = '0.0.0.0'
tcp_port1 = int(sys.argv[1])
tcp_port2 = int(sys.argv[2])
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@g-andrade
g-andrade / create_zlib_bomb.erl
Created August 20, 2017 13:31
Create zlib payloads with obscene compression ratios
-module(create_zlib_bomb).
-export([do_it/1]).
-define(CHUNK_SZ, (128 * (1 bsl 10))).
do_it(Size) ->
Z = zlib:open(),
zlib:deflateInit(Z, 9, deflated, 15, 9, default),
Data = deflate_zeroes(Z, Size),
zlib:deflateEnd(Z),
@g-andrade
g-andrade / zlib_safe_uncompress.erl
Created August 10, 2017 06:43
Safe zlib inflation in Erlang
-spec zlib_safe_uncompress(binary(), non_neg_integer()) -> binary() | no_return().
zlib_safe_uncompress(CompressedData, UncompressedSize) ->
Z = zlib:open(),
zlib:inflateInit(Z),
zlib:setBufSize(Z, UncompressedSize),
case zlib:inflateChunk(Z, CompressedData) of
{more, _Chunk} ->
error({badarg, uncompressed_size_mismatch});
Data ->
zlib:inflateEnd(Z),
@g-andrade
g-andrade / decode_erlang_external_term_format.erl
Last active November 1, 2020 03:28
Decoding Erlang's external term format (incomplete)
-module(decode_erlang_external_term_format).
-export([binary_to_term/1]).
binary_to_term(<<131, 80, _UncompressedSize:32, CompressedData/binary>>) ->
Data = zlib:uncompress(CompressedData),
decode(Data);
binary_to_term(<<131, Data/binary>>) ->
decode(Data).
decode(<<82, _AtomCacheReferenceIndex, _Rest/binary>>) ->
@g-andrade
g-andrade / cesu8.erl
Last active April 9, 2022 19:28
Decoding CESU-8 in Erlang (into UTF-8).
decode_cesu8(B) ->
decode_cesu8(B, []).
% https://en.wikipedia.org/wiki/UTF-16
%
decode_surrogate_pairs([], Acc) ->
Acc;
decode_surrogate_pairs([Low, High | Rest], Acc) when High >= 16#D800, High =< 16#DBFF, Low >= 16#DC00, Low =< 16#DFFF ->
decode_surrogate_pairs(Rest, [16#10000 + (((High - 16#D800) bsl 10) bor (Low - 16#DC00)) | Acc]);
decode_surrogate_pairs([Other | Rest], Acc) ->