#!/usr/bin/python

import time
import os
import subprocess
import sys
from subprocess import call
from subprocess import Popen

#setting
#------------------------
filename = sys.argv[1].split("/")[-1]

original_image_dir="/home/root/VMimage/winxp2-malware.raw"
cp_image_dir="/home/root/VMimage/winxp2-malware00.raw"
mount_dir="/media/sendtoVM"
guest_desktop_dir="/Documents and Settings/All Users/Desktop/malware.exe"
user_pwd=""
# telnet_client_dir="temutelnetclient.py"
telnet_client_dir="/home/root/automation/script/temutelnetclient.py"
temu_dir="/home/root/temu-ants/tracecap/temu"
LD_LIBRARY_PATH_DEFAULT = "/home/root/temu-ants/tracecap"  #should be pointed to tracecap folder
#------------------------

#functions
#------------------------
def print_usage():
  print "your parameter does not met requirement!"
  print "usage: python winmalware.py (malwaredir) (resultdir)"

def copy_os_image():
  print "copying new image"
  call (["cp",original_image_dir,cp_image_dir])
  print "image copied"

def send_malware(filepath):
  print "sending malware into image..."
  call(["cp",filepath,mount_dir+guest_desktop_dir])
  time.sleep(10)
  print "malware sent to image"

def mount_disk():
  print "mounting disk image..."
  #original
  #p = Popen(["sudo" , "-S", "mount", "-o" , "loop,offset=32256" , cp_image_dir , mount_dir , "-t","ntfs"],stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  #p.communicate(user_pwd+'\n')
  #for container ( run as root )
  p = Popen(["mount", "-o" , "loop,offset=32256" , cp_image_dir , mount_dir , "-t","ntfs"])
  time.sleep(3)
  print 'disk mounted'

def umount_disk():
   print 'umounting image...'
   #original
   #p = Popen (["sudo","-S", "umount", mount_dir],stdin=subprocess.PIPE,     stdout=subprocess.PIPE)
   #p.communicate(user_pwd+'\n')
   #for container ( run as root )
   p = Popen (["umount", mount_dir])
   print 'image umounted'

def start_telnet_for_temu(filepath,filename):
  print 'starting telnet client to control temu...'
  Popen (["python",telnet_client_dir,filepath,filename])

def start_temu():
  print 'starting temu!!!'
  #Popen ([temu_dir,"-monitor", "telnet:localhost:4444,server,nowait","-snapshot", "-hda", cp_image_dir, "-k", "en-us", "-net", "nic", "-net","user"])
  call ([temu_dir,"-monitor","telnet:localhost:4444,server,nowait","-snapshot", "-hda",cp_image_dir,"-k", "en-us", "-net", "nic" ,"-net", "user", "-vnc",":0"])

def check_LD():
  print 'checking export for LD_LIBRARY'
  if not "LD_LIBRARY_PATH" in os.environ:
      #print 'LD_LIBRARY_PATH not set, "export LD_LIBRARY_PATH=' + LD_LIBRARY_PATH_DEFAULT+ '" mannually, thanks'
      os.environ["LD_LIBRARY_PATH"] = LD_LIBRARY_PATH_DEFAULT
  print 'path set at: ' + os.getenv("LD_LIBRARY_PATH")


#------------------------

#main
#------------------------
##argument: filedir(full) resultdir(full)
if len(sys.argv) != 3:
  print_usage()
  sys.exit()
if not os.path.isfile(cp_image_dir):
  copy_os_image()
mount_disk()
send_malware(sys.argv[1])   #file dir
umount_disk()
check_LD()
start_telnet_for_temu(sys.argv[2],filename)
start_temu()
sys.exit()
#------------------------
