-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyara_demo.py
69 lines (63 loc) · 1.79 KB
/
yara_demo.py
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
import re
import yara
import sys
import os.path
#---------------------------------------------------------------------
#This is for rule matching from yara. Takes care of all the regex's
data=0
'''
def mycallback(data):
print(data)
a=bool(input("continue to next rule?"))
if(a):
yara.CALLBACK_CONTINUE
else:
yara.CALLBACK_ABORT
'''
def run_yara(filename):
matched = rules.match(filename)
a=[]
for key,value in matched.items():
print(len(value))
for i in range(len(value)):
count = len(value[i]['strings'])
rule_name = str(value[i]['rule'])
print(rule_name+" "+str(count)+" times.")
for j in range(0,count):
rule_match = str(value[i]['strings'][j]['data'])
if(rule_name == "hidden_link"):
a.append(rule_match)
print("match ",str(i)," : ",rule_match)
#---------------------------------------------------------------
#Looking for hidden links and checking for equality of colors
link1_color="black"
body1_color="white"
body2_color="white"
for v in a:
body1=re.search("background-color *: *(.*?);",v)
if(body1!=None):
body1_color=body1.group(1)
body2=re.search("bgcolor *= *[\"\'](.*?)[\"\']",v)
if(body2!=None):
body2_color=body2.group(1)
link1=re.search("[^-]color *: *(.*?);",v)
if(link1):
link1_color=link1.group(1)
if(link1_color==body1_color or link1_color==body2_color):
print("Hidden link found")
print("done")
#---------------------------------------------------------------
if __name__=="__main__":
i=1
file_name = "./html/"+str(i)+".html"
rules = yara.compile(sys.argv[1])
li=[]
while(True):
if(os.path.isfile(file_name)):
print("-------------------------------------")
print("GOT "+file_name)
run_yara(file_name)
li.append(file_name)
if(file_name in li):
i=i+1
file_name="./html/"+str(i)+".html"