Sunday, July 12, 2015

Transfering file to remote machine using tcpdump and dd

Sometimes you need to transfer a file to a remote machine and do not have a file transfer tool available at the remote machine or may be restricted due to firewall rules.
If the remote machine has tcpdump and dd available then you can transfer the file following the process described below.

What do you need  on sender machine?
split and netcat

What do you need on receiver machine?
tcpdump , dd and one reachable port. Most like the dhcp port (67) is going to be open since there has to be a way for the remote to get IP :)

Steps:
On remote machine
1.a:
Create a shell script file like this:
cat > extract.sh <<EOF
# On the receving machine capture tcpdump file using
# tcpdump -i doc0 -w tcpdump.bin port 67

FILE_SIZE=$1
START=82
PACKET_SIZE=1000
FRAME_HEADER=58
BYTES_EXTRACTED=0
while [ $BYTES_EXTRACTED -lt $FILE_SIZE ]
do
        let partName=10000000+$START
        dd if=tcpdump.bin of=$partName.part bs=1 count=1000 skip=$START
        let START=$START+$PACKET_SIZE+$FRAME_HEADER
        let BYTES_EXTRACTED=$BYTES_EXTRACTED+$PACKET_SIZE
done

rm outfile.bin
for name in `ls -1 *.part`
do
        cat $name >> $2
done

EOF


1.b :
Launch tcpdump in a folder where you have write access (typically /dev/ is writable so you can create a folder called /dev/worktmp and launch tcpdump from there).
#mkdir /dev/worktmp
#cd  /dev/worktmp
tcpdump -i doc0 -w tcpdump.bin port 67

Steps on sender machine:
2.a:
Create a file like this:
cat  > sendAFile.sh <<EOF
#On the sending machine
#send the file using this
INFILE=$1
DEST=$2
rm x*
split -b 1000 $INFILE
for name in `ls x*`
do
        cat $name |  nc -w 1 -u $DEST  67 &
        sleep 1 ;
        pkill nc;
        sleep 1;
done
md5sum $INFILE
ls -l $INFILE

EOF

2.b :
Send the file (say the file name is strace) to the remote machine (10.2.2.3) using the script create above:
#bash sendAFile.sh strace 10.2.2.3

Steps on remote machine:
1.c:
Create the final file. The first argument is the file size and second argument is name of the file:
sh extract.sh 45654 strace

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.