#!/bin/bash -x
#moony li
#2017-05-23, CopyLeft
#version 0.3 2017-08-22

assignedCurrentDir=$(pwd)
realCurrentDir=$(pwd)

scriptFolder=$realCurrentDir/scripts/


 

#https://github.com/dpnishant/appmon
appMonHttp=https://github.com/dpnishant/appmon.git
fridaHttp=https://github.com/frida/frida.git
nodeJSPath=/usr/local/bin/node

clone=0
cloneAppMon=0
cloneFridaAll=0
buildFrida=0

install=0

installDependency=0
hostPlatform="macos"
runAppMon=1
#guestPlatform="macos"
#fridaServerIP="127.0.0.1"

guestPlatform="ios"
fridaServerIP="192.168.3.21"

#analysis profile install
#targetAppName="Angry Birds Space"
#targetAppName="Messages"
#targetAppName="WindowServer"
#targetAppName="Radiation Island"
#targetAppName="WeChat"
##targetAppName="Mail"
#targetAppName="Safari"
#targetAppName="SpringBoard"
#targetAppName="Launchd"
#targetAppName="profiled"
#targetAppName="Settings"
targetAppName="Launchd"


#targetAppName="WeChat"
#targetAppName="Twitter"

#targetAppName="Launchd"
#targetAppName="MobileSafari"
#targetAppName="/Applications/MobileSafari.app/MobileSafari"
#targetAppName="MobileSafari"
#targetAppName="Safari"
outDir=$realCurrentDir/out/
reset
clear



if [ $clone -eq 1 ];then
   if [ $cloneAppMon -eq 1 ];then
        git clone $appMonHttp
   fi
   if [ $cloneFridaAll -eq 1 ];then
        git clone $fridaHttp
   fi
fi  

if [ $installDependency -eq 1 ];then
  #https://github.com/frida/frida/releases
  #pleae install frida-server for your target devices(e.g. Android, ios, OSX)
  #https://github.com/dpnishant/appmon/wiki/4.a-Setup-Host
  sudo easy_install pip
  #ignore six install error
  #https://iwww.me/540.html
  sudo -H pip install argparse frida flask termcolor dataset --upgrade --ignore-installed six
  sudo port selfupdate
  sudo port install nodejs6
  #or you should down load nodejs and install it manually
  brew install python3
  #sudo -H pip install argparse frida flask termcolor dataset --upgrade --ignore-installed six

fi

if [ $buildFrida -eq 1 ];then
  #https://github.com/frida/frida
  export MAC_CERTID=moony
  export IOS_CERTID=moony
  cd $assignedCurrentDir/frida
  
  export NODE=$nodeJSPath
  sudo killall taskgated
  make gadget-ios
  make core-ios
  make server-ios
  make gadget-ios
  make python-macos
  make node-macos


fi 

if [ $install -eq 1 ];then
    sudo pip install --ignore-installed six
    sudo pip install frida
    frida-ps -U
fi

if [ $runAppMon -eq 1 ];then
   cd $assignedCurrentDir/frida/appmon/
   clear
   #open http://127.0.0.1:5000/monitor/?app=$targetAppName
   #http://www.freebuf.com/sectool/120010.html
   #python ./appmon.py -p $guestPlatform -ls 1 
   
   #frida-ps   -a -i
   #frida-ps -U
   #for all
   if [ "$guestPlatform"x = "ios"x ]; then
      scriptPlatform="ios" 

      #run malware sample trace
      mkdir $outDir
      #rm -fr $outDir/*
      #Test OK by usb mode

      ios-deploy -d --no-wifi --justlaunch --noinstall -b /Users/lilang_wu/Documents/IOS/ios-sandbox-frida/ipa_repack/test_frida/Payload\ 2/AppleCLCD-memleak.app
      #ios-deploy -d --no-wifi --justlaunch --noinstall -b /Users/lilang_wu/Documents/IOS/ios-sandbox-frida/ipa_repack/C7DA097D-4D64-4160-9057-5D22DE54F0E3/Payload\ 2/PPJailbreakCarrier.app
      python  ./appmon.py -p $guestPlatform   -s $scriptFolder/all-trace/ -a "$targetAppName" -o $outDir

      #Test by network mode
      #python   ./appmon.py -p $guestPlatform  -H $fridaServerIP  -s $scriptFolder/all-trace/ -a "$targetAppName" -o $outDir

      #python   ./appmon.py -p $guestPlatform  -H $fridaServerIP  -s $scriptFolder/$scriptPlatform/ -a "$targetAppName" -o $outDir
      #python   ./appmon-0.1.py -p $guestPlatform   -s $scriptFolder/all/ -a "$targetAppName" -o $outDir

      read -p "Press any key to continue..."
      #run decision
      cd $assignedCurrentDir/scripts/all-decision/
      node ./iOS_decision.js


   fi

   if [ "$guestPlatform"x = "macos"x ]; then
      scriptPlatform="macos" 
      #python   ./appmon.py -p $guestPlatform   -s $scriptFolder/$scriptPlatform/ -a "$targetAppName" -o $outDir

      if [ "run"x = "run"x ]; then
        #$assignedCurrentDir/bin/frida-server-10.4.0-macos-x86_64 -l $fridaServerIP
        python   ./appmon.py -p $guestPlatform  -H $fridaServerIP  -s $scriptFolder/$scriptPlatform/ -a "$targetAppName" -o $outDir
      fi
   fi
   #python   ./appmon.py -p $guestPlatform   -s $scriptFolder/$scriptPlatform/"iOS"/ -a $targetAppName -o $outDir
   #python ./appmon.py -p $guestPlatform  --spawn 1  -s $scriptFolder/$scriptPlatform/"iOS"/ -a $targetAppName -o $outDir
   #frida-ps -U

fi

