Skip to content

Instantly share code, notes, and snippets.

@lqdc
Last active August 29, 2015 14:08
Show Gist options
  • Save lqdc/b171c280fbf543c7831c to your computer and use it in GitHub Desktop.
Save lqdc/b171c280fbf543c7831c to your computer and use it in GitHub Desktop.
Mathematical Model of Malware Proliferation
import numpy as np
import pylab as pl
def calc_lv(x, y, coefs):
x_new = x * (coefs['a'] - coefs['b'] * y - coefs['c'])
y_new = y * (coefs['e'] * coefs['b'] * x - coefs['d'])
return x_new, y_new
def calc_ml(V, Q, I, U, W, W0, coefs):
n = 1 - np.e ** (-coefs['a'] * W)
n0 = 1 - np.e ** (-coefs['a0'] * W0)
nw = 1 - np.e ** (-coefs['p'] * W)
nw0 = 1 - np.e ** (-coefs['p0'] * W0)
bQ = coefs['b'] * Q
gU = coefs['g'] * U
Vk = V ** coefs['k']
hU = coefs['h'] * U
V_new = -coefs['e'] * Vk * n0 - coefs['c'] * Vk * n + bQ
Q_new = coefs['f'] * I - bQ + gU
I_new = coefs['c'] * Vk * n - coefs['f'] * I + hU
U_new = coefs['e'] * Vk * n0 - gU - hU
W_new = coefs['l'] * nw * Vk - coefs['m'] * W
W0_new = coefs['n'] * nw0 * Vk - coefs['o'] * W0
return V_new, Q_new, I_new, U_new, W_new, W0_new
def forward_euler_lotka(t, x, y, coefs, dt, N):
for i in range(N):
dx, dy = calc_lv(x[i], y[i], coefs)
x[i + 1] = x[i] + dt * dx
y[i + 1] = y[i] + dt * dy
t[i + 1] = i * dt
return t, x, y
def midpoint_method_lotka(t, x, y, coefs, dt, N):
for i in range(N):
dx1, dy1 = calc_lv(x[i], y[i], coefs)
dx2, dy2 = calc_lv(x[i] + dt * dx1 / 2, y[i] + dt * dy1 / 2, coefs)
x[i + 1] = x[i] + dt * dx2
y[i + 1] = y[i] + dt * dy2
t[i + 1] = i * dt
return t, x, y
def midpoint_ml(t, V, Q, I, U, W, W0, coefs, dt, N):
for i in range(N):
dv, dq, di, du, dw, dw0 = calc_ml(V[i], Q[i], I[i], U[i], W[i], W0[i], coefs)
dv2, dq2, di2, du2, dw2, dw02 = calc_ml(
V[i] + dt * dv/2,
Q[i] + dt * dq/2,
I[i] + dt * di/2,
U[i] + dt * du/2,
W[i] + dt * dw/2,
W0[i] + dt * dw0/2,
coefs)
V[i + 1] = V[i] + dt * dv2
Q[i + 1] = Q[i] + dt * dq2
I[i + 1] = I[i] + dt * di2
U[i + 1] = U[i] + dt * du2
W[i + 1] = W[i] + dt * dw2
W0[i + 1] = W0[i] + dt * dw02
t[i + 1] = i * dt
return t, V, Q, I, U, W, W0
def do_lotka():
# a,b,c,d,e are the coefficients of the Lotka-Volterra model
# dt=step size
# N=number of iterations
coefs = {'a': 0.04,
'b': 0.0005,
'c': 0.0001,
'd': 0.2,
'e': 0.1}
x0 = 200
y0 = 50
dt = 0.01
days = 500
N = int(days / dt)
x, y, t = np.zeros((3, N + 1))
x[0] = x0 # Initial prey data
y[0] = y0 # Initial predator data
t, x, y = forward_euler_lotka(t, x, y, coefs, dt, N)
pl.plot(t, x/10, 'g', t, y, 'r')
pl.show()
def do_malware():
coefs = {
'a': 0.0001, # infection coefficient of known viruses
'a0': 0.001, # infection coefficient of zero days
'b': 1., # rate of restoring quarantined computers
'c': 0.2, # fraction of known viruses actually working
'e': 0.8, # fraction of 0day viruses actually working
'f': 0.9, # detection rate of known infected viruses
'g': 0.2, # detection rate of 0days
'h': 1., # rate of conversion from 0days to known
'k': 1, # clustering coefficient of computers
'l': 1,
'm': 0.3,
'n': 0.1,
'o': 0.2,
'p': 0.05,
'p0': 0.08}
days = 60
dt = 0.1
N = int(days / dt)
V, I, Q, U, W, W0, t = np.zeros((7, N + 1))
v0, i0, q0, U0 = 989, 10, 0, 1
V[0] = v0 # Initial vulnerable
I[0] = i0 # Initial infected
Q[0] = q0 # initial quarantined
U[0] = U0 # initial infected with zero days
W[0] = 100
W0[0] = 1
t, V, Q, I, U, W, W0 = midpoint_ml(t, V, Q, I, U, W, W0, coefs, dt, N)
total = 1000
pl.subplot(211)
pl.plot(t, V/total, 'g', label="Vulnerable", linewidth=2)
pl.plot(t, I/total, 'r', label="Infected with known malware")
pl.plot(t, Q/total, 'b', label="Quarantined")
pl.plot(t, U/total, 'k', label="Infected with 0-Days")
pl.title("Population dynamics")
pl.legend(loc='best')
pl.subplot(212)
pl.plot(t, W, 'r', label='Known Malware')
pl.plot(t, W0, 'y', label='0-Day Malware')
pl.legend(loc='best')
pl.show()
if __name__ == '__main__':
# do_lotka()
do_malware()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment