data.js 5.65 KB
const { ipcRenderer } = require("electron");
const { BrowserWindow } = require("electron").remote;
const path = require('path')
const fs = require('fs')
var io = require('socket.io').listen(3000);
var exec = require('child_process').exec, child;
//var mysql = require('mysql');


const connect_btn = document.getElementById("connect_btn");

connect_btn.addEventListener("click", () => {

    win = new BrowserWindow({
        width: 600,
        height: 450,
        frame: false,
        alwaysOnTop: true,
        webPreferences: {
			nodeIntegration: true
        }
    });

    //win.webContents.openDevTools();
    win.on("close", () => {
    	win = null;
    });
    win.loadFile("./src/connect.html");
    win.show();
    
});

var mysql = {
	user: 'root',
	password: '1234'
};

//1. apk 파일을 연결된 스마트폰 기기에 설치 및 애플리케이션을 실행
function apkInstall(){
	cmd_install = exec('adb -s '+device+' install -r app-release.apk',function(error, stdout, stderr){
		cc.innerHTML="install apk";
		cmd_exec = exec('adb -s '+device+' shell am start -n com.example.dataextraction/com.example.dataextraction.MainActivity',function(error, stdout, stderr){
			cc.innerHTML="start app";
		})
	});
	cmd_install();
}

function convertToMysql(){
	cmd_sqlIdb = exec('sqlite3 InnerDatabase.db .dump | python SQLiteToMysql.py > InnerDatabase.sql',function(error, stdout, stderr){
		cc.innerHTML="make InnerDB SQL";
		cmd_sqlNdb = exec('sqlite3 networkDatabase.db .dump | python SQLiteToMysql_network.py > networkDatabase.sql', function(error, stdout, stderr){
			cc.innerHTML="make NetworkDB SQL";
			cmd_makeIdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' -e "DROP DATABASE IF EXISTS DATAEXTRACTION;CREATE DATABASE DATAEXTRACTION CHARACTER SET utf8 COLLATE utf8_unicode_ci;USE DATAEXTRACTION;"',function(error, stdout, stderr){
				cc.innerHTML="create InnerDB";
				cmd_insertIdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' DATAEXTRACTION < InnerDatabase.sql',function(error, stdout, stderr){
					cc.innerHTML="insert Data ; InnerDB";
					cmd_makeNdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' -e "DROP DATABASE IF EXISTS DATAEXTRACTION_network;CREATE DATABASE DATAEXTRACTION_network;USE DATAEXTRACTION_network;"',function(error, stdout, stderr){
						cc.innerHTML="create NetworkDB";
						cmd_insertNdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' DATAEXTRACTION_network < networkDatabase.sql',function(error, stdout, stderr){
							cc.innerHTML="insert Data ; NetworkDB";
							cc.innerHTML="Data Extraction Complete !";
							cc.innerHTML="Click 'NEXT' Button";
						})
					})
				})
			})
		})
	})
}

//4. 추출한 .db 파일을 .sql 파일로 변환하여 mysql에 등록
function convertToMysql(){
	cmd_sqlIdb = exec('sqlite3 InnerDatabase.db .dump | python SQLiteToMysql.py > InnerDatabase.sql',function(error, stdout, stderr){
    cc.innerHTML="make InnerDB SQL";
    cmd_sqlNdb = exec('sqlite3 networkDatabase.db .dump | python SQLiteToMysql_network.py > networkDatabase.sql', function(error, stdout, stderr){
      cc.innerHTML="make NetworkDB SQL";
      cmd_makeIdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' -e "DROP DATABASE IF EXISTS DATAEXTRACTION;CREATE DATABASE DATAEXTRACTION CHARACTER SET utf8 COLLATE utf8_unicode_ci;USE DATAEXTRACTION;"',function(error, stdout, stderr){
        cc.innerHTML="create InnerDB";
        cmd_insertIdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' DATAEXTRACTION < InnerDatabase.sql',function(error, stdout, stderr){
          cc.innerHTML="insert Data ; InnerDB";
          cmd_makeNdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' -e "DROP DATABASE IF EXISTS DATAEXTRACTION_network;CREATE DATABASE DATAEXTRACTION_network;USE DATAEXTRACTION_network;"',function(error, stdout, stderr){
            cc.innerHTML="create NetworkDB";
            cmd_insertNdb = exec('mysql -u'+mysql.user+' -p'+mysql.password+' DATAEXTRACTION_network < networkDatabase.sql',function(error, stdout, stderr){
              cc.innerHTML="insert Data ; NetworkDB";
              cc.innerHTML="Data Extraction Complete !";
              cc.innerHTML="Click 'NEXT' Button";
            })
          })
        })
      })
    })
  })
}

//3. adb backup 명령어를 통해 데이터베이스 추출
function extractDB () {
	cmd_backup = exec('adb -s '+device+' backup com.example.dataextraction -f backup.dataextraction.ad', function(error, stdout, stderr){
		cc.innerHTML="backup app";
		setTimeout(function(){
			cmd_unpack = exec('java -jar abe.jar unpack backup.dataextraction.ad dataextraction.tar',function(error, stdout, stderr){
				cc.innerHTML="unpack backup";
				cmd_unzip = exec('tar xvf dataextraction.tar', function(error, stdout, stderr){
					cc.innerHTML="unzip tar";
					cmd_moveIdb = fs.rename('apps/com.example.dataextraction/db/InnerDatabase.db', 'InnerDatabase.db', function(){
						cc.innerHTML="move InnerDB File";
						cmd_moveNdb = fs.rename('apps/com.example.dataextraction/db/networkDatabase.db', 'networkDatabase.db',function(){
							cc.innerHTML="move NetworkDB File";
							convertToMysql();
						})
					})
				})
			})
		},7000);
	});
  cmd_backup();
}

//2. 애플리케이션에서 데이터베이스 생성이 끝나면 알림
io.on('connection', function(socket){
	var instanceId = socket.id;

	socket.on('alert', function(data){
		cc.innerHTML = data.comment+" db created";
	})
	socket.on('end', function(data){
		cc.innerHTML="all databases created !";
		extractDB();
  	})
})

var device = new String();
ipcRenderer.on("device", (e, arg) => {
    device = arg;
    connect_btn.innerHTML = "Connected : ".concat(device);
    //device.innerHTML = arg.toString();

    cc.innerHTML="DATA EXTRACTION START !";
	apkInstall();

});