This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obf.js
126 lines (111 loc) · 3.6 KB
/
obf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
String.prototype.replaceBetween = function(start, end, what) {
return this.substring(0, start) + what + this.substring(end);
};
const fs = require("fs");
let data = fs.readFileSync("./simple.js", {encoding:'utf8', flag:'r'});
let datArray = Array.from(String(data))
var lastCharFound = "";
var lastCharCount = 0;
function findBy(char, alt) {
if(!alt) alt = char
let base = 0;
var found = [];
while(true) {
//console.log("Finding by", char, "and", alt)
let loc_s = findNextChar(data, char, base);
let loc_e = findNextChar(data, char, loc_s + 1) + 1;
if(loc_s == undefined || loc_e == undefined) {
//console.log("Breaking:", loc_s,loc_e)
break;
}
console.log("Found both at", loc_s, "and", loc_e);
let content = data.substr(loc_s, loc_e - loc_s);
found.push({loc_s, loc_e, content})
base = loc_e + 1
}
//console.log("findBy", found)
return found;
}
function combineArrays(first, second, third) {
//console.log("Combining")
//console.log(first)
//console.log(second)
//console.log(third)
first.forEach(e => second.push(e));
if(third) third.forEach(e => second.push(e));
return second;
}
function relocateStrings(data) {
console.log("starting string relocation.")
var strTableName = "relocTable";
let res = combineArrays(findBy('"'), findBy("'"))
console.log("Finshed looking for chars.")
//console.log(res)
let i = 0;
let tmp = "";
let table = "";
let offset = 0;
res.forEach(val => {
console.log("Found string starting at", val.loc_s, "and ending at", val.loc_e, "- Contents is", val.content, "Replacing with id:", i)
let txt = `${strTableName}[${i}]`;
data = data.replaceBetween(val.loc_s + offset, val.loc_e + offset, txt);
offset += txt.length - val.content.length;
table += `${tmp}${val.content}`;
tmp = ", ";
++i;
});
//console.log(data)
// console.log(data)
data = `let ${strTableName} = [${table}];\n` + data;
//console.log(`let ${strTableName} = [${table}];\n`);
//console.log(data)
fs.writeFileSync("./stringRelocated.js", data)
return data;
}
function findLocationsOfChar(data, value) {
//console.log("Looking for", value);
found = [];
let i = 0;
while(i != data.length) {
if(data[i] == value) {
found.push(i);
}
++i;
};
//console.log("Found:", found)
return found;
}
function findNextChar(data, value, offset) {
//console.log("Looking for", value);
let i = offset;
while(i <= data.length) {
//console.log(i," : [", data[i], "]")
if(data[i] == value) {
//console.log("FOUND returning", i)
return i;
}
++i;
};
}
console.log("Starting scan.")
var functions = [];
function resolveFunctionLocations() {
var starts = findLocationsOfChar(datArray, "{");
var ends = findLocationsOfChar(datArray, "}");
console.log("Found", starts.length, "starts and", ends.length, "endings.")
var funcLocs = [];
if(starts.length != ends.length) {
console.log("Code is broken or a string contains a {");
}
}
//console.log(data)
//console.log(datArray)
console.log("========================")
relocateStrings(data);
//resolveFunctionLocations();
// let startFunc = findNextChar(datArray, "{")
// let nextFunc = findNextChar(datArray, "{", startFunc)
// let testEnd = findNextChar(datArray, "}", startFunc)
// console.log("startFunc:", startFunc)
// console.log("nextFunc:", nextFunc)
// console.log("testEnd:", testEnd)