Busybox for Windows httpd

Recently, I've been using Busybox for Windows a lot more and the latest version includes the ability to use the httpd applet with CGI integration!

Busybox for Windows
Documentation on the httpd applet.

This meant that when I was faced with trying to get data off an old iPad with a web browser with broken HTTPS, no other internet connectivity and no way of installing apps, I could use the httpd applet to copy data off it (if only in plain text).

I created an HTML file on my PC containing the following code:

<!DOCTYPE html>
<html>
<head>
<title>Data Extraction</title>
</head>
<body>
<h1>Test</h1>
<form method=POST action="/cgi-bin/write.sh" type="multipart/formdata">
File Name: <input type="text" name="name" /><br />
Contents:<br />
<textarea name="contents" rows="5" 
   cols="60"></textarea><br />
<input type="submit" value="Send">
</form>
</body>
</html>

This has a textbox for the filename (called "name") and one for the contents of the file (called "contents"). I simply had to copy the text of each file to the webpage individually on running this.

After creating the HTML page, I then created a subdirectory called "cgi-bin" (as required by the httpd applet) and placed in it the following as "write.sh":

#!/bin/ash
if test ! -z "${CONTENT_LENGTH}"; then
  read -n ${CONTENT_LENGTH} -d '\x00' data
fi
printf "Content-type: text/plain\r\n\r\n"
echo "Read ${CONTENT_LENGTH} byte(s)"
if test ! -z "$data"; then
  echo "Data read: $data"
  if echo "$data"| grep 'contents\='; then
    echo "$data"|sed -e 's/^.*contents\=//'|sed -e 's/\&.*$//' > da.txt
  else
    echo "No Data Submitted!"
	exit 1
  fi
  httpd -d "$(cat da.txt)" > dd.txt
  if test -z "$(cat dd.txt)"; then
    echo "No Data Submitted!"
	exit 2
  fi
  if echo "$data"| grep 'name\='; then
    n=$(echo "$data"|sed -e 's/^.*name\=//'|sed -e 's/\&.*$//')
  else
    n=''
  fi
  mkdir -p "../Documents/"
  if test ! -z "$n"; then
    nd=$(httpd -d "$n"|sed -e 's/\.\./_/g'|sed -e 's#[\\\/\:\?\*\"\<\>\|]#_#g')
	nc="$(eval "ls $(echo "../Documents/${nd}*.txt"|sed -e 's/ /\\ /g')" 2>/dev/null|wc -l)"
	fau=''
	echo "Name count: $nc"
	if test "$nc" != '0' || test -f "../Documents/${nd}.txt"; then
	  if cmp -s dd.txt "../Documents/${nd}.txt"; then
	    echo "File already uploaded!"
		fau='1'
	  else
	    nd="${nd}-${nc}"
	  fi
	fi
	if test -z "$fau"; then
      cp dd.txt "../Documents/${nd}.txt"
	  echo "File saved as ${nd}.txt"
	fi
  else
    nc="$(eval "ls $(echo "../Documents/doc*.txt"|sed -e 's/ /\\ /g')" 2>/dev/null|wc -l)"
	nc="$(dc -e"$nc 1 + p")"
	echo "Document Number: $nc"
	cp dd.txt "../Documents/doc${nc}.txt"
	echo "File saved as doc${nc}.txt"
  fi
fi
echo "Using Method ${REQUEST_METHOD}"
echo "From agent \"${HTTP_USER_AGENT}\""

This writes the text in the "contents" box to a file named as whatever is in the "name" box, appended with ".txt", saved in a "Documents" subdirectory, appending a number if a file with that name already exists and not copying anything if the file with that name is identical. If the "name" box is empty, "doc" followed by a number and then ".txt" is used instead. Lots of debug information is given to the subitter of the data as I needed to be sure things were going through correctly. Obviously, this code would be dangerous to run outside a secure network environment, but here it did the job fine.

Bare in mind, this script is very much a bodge, so do review the code and adjust as needed if you wish to use any of this.