Natas - Annex

2 November 2017

Converting from hexadecimal representation to binary

$ echo '3d3d516343746d4d6d6c315669563362' | sed -r 's/.{2}/\\x&/g'
\x3d\x3d\x51\x63\x43\x74\x6d\x4d\x6d\x6c\x31\x56\x69\x56\x33\x62
$ echo '3d3d516343746d4d6d6c315669563362' | sed -r 's/.{2}/\\x&/g' | xxd -b | cut -d ' ' -f 2-8 | xargs echo -n| sed 's/\ //g'
0101110001111000001100110110010001011100011110000011001101100100010111000111100000110101001100010101110001111000001101100011001101011100011110000011010000110011010111000111100000110111001101000101110001111000001101100110010001011100011110000011010001100100010111000111100000110110011001000101110001111000001101100110001101011100011110000011001100110001010111000111100000110101001101100101110001111000001101100011100101011100011110000011010100110110010111000111100000110011001100110101110001111000001101100011001000001010

strrev

$ echo "hello" | rev
olleh
sammy@server:~$ echo '3d3d516343746d4d6d6c315669563362' | sed -r 's/.{2}/\\x&/g' | xxd -b | cut -d ' ' -f 2-8 | xargs echo -n| sed 's/\ //g'| rev
0101000001001100011011000001111000111010110011001100110000011110001110100110110010101100000111100011101010011100011011000001111000111010011011001010110000011110001110101000110011001100000111100011101011000110011011000001111000111010001001100110110000011110001110100010011000101100000111100011101000100110011011000001111000111010001011001110110000011110001110101100110000101100000111100011101011001100011011000001111000111010100011001010110000011110001110100010011011001100000111100011101000100110110011000001111000111010

Converting binary to hexadecimal representation

Method 1: printf

sammy@server:~$ printf '%x\n' "$((2#111))"
7
sammy@server:~$ echo '3d3d516343746d4d6d6c315669563362' | sed -r 's/.{2}/\\x&/g' | xxd -b | cut -d ' ' -f 2-8 | xargs echo -n| sed 's/\ //g'| rev | awk '{print "\"$((2#"$0"))\""}'
"$((2#0101000001001100011011000001111000111010110011001100110000011110001110100110110010101100000111100011101010011100011011000001111000111010011011001010110000011110001110101000110011001100000111100011101011000110011011000001111000111010001001100110110000011110001110100010011000101100000111100011101000100110011011000001111000111010001011001110110000011110001110101100110000101100000111100011101011001100011011000001111000111010100011001010110000011110001110100010011011001100000111100011101000100110110011000001111000111010))"

Method 2: bc

echo '3d3d516343746d4d6d6c315669563362' | sed -r 's/.{2}/\\x&/g' | xxd -b | cut -d ' ' -f 2-8 | xargs echo -n| sed 's/\ //g'| rev | awk '{print "obase=16;ibase=2;"$0}' | bc
504C6C1E3ACCCC1E3A6CAC1E3A9C6C1E3A6CAC1E3A8CCC1E3AC66C1E3A266C1E3A26\
2C1E3A266C1E3A2CEC1E3ACC2C1E3ACC6C1E3A8CAC1E3A26CC1E3A26CC1E3A

Writing in hex to a file

Write ABCD as the first bytes of a file:

sammy@server:~$ echo "0 41424344" | xxd -r - data.bin
sammy@server:~$ cat data.bin
ABCD
sammy@server:~$ xxd data.bin
00000000: 4142 4344                                ABCD
sammy@server:~$ ls -l data.bin
-rw-rw-r-- 1 sammy sammy 4 oct.  23 13:16 data.bin

xor

sammy@server:~$ printf '0x%X\n' $((0x0 ^ 0xFFFFFFFFF))
0xFFFFFFFFF