-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessMetaEntities.py
More file actions
188 lines (179 loc) · 7.14 KB
/
ProcessMetaEntities.py
File metadata and controls
188 lines (179 loc) · 7.14 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
from __future__ import unicode_literals
import decision_tree
import sys
import os
import re
import random
import json
COMMON_NUM = 5
titleVocab = []
textVocab = []
languages = set()
allUsers = set()
def getPostCount(id, allPosts):
count =0
for post in allPosts:
if post["author_id"] == id:
count = count +1
return count
def getTopEntities(count, dirName, potName):
result = []
print("getting meta entities")
contentFile = open(dirName +'/'+potName+'-content.json', 'r')
content = json.load(contentFile)
usersFile = open(dirName +'/'+potName+'-user.json', 'r')
users = json.load(usersFile)
entitiesFile = open(dirName +'/'+potName+'-entities.json', 'r')
entities = json.load(entitiesFile)
entPostCounts = {}
for entity in entities:
entPostCounts[str(entity["id"])] = 0
uids = []
for user in users:
if user["ip"] in entity["ips"] and user["uid"] not in uids:
uids.append(user["uid"])
numPosts = getPostCount(user["uid"], content)
entPostCounts[str(entity["id"])] = entPostCounts[str(entity["id"])] + numPosts
print("%d %d" % (entity["id"], entPostCounts[str(entity["id"])]))
values = sorted([(v, k) for (k, v) in entPostCounts.items()], reverse=True)
for val in values[:count]:
result.append(entities[int(val[1])])
#print(result)
print([t for t in values[:count]])
return result
def getTopMetaEntities():
result = []
print("getting meta entities")
metaFile = open('meta-entities.json', 'r')
meta = json.load(metaFile)
gjamsFile = open('/lib/466/spam/gjams/gjams-content.json', 'r')
gjamsContent = json.load(gjamsFile)
ggjxFile = open('/lib/466/spam/ggjx/ggjx-content.json', 'r')
ggjxContent = json.load(ggjxFile)
npcagentFile = open('/lib/466/spam/npcagent/npcagent-content.json', 'r')
npcagentContent = json.load(npcagentFile)
contentData = {"gjams":gjamsContent, "ggjx":ggjxContent, "npcagent":npcagentContent}
#gjamsFile2 = open('/lib/466/spam/gjams/gjams-entities.json', 'r')
#gjamsEntities = json.load(gjamsFile2)
#ggjxFile2 = open('/lib/466/spam/ggjx/ggjx-entities.json', 'r')
#ggjxEntities = json.load(ggjxFile2)
#npcagentFile2 = open('/lib/466/spam/npcagent/npcagent-entities.json', 'r')
#npcagentEntities = json.load(npcagentFile2)
#entitiesData = {"gjams":gjamsEntities, "ggjx":ggjxEntities, "npcagent":npcagentEntities}
gjamsFile3 = open('/lib/466/spam/gjams/gjams-user.json', 'r')
gjamsUsers = json.load(gjamsFile3)
ggjxFile3 = open('/lib/466/spam/ggjx/ggjx-user.json', 'r')
ggjxUsers = json.load(ggjxFile3)
npcagentFile3 = open('/lib/466/spam/npcagent/npcagent-user.json', 'r')
npcagentUsers = json.load(npcagentFile3)
usersData = {"gjams":gjamsUsers, "ggjx":ggjxUsers, "npcagent":npcagentUsers}
pots = ["gjams", "ggjx", "npcagent"]
entPostCounts = {}
for entity in meta:
entPostCounts[str(entity["id"])] = 0
for entIp in entity["ips"]:
for potName in pots:
uids = []
for user in usersData[potName]:
if user["ip"] == entIp and user["uid"] not in uids:
uids.append(user["uid"])
numPosts = getPostCount(user["uid"], contentData[potName])
entPostCounts[str(entity["id"])] = entPostCounts[str(entity["id"])] + numPosts
print("%d %d" % (entity["id"], entPostCounts[str(entity["id"])]))
values = sorted([(v, k) for (k, v) in entPostCounts.items()], reverse=True)
for val in values[:50]:
result.append(meta[int(val[1])])
#print(result)
print([t for t in values[:50]])
return result
def getTopMetaEntities2():
result = []
print("getting meta entities")
metaFile = open('meta-entities.json', 'r')
meta = json.load(metaFile)
gjamsFile = open('/lib/466/spam/gjams/gjams-content.json', 'r')
gjamsContent = json.load(gjamsFile)
ggjxFile = open('/lib/466/spam/ggjx/ggjx-content.json', 'r')
ggjxContent = json.load(ggjxFile)
npcagentFile = open('/lib/466/spam/npcagent/npcagent-content.json', 'r')
npcagentContent = json.load(npcagentFile)
contentData = {"gjams":gjamsContent, "ggjx":ggjxContent, "npcagent":npcagentContent}
gjamsFile2 = open('gjams-entities-ids.json', 'r')
gjamsEntities = json.load(gjamsFile2)
ggjxFile2 = open('ggjx-entities-ids.json', 'r')
ggjxEntities = json.load(ggjxFile2)
npcagentFile2 = open('npcagent-entities-ids.json', 'r')
npcagentEntities = json.load(npcagentFile2)
entitiesData = {"gjams":gjamsEntities, "ggjx":ggjxEntities, "npcagent":npcagentEntities}
entPostCounts = {}
for entity in meta:
entPostCounts[str(entity["id"])] = 0
for entId in entity["ent_ids"]:
idParts = entId.split('_')
print(entId)
for id in entitiesData[idParts[0]][int(idParts[1])]["user_ids"]:
numPosts = getPostCount(id, contentData[idParts[0]])
entPostCounts[str(entity["id"])] = entPostCounts[str(entity["id"])] + numPosts
print("%d %d" % (entity["id"], entPostCounts[str(entity["id"])]))
return result
def main():
global languages
global allUsers
global titleVocab
global textVocab
args = sys.argv[1:]
if not args or len(args) < 1:
print("usage: ProcessSpam.py filename")
sys.exit(1)
filename = args[0]
potName = filename.split('/')[-1]
if os.path.exists(filename) and os.path.exists(filename + "/" + potName+"-content.json"):
#entities = getTopMetaEntities()
#allDocs = getPostsByTopMetaEntities(entities, "gjams")
entities = getTopEntities(20, filename, potName)
allDocs = getPostsByTopEntities(entities, filename, potName)
print("cumulative summary")
print(len(allUsers))
print(languages)
print(len(titleVocab))
print(len(textVocab))
print("getting features")
allDocs = extractFeatures(allDocs)
#print(allDocs[0][1].keys())
print("got data")
random.shuffle(allDocs)
cutoff = len(allDocs)/3
print("creating test and training sets")
testSet, trainingSet = allDocs[:cutoff ], allDocs[cutoff:]
tree = decision_tree.ML()
print("training decision tree")
tree.train(trainingSet)
#tree.post_traversal(tree.root, 0)
print("getting accuracy")
print(tree.accuracy(testSet))
print("getting f1 score")
#print(type(testSet[0][0]))
print(testSet[0][0])
tp, tn, fp, fn = tree.getStats(testSet[0][0],testSet)
print(tp, tn, fp, fn)
if(tp>0):
print(tree.getF1(tp, tn, fp, fn))
else:
print("No true positivex")
#print(len(testSet))
#print(len(trainingSet))
print("done")
else:
print("directory %s isn't a honeypot directory" % filename)
sys.exit(1)
#usersFile = open('/lib/466/spam/gjams/gjams-user.json', 'r')
#users = json.load(usersFile)
#accessFile = open('/lib/466/spam/gjams/gjams-access.json', 'r')
#access = json.load(accessFile)
#contentFile = open('/lib/466/spam/gjams/gjams-content.json', 'r')
#content = json.load(contentFile)
#entitiesFile = open('/lib/466/spam/gjams/gjams-entities.json', 'r')
#entities = json.load(entitiesFile)
if __name__ == '__main__':
main()