#!/usr/bin/env fish

set shellcode $argv[1]

set testFile "shellcode-test.c"

if test -n "$shellcode"

        rm $testFile

        echo 'Creating shellcode test file...'
        echo '================================='
        echo 'char shellcode[] = "'$shellcode'";' | tee --append $testFile
        echo 'int main(){' | tee --append $testFile
        echo '    void (*fp) (void);' | tee --append $testFile
        echo '    fp = (void *)shellcode;' | tee --append $testFile
        echo '    fp();' | tee --append $testFile
        echo '}' | tee --append $testFile
        echo '================================='

    echo compilling $testFile...
    gcc \
        -fno-stack-protector   \
        -z execstack  \
        $testFile \
        -o (echo $testFile | cut -d"." -f1).bin ; and echo 'done!'
    
else
    echo '[!]   Expected shellcode as argument'
    echo '[!]   Example:'
    echo '[!]       test-shellcode "\x90\x90"'
    exit
end