Android Twisted SpyCam

I have been playing around with Androids ASE(android scripting environment). ASE allows you to run scripts on your android powered device. You can use Python, PHP, Javascript, Pearl, Ruby and much more. To install ASE on your device you must either have root, allow third party apps install,, or know how to sideload apk’s using android’s SDK tools like adb.

You can find the apk along with install instructions and more info on ASE at their website.

Here is an example of using python as a remote spycam. I am using the Twisted Framework as the server, and it gets interesting because I embed the captured image from the phone as base64 data on the webpage Twisted is serving up. You can choose to install the Twisted libs when you install python on your device. This is a fairly new feature.

To learn more about twisted, you can visit their website.

import android, os, base64
from twisted.internet import protocol, reactor

droid = android.Android()
os.chdir('/sdcard/webserver/')

def imgb64():
  droid.cameraCapturePicture('/sdcard/webserver/latest.jpg', True)
  im = open("latest.jpg","rb")
  ime = base64.b64encode(im.read())
  im.close()
  os.remove("latest.jpg")
  return ime

class ServeImage(protocol.Protocol):
  def connectionMade(self):
    data = imgb64()
    self.transport.write("""<html><head><title>Android Camera</title></head><body><img src="data:image/jpg;base64,%s" alt="image"/>test</body></html>""" % data)
    self.transport.loseConnection()

class ServeImageFactory(protocol.ServerFactory):
  protocol = ServeImage

reactor.listenTCP(8080, ServeImageFactory())
reactor.run()