Socialify

Folder ..

Viewing app.R
451 lines (430 loc) • 18.3 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#
library(shinythemes)
library(shiny)
library(DT)
library(data.table)
library(ggplot2)
library(shinycssloaders)
library(h2o)

# Defining Non Changing Variables
data <- fread("2020.10.01.csv")
data_na_removed <- na.omit(data)

# Encoding the Label Column
# 1 - Benign 2 - Malicious 3 - Outlier
data_encoded <- data_na_removed
data_encoded$label <- factor(data_na_removed$label,
                               levels = c("benign", "malicious", "outlier"),
                               labels = c(1, 2, 3))
data_encoded$label = as.numeric(data_encoded$label)
# Define Default Values
pch = 16
features <- c("Average Input","Incoming Bytes","Outgoing Bytes",
              "Destination IP", "Destination Port", "Entropy", 
              "Inbound Packets", "Outbound Packets", "Protocol",
              "Source IP", "Source Port", "Start Time (s)", 
              "End Time (s)", "Total Entropy", "Type", "Duration")
feature_variables <- c("avg_ipt", "bytes_in", "bytes_out", "dest_ip", 
                       "dest_port", "entropy", "num_pkts_out", "num_pkts_in",
                       "proto", "src_ip", "src_port", "time_end", "time_start",
                       "total_entropy", "label", "duration")

# Define Elementary Functions
get_color <- function(a = 1) {
    return(alpha("#e95420", a))
}

# Load the models
model.dl <- h2o.loadModel(dl_model)
model.drf <- h2o.loadModel(drf_model)
model.gbm <- h2o.loadModel(gbm_model)
model.nb <- h2o.loadModel(nb_model)

# Define UI for application
ui <- fluidPage(
    theme = shinytheme("united"),
    # Application title
    titlePanel("A Comprehensive Approach To Analysis and Detection of Emerging 
               Threats due to Network Intrusion"),

    navbarPage(
        "Network Intrusion Detection Demo",
        tabPanel(
            icon("home"),
            p("Through this application, it is intended to develop a demo of a",
            strong("Network Intrusion Detection System"), 
            "using different Machine Learning Techniques using the 
            LUFlow Network Intrusion Detection Data Set. This page is intended
            to display the information about the dataset."
            ,style="text-align:justify;color:black;
            background-color:lavender;padding:15px;border-radius:10px"),
            br(),
            p("The data used in this application are publicly available on the",
            em("LUFlow Network Intrusion Detection Data Set"), "Kaggle page. 
            The Data Set contains telemetry cap- tured using Cisco’s Joy tool. 
            This tool records multiple measurements asso- ciated with flows. 
            Features are engineered from these measurements, which are also 
            outlined below",style="text-align:justify;color:black;
            background-color:papayawhip;padding:15px;border-radius:10px"),
            hr(),
            tags$style(".fa-database {color:#e95420}"),
            h3(p(icon("database",lib = "font-awesome"),
                 em("Dataset Exploration "),
                 style="color:black;text-align:center")),
            fluidRow(column(DT::dataTableOutput("renderData"),
                            width = 12)),
            hr(),
            p(em("Developed by"), br("Kumar Priyansh, Ritu Dimri,
                                     Sandeep Perumalla, Hemanth Katikala"), 
              style="text-align:center; font-family: times")
        ),
        tabPanel(
            "Data Visualization",
            p("This part allows you to visualize features via different types of
              plots. You can select whatever features you want to plot and hit
              the \"Plot Graph\" button. Please keep in mind that all plots",
              strong("might not be useful"), 
              "and you need to select which plots you want to visualize. If you
              want to save an image of the currently visualized plot, please
              right click on the plot and click on the relevant",
              strong("save image"),
              "option."
              ,style="text-align:justify;color:black;
            background-color:lavender;padding:15px;border-radius:10px"),
            sidebarLayout(
                sidebarPanel(
                    selectInput(
                        "plotType",
                        p("Type of plot:"),
                        choices = c(Histogram = "hist",
                                    "Scatter Plot" = "scatter",
                                    "Mosaic Plot" = "mosaic")
                    ),
                    # Only show this panel if the plot type is a histogram
                    conditionalPanel(
                        condition = "input.plotType == 'hist'",
                        selectInput(
                            "plotVariable",
                            p("Feature to visualize:"),
                            choices = features
                        ),
                        selectInput(
                            "plotVariant",
                            p("Plot variant:"),
                            choices = c("Normal", "Log 10 Scale")
                        )   
                    ),
                    
                    # Only show this panel if the plot type is a scatter plot
                    conditionalPanel(
                        condition = "input.plotType == 'scatter'",
                        selectInput(
                            "plotVariable1",
                            p("First feature to visualize:"),
                            choices = features
                        ),
                        uiOutput("secondSelection")
                    ),
                    
                    # Single Mosiac Plot for now
                    conditionalPanel(
                        condition = "input.plotType == 'mosaic'",
                        selectInput(
                            "mosaicVariable",
                            p("Select features to visualize:"),
                            choices = c("Labels vs Protocols" = "labproto")
                        )
                    ),
                    actionButton("plot", "Plot Graph",
                                 width = "100%", icon = icon("chart-line"),
                                 class = "btn btn-primary")
                ),
                mainPanel(
                    withSpinner(
                        plotOutput("selectedFeatureVariableForVisualization"),
                        type = 6, color = "#e95420"
                    )
                )
            )
        ),
        tabPanel(
            "Predictions",
            sidebarLayout(
                sidebarPanel(
                    selectInput(
                        "modelType",
                        p("Choose a model to predict:"),
                        choices = c("Deep Learning" = "dl",
                                    "Distributed Random Forest" = "drf",
                                    "Gradient Boosting Machine" = "gbm",
                                    "Naive Bayes" = "nb")
                    ),
                    numericInput("npin", "Number of inbound packets:", 
                                 10, min = 0),
                    numericInput("npob", "Number of outbound packets:", 
                                 10, min = 0),
                    numericInput("nbin", "Number of bytes in:", 
                                 2000, min = 0),
                    numericInput("nbob", "Number of bytes out:", 
                                 10000, min = 0),
                    numericInput("dprt", "Destination port (1024 - 49151):", 
                                 5234, min = 1024, max = 49151),
                    numericInput("tepy", "Total entropy:", 
                                 18000, min = 0),
                    actionButton("predictButton", "Predict",
                                 width = "100%", icon = icon("think-peaks"),
                                 class = "btn btn-primary")
                ),
                mainPanel(
                    tags$label(h3('Status/Output')),
                    verbatimTextOutput('contents'),
                    fluidRow(
                        column(
                            width = 6,
                            p(strong("Prediction Legend"), br(), br(), em("1"), 
                              " - Benign", br(), em("2"), " - Malicious",
                              br(), em("3"), " - Outlier", 
                              style="text-align:justify;color:black;
            background-color:lavender;padding:15px;border-radius:10px"),
                        ),
                        column(
                            width = 6,
                            p(strong("Probabilty Legend"), br(), br(), em("p1"), 
                              " - Probability of being a Benign Connection",
                              br(), em("p2"), " - Probability of being a 
                              Malicious Connection", br(), em("p3"), " - 
                              Probability of being an Outlier Connection", 
                              style="text-align:justify;color:black;
            background-color:papayawhip;padding:15px;border-radius:10px"),
                        )
                    ),
                    tableOutput('tabledata'), # Prediction results table
                    fluidRow(
                        column(
                            width = 6,
                            withSpinner(
                                plotOutput("varImpPlot"),
                                type = 6, color = "#e95420"
                            )
                        ),
                        column(
                            width = 6,
                            withSpinner(
                                plotOutput("lcPlot"),
                                type = 6, color = "#e95420"
                            )
                        )
                    )
                )
            )
        ),
        tabPanel(
            "Model Metrics",
            sidebarLayout(
                sidebarPanel(
                    selectInput(
                        "metricModelType",
                        p("Choose a model to show metrics:"),
                        choices = c("Deep Learning" = "dl",
                                    "Distributed Random Forest" = "drf",
                                    "Gradient Boosting Machine" = "gbm",
                                    "Naive Bayes" = "nb"),
                    ),
                    actionButton("metricsButton", "Show Metrics",
                                 width = "100%", icon = icon("tachometer-alt"),
                                 class = "btn btn-primary")
                ),
                mainPanel(
                    verbatimTextOutput('metrics')
                )
            )
        ),
        tabPanel(
            "Model Summary",
            sidebarLayout(
                sidebarPanel(
                    selectInput(
                        "summaryModelType",
                        p("Choose a model to show metrics:"),
                        choices = c("Deep Learning" = "dl",
                                    "Distributed Random Forest" = "drf",
                                    "Gradient Boosting Machine" = "gbm",
                                    "Naive Bayes" = "nb"),
                    ),
                    actionButton("summaryButton", "Show Information",
                                 width = "100%", icon = icon("clipboard"),
                                 class = "btn btn-primary")
                ),
                mainPanel(
                    verbatimTextOutput('summary')
                )
            )
        )
    )
)

# Define server logic
server <- function(input, output) {
    output$renderData <- DT::renderDataTable(
        DT::datatable({
            data_na_removed
        },
        options = list(
            initComplete = JS(
                "function(settings, json) {",
                "$(this.api().table().header()).css({'background-color': 
                'moccasin', 'color': '1c1b1b'});",
                "}"),
            columnDefs=list(list(className='dt-center',targets="_all"))),
        style = 'bootstrap',
        class = 'cell-border stripe',
        rownames = FALSE,
        colnames = features)
    )
    
    datasetInput <- reactive({
        modelType <- input$modelType
        req(input$npin)
        req(input$npob)
        req(input$nbin)
        req(input$nbob)
        req(input$dprt)
        req(input$tepy)
        df <- data.frame(
            Name = c("num_pkts_in", "bytes_in", "num_pkts_out", "bytes_out",
                     "dest_port", "total_entropy"),
            Value = as.character(c(input$npin, input$nbin, input$npob, 
                                   input$nbob, input$dprt, input$tepy)),
            stringsAsFactors = FALSE)
        labels <- 0
        df <- rbind(df, labels)
        input <- transpose(df)
        write.table(input,"input.csv", sep=",", quote = FALSE, 
                    row.names = FALSE, col.names = FALSE)
        test <- read.csv(paste("input", ".csv", sep=""), header = TRUE)
        
        if (modelType == "dl") {
            predict(model.dl, as.h2o(test))
        } else if (modelType == "drf") {
            predict(model.drf, as.h2o(test))
        } else if (modelType == "gbm") {
            predict(model.gbm, as.h2o(test))
        } else if (modelType == "nb") {
            predict(model.nb, as.h2o(test))
        }
    })
    
    output$varImpPlot <- renderPlot({
        modelType <- input$modelType
        if (modelType == "dl") {
            h2o.varimp_plot(model.dl)
        } else if (modelType == "drf") {
            h2o.varimp_plot(model.drf)
        } else if (modelType == "gbm") {
            h2o.varimp_plot(model.gbm)
        }
        
    })
    
    output$lcPlot <- renderPlot({
        modelType <- input$modelType
        if (modelType == "dl") {
            h2o.learning_curve_plot(model.dl)
        } else if (modelType == "drf") {
            h2o.learning_curve_plot(model.drf)
        } else if (modelType == "gbm") {
            h2o.learning_curve_plot(model.gbm)
        }
    })
    
    output$metrics <- renderPrint({
        input$metricsButton
        isolate({
            modelType <- input$metricModelType
            if (modelType == "dl") {
                h2o.performance(model.dl)
            } else if (modelType == "drf") {
                h2o.performance(model.drf)
            } else if (modelType == "gbm") {
                h2o.performance(model.gbm)
            } else {
                h2o.performance(model.nb)
            }
        })
    })
    
    output$summary <- renderPrint({
        input$summaryButton
        isolate({
            modelType <- input$summaryModelType
            if (modelType == "dl") {
                summary(model.dl)
            } else if (modelType == "drf") {
                summary(model.drf)
            } else if (modelType == "gbm") {
                summary(model.gbm)
            } else {
                summary(model.nb)
            }
        })
    })
    
    # Status/Output Text Box
    output$contents <- renderPrint({
        if (input$predictButton>0) { 
            isolate("Calculation complete.") 
        } else {
            return("Server is ready for calculation.")
        }
    })
    
    # Prediction results table
    output$tabledata <- renderTable({
        if (input$predictButton>0) { 
            isolate(datasetInput())
        } 
    })
    
    output$secondSelection <- renderUI({
        selectedFeature <- input$plotVariable1
        selectInput(
            "plotVariable2",
            p("Second feature to visualize:"),
            choices = features[!features %in% selectedFeature]
        )
    })

    output$selectedFeatureVariableForVisualization <- renderPlot({
        input$plot
        isolate({
            plotType <- input$plotType
            if (plotType == 'hist') {
                selectedFeature <- input$plotVariable
                plotVariant <- input$plotVariant
                positionInFeatureArray <- which(features == selectedFeature)
                selectedFeatureVariable <- feature_variables[positionInFeatureArray]
                if (plotVariant == "Normal") {
                    hist(data_encoded[[selectedFeatureVariable]], 
                         main = paste("Histogram Plot of", selectedFeature, sep = " ", collapse = NULL),
                         ylab = "Frequency", xlab = selectedFeature,
                         col = get_color(), pch = pch)
                } else {
                    nonZeroSelectedFeature = data_encoded[data_encoded[[selectedFeatureVariable]] > 0]
                    hist(log(nonZeroSelectedFeature[[selectedFeatureVariable]]), 
                         main = paste("Log 10 Base Histogram Plot of", selectedFeature, sep = " ", collapse = NULL),
                         ylab = "Frequency", xlab = selectedFeature,
                         col = get_color(), pch = pch)
                } 
            } else if (plotType == 'scatter') {
                firstFeature <- feature_variables[which(features == 
                                                            input$plotVariable1)]
                secondFeature <- feature_variables[which(features == 
                                                             input$plotVariable2)]
                try(plot(data_encoded[[firstFeature]], data_encoded[[secondFeature]],
                         main = paste("Scatter Plot of", input$plotVariable1,
                                      "vs", input$plotVariable2, sep = " ", collapse = NULL),
                         ylab = input$plotVariable2, xlab = input$plotVariable1,
                         col = get_color(0.02),
                         pch = 16,), silent = TRUE)
            } else {
                selectedFeatures <- input$mosaicVariable
                if (selectedFeatures == 'labproto') {
                    proto_label_mosaic <- table(data_encoded$proto, data_encoded$label)
                    mosaicplot(~ factor(proto)+factor(label, labels=c("benign","malicious","outlier")),
                               data = data_encoded,xlab = "Protocol", ylab = "Category", 
                               main= "Mosaic plot of Protocol vs Category",shade = TRUE)
                }
            }
        })
    })
}

# Run the application 
shinyApp(ui = ui, server = server)