Wednesday, April 15, 2015

Transfer a file to a remote machine using console access

How to transfer a file to a remote machine using console access (telnet etc) when you have no file transfer tools?

Sometimes you are stuck on machine that is badly locked due to "security" reasons and does not contain any tools like wget, ftp, tftp. But you want to transfer a file on that machine. What to do now?
Assuming that the remote machine lets you some kind of console access via a tools that lets you copy paste (like putty) and also has these 3 tools:
  1. dd
  2. awk
  3. sh/bash/ash etc.

We are assuming you intend to transfer netcat.bin to remote machine.

Step 1) On remote machine, create a working folder at some volume that is writable. Even if everything else is write protected /tmp and /dev are generally writable.

Step 2) Change the working folder to the newly created folder.

Step 3) Create a file called hex2bin.dat (Yes, I am not naming it a .sh file you can name it if you want) on the remote machine. Contents of the file are as shown below.
#usage:
# cat data.txt | sh hex2bin.txt
#where data.txt is like this without # signs:
#cat data.txt
#0x64
#0x0a
#0x65
#0x0d
#0x66
#EEEE
#
# You also nee a file called zero.file which can be created like this:
# dd if=/dev/zero bs=1 count=1 of=zero.file

OFILE=$1
rm $OFILE
while [ 1 -eq 1 ]
do
        read aLine
        if [ x$aLine = "xEEEE" ]
        then
                exit
        else
                if [ x$aLine = x0x00 ]
                then
                        cat zero.file>>$OFILE
                else
                        echo $aLine| awk '{printf "%c", $1;}'>>$OFILE
                fi
        fi
done

Step 4) Create a file named zero.file by using the command shown below:
dd if=/dev/zero bs=1 count=1 of=zero.file

Step 5) Now on you local machine create a file named data.txt as shown below:
xxd -c 1 netcat.bin | awk '{print "0x" $2;}' > data.txt

Step 6) Now cat data.txt and take all the contents on clipboard.

Step 7) On remote machine create the same data.txt as shown below:
cat > data.txt << EOL
<<<< Paste the clipboard contents here      >>>>
<<<< Now type "EEEE" here, without quotes   >>>>
<<<< Now type "EOL"  here, without quotes   >>>>


Step 8) Now you should have a file named data.txt on remote machine. Do grep -vn "0x" data.txt and you should not see any empty lines. If you see any empty lines then you need to correct the empty lines using a text editor on remote machine by comparing contents of your local data.txt.

Step 9) Now just use following:
cat data.txt | sh hex2bin.txt netcat.bin


If you have a checksum tools on the remote machine (like md5sum or cksum) then you may want to do checksum comparison to make sure that file is good.