Autore Topic: Machine Learning-driven Firewall  (Letto 173 volte)

0 Utenti e 1 Visitatore stanno visualizzando questo topic.

Offline Flavio58

Machine Learning-driven Firewall
« Risposta #1 il: Maggio 04, 2018, 03:16:51 am »
Advertisement
Lately, I have been thinking of ways of applying machine learning to a security project that I can do and share with all of you. A few days ago, I happened to come across a website called ZENEDGE which is offering AI driven web application firewall. I liked the concept and thought of making something similar and sharing it with the community. So, lets make one.

fWaf – Machine learning driven Web Application Firewall

http://www.tfidf.com/

Dataset:
The first thing to do was to find labelled data but the data I could find was quite old (2010). There is a website called SecRepo that has a lot of security related datasets. One of them was of http logs containing millions of queries. That was the dataset I wanted but it was not labelled. I used some heuristics and my previous knowledge of security to label the data set by writing a few scripts.

After pruning the data, I wanted to collect some more malicious queries. Therefore, I went on for payloads and found some famous GitHub repositories containing Xss, SQL and other attack payloads and used all of them in my malicious queries dataset.

Now, we had two files; one containing clean web queries(1000000) and another one containing malicious web queries(50000). That’s all the data we need to train our classifier.

Training:
For training, I used logistic regression since it is fast and I wanted something fast. We can use SVM or Neural networks but they take a little more time than logistic regression. Our problem is a binary classification problem since we have to predict whether a query is malicious or not. We’ll  be using ngrams as our tokens. I read some research papers and using ngrams was a good idea for this sort of project. For this project, I used n=3.

Lets dive right into the code.

Lets define our tokenizer function which will give 3 grams.

Codice: [Seleziona]
def getNGrams(query):
tempQuery = str(query)
ngrams = []
for i in range(0,len(tempQuery)-3):
ngrams.append(tempQuery[i:i+3])
return ngrams


Lets load the queries dataset.
Codice: [Seleziona]
filename = 'badqueries.txt'
directory = str(os.getcwd())
filepath = directory + "/" + filename
data = open(filepath,'r').readlines()
data = list(set(data))
badQueries = []
validQueries = []
count = 0
for d in data:
d = str(urllib.unquote(d).decode('utf8'))
badQueries.append(d)

filename = 'goodqueries.txt'
directory = str(os.getcwd())
filepath = directory + "/" + filename
data = open(filepath,'r').readlines()
data = list(set(data))
for d in data:
d = str(urllib.unquote(d).decode('utf8'))
validQueries.append(d)
 

Now that we have the dataset loaded into good queries and bad queries. Lets try to visualize them. I used Principal component analysis to visualize the dataset. The read are the bad query ngrams and the blue are the good query ngrams.



We can see that bad points and good points are indeed on coming out on different positions. Lets proceed further.

badQueries = list(set(badQueries))
tempvalidQueries = list(set(validQueries]))
tempAllQueries = badQueries + tempvalidQueries
bady = [1 for i in range(0,len(tempXssQueries))]
goody = [0 for i in range(0,len(tempvalidQueries))]
y = bady+goody
queries = tempAllQueries


Lets now use Tfidvectorizer to convert the data into tfidf values and then use our classifier. We are using tfidf values since we want to assign weights to our ngrams e.g the ngram ‘<img’ should have large weight since a query containing this ngram is most likely to be malicious. You can read more about tfidf in this link.

Codice: [Seleziona]
vectorizer = TfidfVectorizer(tokenizer=getNGrams)
X = vectorizer.fit_transform(queries)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
 

Now that we have everything set up, lets apply logistic regression.

lgs = LogisticRegression()
lgs.fit(X_train, y_train)
print(lgs.score(X_test, y_test))


That’s it.

Here’s the part everyone waits for, you must be wanting to see the accuracy, right? The accuracy comes out to be 99%. That’s pretty amazing right? But you won’t believe it until you see some proof. So, lets check some queries and see if the model detects them as malicious or not. Here are the results.

wp-content/wp-plugins (CLEAN)
<script>alert(1)</script> (MALICIOUS)
SELECT password from admin (MALICIOUS)
"> (MALICIOUS)
/example/test.php (CLEAN)
google/images (CLEAN)
q=../etc/passwd (MALICIOUS)
javascript:confirm(1) (MALICIOUS)
"> (MALICIOUS)
foo/bar (CLEAN)
foooooooooooooooooooooo (CLEAN)
example/test/q=<script>alert(1)</script> (MALICIOUS)
example/test/q= (MALICIOUS)
fsecurify/q= (MALICIOUS)
example/test/q= (MALICIOUS)
 

Looks good, doesn’t it? It can detect the malicious queries very well.

What next? This is a weekend project and there is a lot that can be done or added in it. We can do multi class classification to detect whether a malicious query is SQL Injection or Cross site scripting or any other injection. We can have a larger dataset with all types of malicious queries and train the model on it thus expanding the type of malicious queries it can detect. One can also save this model and use it with a web server. Let me know if you do any of the above.

Data and script: https://github.com/faizann24/Fwaf-Machine-Learning-driven-Web-Application-Firewall

I hope you liked the post. We believe in providing security resources for free to the community. Let me know about your comments and critique.


Consulente in Informatica dal 1984

Software automazione, progettazione elettronica, computer vision, intelligenza artificiale, IoT, sicurezza informatica, tecnologie di sicurezza militare, SIGINT. 

Facebook:https://www.facebook.com/flaviobernardotti58
Twitter : https://www.twitter.com/Flavio58

Cell:  +39 366 3416556

f.bernardotti@deeplearningitalia.eu

#deeplearning #computervision #embeddedboard #iot #ai

 

Related Topics

  Oggetto / Aperto da Risposte Ultimo post
0 Risposte
147 Visite
Ultimo post Giugno 20, 2018, 03:34:40 pm
da Marco1971
0 Risposte
227 Visite
Ultimo post Giugno 20, 2018, 03:53:04 pm
da Marco1971
0 Risposte
167 Visite
Ultimo post Giugno 26, 2018, 10:01:40 pm
da Ruggero Respigo
0 Risposte
116 Visite
Ultimo post Giugno 28, 2018, 08:02:47 pm
da Flavio58
0 Risposte
166 Visite
Ultimo post Luglio 02, 2018, 10:00:45 pm
da Flavio58

Sitemap 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