diff --git a/bin/up2k.sh b/bin/up2k.sh new file mode 100755 index 00000000..b5b1920c --- /dev/null +++ b/bin/up2k.sh @@ -0,0 +1,222 @@ +#!/bin/bash +set -e + +# upload $datalen worth of generated data with a random filename, +# using a single connection for all chunks + + +## +## config + +datalen=$((2*1024*1024*1024)) +target=127.0.0.1 +posturl=/inc +passwd=wark + + +## +## derived/generated + +salt=$(head -c 21 /dev/urandom | base64 -w0 | tr '+/' '-_') +datalen=$((datalen-3)) + +openssl enc -aes-256-ctr -pass pass:$salt -nosalt < /dev/zero 2>/dev/null | +head -c 524287 > /dev/shm/$salt.bin #2147483647 + + +## +## functions + +# produce a stream of data +gendata() { + while true; do + cat /dev/shm/$salt.bin{,,,,,,,,,,,,,,,,,,} + done +} + +# pipe a chunk, get the base64 checksum +gethash() { + printf $( + sha512sum | cut -c-64 | + sed -r 's/ .*//;s/(..)/\\x\1/g' + ) | + base64 -w0 | cut -c-43 | + tr '+/' '-_' +} + +# division except ceil() instead of floor() +ceildiv() { + local num=$1 + local div=$2 + echo $(((num+div-1)/div)) +} + +# provide filesize, get correct chunksize +getchunksize() { + local filesize=$1 + local chunksize=$((1024 * 1024)) + local stepsize=$((512 * 1024)) + while true; do + for mul in 1 2; do + local nchunks=$(ceildiv filesize chunksize) + + [ $nchunks -le 256 ] || + [ $chunksize -ge $((32 * 1024 * 1024)) ] && { + echo $chunksize + return + } + + chunksize=$((chunksize+stepsize)) + stepsize=$((stepsize*mul)) + done + done +} + +# pipe data + provide full length, get all chunk checksums +gethashes() { + local datalen=$1 + local chunksize=$(getchunksize $datalen) + local nchunks=$(ceildiv $datalen $chunksize) + + echo >&2 + while [ $nchunks -gt 0 ] + do + head -c $chunksize | gethash + nchunks=$((nchunks-1)) + printf '\033[Ahash %s \n' $nchunks >&2 + done +} + +# pipe handshake response, get wark +getwark() { + awk '/"wark": ?"/ {sub(/.*"wark": ?"/,"");sub(/".*/,"");print}' +} + + +## +## create handshake json + +chunksize=$(getchunksize $datalen) + +printf '{"name":"%s.bin","size":%d,"hash":[' $salt $datalen >/dev/shm/$salt.hs + +printf '\033[33m' +gendata | +head -c $datalen | +gethashes $datalen > /dev/shm/$salt.hl +printf '\033[0m' + +IFS=$'\n' GLOBIGNORE='*' command eval 'HASHES=($(cat "/dev/shm/$salt.hl"))' + +awk '{printf "%s\"%s\"", v, $0; v=","}' /dev/shm/$salt.hl >> /dev/shm/$salt.hs + +printf ']}' >> /dev/shm/$salt.hs + + +## +## post handshake + +printf '\033[36m' + +#curl "http://$target:1234$posturl/handshake.php" -H "Content-Type: text/plain;charset=UTF-8" -H "Cookie: cppwd=$passwd" --data "$(cat "/dev/shm/$salt.hs")" | tee /dev/shm/$salt.res + +{ + { + cat < %s %s\n' $remains $postlen >&2 + + remains=$((remains-postlen)) + + { + cat <