Keras
File size: 863 Bytes
659e0b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffe3a22
659e0b9
 
 
 
 
 
 
 
 
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
# Code for using the DGA detector model

library(keras)
library(plumber)
library(reticulate)

hfhub <- reticulate::import('huggingface_hub')
model <- hfhub$from_pretrained_keras("harpomaxx/dga-detector")
modelid="cacic-2018-model"
valid_characters <- "$abcdefghijklmnopqrstuvwxyz0123456789-_."
valid_characters_vector <- strsplit(valid_characters,split="")[[1]]
tokens <- 0:length(valid_characters_vector)
names(tokens) <- valid_characters_vector

# DGA prediction function
predict<-function(domain){
  domain_encoded <-
	  	sapply( 
			unlist(strsplit(tolower(domain),split="")), function(x) tokens [[x]] 
			) 
  domain_encoded<-pad_sequences(t(domain_encoded),maxlen=45,padding='post', truncating='post')

  prediction<-predict(model,domain_encoded)
  return(list(modelid=modelid,domain=domain,class=ifelse(prediction[1]>0.9,1,0),probability=prediction[1]))
}