-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.js
65 lines (56 loc) · 1.84 KB
/
check.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
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("*[xtag!='509']").each(function(){
var tagname = this.tagName;
/* TODO try to remove and STOP attacker's script
if (tagname === "SCRIPT") {
$(this).remove();
}
*/
// $(this).hide();
$(this).fadeOut(3000, "linear");
console.log(tagname + " removed hided (no xtag");
});
// remove element without ssig
$(":not([ssig])").each(function(){
if ($(this).attr('xtag') != undefined) { // need not remove twice
var tagname = this.tagName;
$(this).fadeOut(3000, "linear");
console.log(tagname + " removed hided (no ssig");
}
});
// traverse dom tree with post-order to check the integrity
traverse(document.body);
});
function check_and_alarm(node) {
// TODO implement asymmetric hash
md5 = CryptoJS.MD5(node.innerHTML).toString().substring(0, 8)
ssig = node.getAttribute('ssig')
if (md5 != ssig) {
console.log(ssig + ' vs ' + md5 + ' ' + node.nodeName);
$(node).css('background-color', 'red');
return false;
}
// console.log(ssig + ' ' + node.nodeName); // md5 matched
return true;
}
// return false if md5 not matched, traverse with post-order
function traverse(node) {
var num = $(node).children('[ssig]').length
var children_intact = true;
$(node).children('[ssig]').each(function(index, item) {
if (false == traverse(item))
children_intact = false;
});
// console.log(node.innerHTML);
// console.log(node.textContent);
// perform check for leaves
// if children are intact, also check for this node (like text field,
// the order of children)
if (num == 0 || children_intact == true)
return check_and_alarm(node);
return children_intact;
}
</script>