-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfake_accounts_detection.py
360 lines (294 loc) · 8.01 KB
/
fake_accounts_detection.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import pickle
import time
import math
Caltech_file=open('Caltech36', 'rb')
F=pickle.load(Caltech_file)
#F=Caltech
###Construction of the detection algorithm
#matrix of friend lists
def friends(A):
n=len(A)
B=[[-1 for i in range(n)] for j in range(n)]
t=0
for i in range(n):
for j in range(n):
if (A[i])[j]==1 and (i<j or j>i):
(B[i])[t]=j
t=t+1
t=0
return B
#s= friends(F)
#degree of the node
def degre (A,i):
n=len(A)
t=0
B=friends(A)
while (B[i])[t]!=(-1) and t<n:
t=t+1
return t
#average node degree of the network
def degremoyen(A):
n=len(A)
s=0
for i in range(n):
s+=degre(A,i)
return float(s)/float(n)
#Computation of the permutations for each node list
import random
def tablederoutage (A,B):
n=len(B[0])
t=[[B[k][l] for k in range(n)] for l in range(n)]
for i in range(n):
print(100*float(i)/float(n))
d = degre(A,i)
for j in range(d):
k = random.randint(0, d - j)
t[i][j],t[i][k+j] = t[i][k+j],t[i][j]
return t
#associate each node to its permutation
def tableroutage2 (A,B,C, node):
d=degre(A,node)
RT=[[0 for i in range(d)] for j in range(2)]
for i in range(d):
(RT[0])[i]=(B[node])[i]
(RT[1])[i]=(C[node])[i]
return RT
def search(B,row,friend):
n=len(B[0])
i=0
while i<n and (B[row])[i]!=friend:
i=i+1
if i==n:
i=0
return i
#image of each node by these permutations at a certain step
def next2(B, RT, friend):
n=len(RT[0])
j=0
while j<n and B[friend][j]!=friend:
j=j+1
if j>n-1:
return -1
else:
return (RT[friend])[j]
###Construction of the random routes and cromparison
#matrix in which every coefficient ai,j corresponds to the number of the final step of a route starting
#at the node node and going to the node j, after i permutations
def witness(A,B,RT,node,w):
n=degre(A,node)
l=len(A)
WT=[[0 for h in range(n)] for f in range(w)]
for i in range(n):
(WT[0])[i]=node
for i in range(n):
(WT[1])[i]=(B[node])[i]
for i in range(2,w):
for j in range(n):
WT[i][j]=next2(B,RT,WT[i-1][j])
return WT
#Comparison between two random routes
def comp (A,B,RT,V,S,W):
cou=witness(A,B,RT,V,W)
cou2=witness(A,B,RT,S,W)
d=0
n=len(cou[0])
p=len(cou2[0])
for i1 in range(0,n):
c4=0
for j1 in range(W):
for i2 in range(p):
for j2 in range(W):
if (cou[j1])[i1]==(cou2[j2])[i2]:
c4=c4+1
if c4>0:
d=d+1
return d
###Defining a detection criterion
###Construction of a random adjacency matrix to train the model
#returns a vector v, which i_th coordinate is equal to the number of nodes that have a degree equal to i
def statistique (A):
n=len (A)
v=[0 for s in range (0,n)]
for i in xrange (0,n):
v[degre(A,i)]+=1
print(100*float(i)/float(n))
return v
#plot the graph
#n=len(F)
#t = arange(0.0,n, 1)
#h=log(t)
y=statistique(F)
#print y[1], y[n-1]
v=np.zeros(n)
for i in xrange(n):
v[i]=y[i]
print v[1], v[n-1]
q=log(v)
plot(h,q)
show()
#returns a vector which ith coordinate is equal to the sum of the images of the i first integers by f.
def somme (f,n):
c=0
v=[0.0 for i in range(n)]
for i in xrange(1,n):
v[i]=f(i)+v[i-1]
return v
#definition of the function modeling the number of nodes by degree
def f(i):
if i==0:
c=0.0
else:
c=0.25*1000*(math.exp(-0.75*math.log(i*(1+math.exp(-math.log(0.75/35)/i)))))
return c
n=len(F)
v=somme(f,n)
#computes a random node degree, according to the probability distribution associated with the function f.
def prob(v):
n=len(v)
c=n
d=random.random()
for i in range(n-1):
if ((v[i])/v[n-1])<=d<((v[i+1])/v[n-1]):
c= i
return c
#random adjacency matrix similar to the Caltech one (same size, similar “number of nodes of a given degree” profile)
def matricerandom2(A,v):
n=len(A)
B=[[0 for x in range(n)]for w in range(n)]
for i in xrange(n):
h=prob(v)
print(100*float(i)/float(n))
g=[0 for s in range(h)]
for f in range(h):
s=random.randint(0,n)
if s<>i :
g[f]=s
else :
g[f]=random.randint(0,n)
for j in xrange(i):
if j in g:
B[i][j]=1
for i in xrange(n):
for j in xrange(i,n):
B[i][j]=B[j][i]
B[i][i]=1
return B
#plot the graph of the new matrix
C=matricerandom2(F,v)
t = range(0.0,n, 1)
#h=log(t)
y=statistique(C)
v=np.zeros(n)
for i in xrange(n):
v[i]=y[i]
t[i]=i
plot(t,v)
show()
###Generation of a random sybil attack
#generation of an exponential probability distribution
def k(v):
d=random.random()
s=(math.log(1-d)/v)
return -s
#which nodes will link the two networks
def generation( n,f):
c=[[(i,j) for i in range(f)]for j in range(n)]
for i in xrange(f):
for j in xrange(n):
k = random.randint(0, n - j-1)
c[j][i],c[k+j][i] = c[k+j][i],c[j][i]
return c
#linking the two networks (sybils+ new generated matrix)
def ajoute(A):
n=len(A)
f=k(0.1)
s=k(1)
if f>=n : #first probability is very low
f=n-1
if s>=n :
s=n-1
f=int(f)+1 #at least one sybil node
s=int(s)
G=generation(n,f)
V=[[0 for i in range(f)]for j in range(n)]
for i in xrange(f):
for j in xrange(s+1):#+1 because we want at least one connection
(a,b)=G[j][i]
V[b][a]=1
for i in xrange(n):
A[i]=A[i] +V[i]
for j in xrange(f):
b=[0 for s in range(n+f)]
for g in xrange(n):
if A[g][n+j]==1:
b[g]=1
for g in xrange(n+1,n+f):
b[g]=1
A=A+ [b]
return A
D=ajoute(C)
#list of the different number of intersections between the routes of two nodes
def listpour (A,v,s,m):
B=[0 for i in range (m)]
Q=friends(A)
q=tablederoutage(A,Q)
n=len(A)
w=int(math.sqrt(n)*math.log(n))
g=A
h=A
d=Q
c=Q
j=q
l=q
for s in xrange(m):
B[s]=comp(g,d,j,v,s,w)
g=h
d=c
j=l
return B
#empirical expectation of the previous function
def estimation (A,v,s,m):
V=listpour(A,v,s,m)
s=somme2(V,m)
return s/m
#Confusion matrix (number of honest/sybil nodes detected as honest/sybil
def houbre(D,v,s,x):
f=friends(D)
B=tablederoutage(D,f)
n=769
k=len(D)
print k
w=int(math.sqrt(k)*math.log(k))
J=[[0 for q in range(2)]for d in range(2)]
xou=[comp(D,f,B,i,s,w) for i in range(k-n)]
xou2=[comp(D,f,B,i,s,w) for i in range(n,k)]
print xou,xou2
for i in xrange((k-n)):# on va pas tous les prendre car ça serait bien trop couteux en terme de complexité.mais comme c'est créer aléatoirement ce n'est pas grave
print float(i)*float(100)/float (2*(k-n))
if xou[i]==x:
J[0][0]=J[0][0]+1
if xou[i]>x:
J[0][0]=J[0][0]+1
if xou[i]<x:
J[0][1]=J[0][1]+1
for i in xrange(k-n):
print float(i)*float(100)/float (2*(k-n))
if xou2[i]==x:
J[1][1]=J[1][1]+1
if xou2[i]>x:
J[1][0]=J[1][0]+1
if xou2[i] < x:
J[1][1]=J[1][1]+1
return J
###Results
#True if node is sybil (sybil detected as sybil), False if node is honest (honest detected as sybil
def resul(A,v,s,ve):
n=len(A)
verite=False
Fr=friends(A)
RT=tablederoutage(A,Fr)
w=int(math.sqrt(n)*math.log(n))
r=estimation(A,ve,s,m)
if r<50:
verite=True
return verite