-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoxcli_main.vb
3003 lines (2372 loc) · 117 KB
/
oxcli_main.vb
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Imports System.Text.RegularExpressions
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Net.WebRequestMethods
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.ComponentModel
Imports System.Threading
Imports System.Runtime.Intrinsics.Arm
Module Program
Public aTimer As New System.Timers.Timer
Public OX As oxWrapper
Public currOffset = 0
Public issueLimit = 10000
Public ogDir$
Public pyDir$
Public cacheDir$
Public osType$
Public numResponseFiles As Integer = 0
Public WithEvents issueCacheLoader As BackgroundWorker
Public issuesCache As List(Of singleIssue)
Public fileNames As List(Of String)
Public currentlyLoading As Boolean
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
' Public Sub loadJSONissues() As
Function Main(args As String()) As Integer
' Environment.ExitCode = 1
If UBound(args) = -1 Then
Console.WriteLine("You must enter a command. Try 'help'.")
End
End If
OX = New oxWrapper("", "")
' Main = 1
' Console.WriteLine("Returning Exit Code " + Environment.ExitCode.ToString)
Dim actioN$ = args(0)
Console.WriteLine("ACTION: " + actioN)
' this will generate errors if no python folder exists
ogDir$ = FileSystem.CurDir
Call System.IO.Directory.CreateDirectory("cache")
ChDir("cache")
cacheDir = FileSystem.CurDir
ChDir(ogDir)
ChDir("python")
pyDir$ = FileSystem.CurDir
ChDir(ogDir)
If RuntimeInformation.IsOSPlatform(OSPlatform.Windows) = True Then osType = "Windows"
If RuntimeInformation.IsOSPlatform(OSPlatform.OSX) = True Then osType = "MacOSX"
If RuntimeInformation.IsOSPlatform(OSPlatform.Linux) = True Then osType = "Linux"
Console.WriteLine("Detecting " + osType + " environment")
Select Case LCase(actioN)
Case "help"
Console.WriteLine(vbCrLf + "Usage : ACTION --PARAM1 value --PARAM2 value >>>> Actions and parameters are not case sensitive")
Console.WriteLine("Example: oxcli getjson --api getapplications --file applist.json >>>> Performs 'getjson' action using param values of 'api' and 'file'")
Console.WriteLine("=======================================================================================================================================================")
Console.WriteLine(fLine("help", "shows this list of commands"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("checkme", "self-inspection of environment"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("setenv", "sets environment vars for Python API calls"))
Console.WriteLine(fLine("", "[REQUIRED] --KEY (OX API Key)"))
Console.WriteLine(fLine("", "[OPTIONAL] --KEY (OX API Key defaults to https://api.cloud.ox.security/api/apollo-gateway)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("policycsv", "create CSV of policies (requires that policy JSON files are saved manually)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("getjson", "uses python engine to pull API JSON args"))
Console.WriteLine(fLine("", "[REQUIRED] --API (name of API)"))
Console.WriteLine(fLine("", "[OPTIONAL] --file (output filename)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("getconnectors", "prints all connectors to screen or CSV"))
Console.WriteLine(fLine("", "[OPTIONAL] --FILE (name of CSV file to create)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("apptagsxls", "retrieves all APP TAGS and creates pivot Excel doc"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (name of XLS file to create)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("getapps", "prints all irrelevant apps to screen or CSV"))
Console.WriteLine(fLine("", "[OPTIONAL] --FILE (name of CSV file to create)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("getirrelevantapps", "prints all irrelevant apps to screen or CSV"))
Console.WriteLine(fLine("", "[OPTIONAL] --FILE (name of CSV file to create)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("issuesdetailed", "retrieves all issue data and creates pivot Excel doc"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (name of XLS file to create)"))
Console.WriteLine(fLine("", "[OPTIONAL] --CACHE false (by default, issues will be stored locally for caching)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("issuesxls", "retrieves all issues and creates pivot Excel doc"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (name of XLS file to create)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("issuescsv", "retrieves all issues and creates CSV doc"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (name Of CSV file To create)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("addtag", "adds a New tag - name must be unique"))
Console.WriteLine(fLine("", "[REQUIRED] --NAME (name Of the tag)"))
Console.WriteLine(fLine("", "[OPTIONAL] --DISPLAY (display name, defaults To same As --NAME), --TYPE (defaults To 'simple' - recommend default)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("edittags", "Loop through apps and Add/Remove tags using string and/or regex match"))
Console.WriteLine(fLine("", "[REQUIRED] --ADDTAG (tag displayname) OR --REMTAG (name)"))
Console.WriteLine(fLine("", "[OPTIONAL] --STR (app contains string), --REGEX (app matches regex), --COMMIT true (otherwise will only preview)"))
Console.WriteLine(fLine("", "[TEST] --MATCH (submit test app name) - will confirm string and/or regex match without looping through apps"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("devdetail", "Takes in JSON of Dev Detail and presents 30/90/180 commit stats, plus creates pivot"))
Console.WriteLine(fLine("", "[REQUIRED] --INFILE (filename of JSON)"))
Console.WriteLine(fLine("", "[OPTIONAL] --FILE (Excel report filename)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("tagfromxls", "Takes in EXCEL of columns with APP NAMES or APP IDs and their respective tag, adds TAGS to apps"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (XLS filename containing App and Tag info)"))
Console.WriteLine(fLine("", "[REQUIRED] --APPNAME (Excel column representing App Name)"))
Console.WriteLine(fLine("", "[REQUIRED] --TAG (Excel column representing TAG to apply)"))
Console.WriteLine(fLine("", "[OPTIONAL] --COMMIT (set to true to apply tags, otherwise test only)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("gatecheck", "Checks most recent results for HIGH+ vulns across all sources based on recent scan"))
Console.WriteLine(fLine("", "[REQUIRED] --APPNAME (Name of application to check)"))
Console.WriteLine(fLine("", "[REQUIRED] --APPNAME (Failcode to return eg 1 when HIGH+ encountered)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("jsonfromxls", "Creates JSON used to define mapping of integration projects -> OX applications"))
Console.WriteLine(fLine("", "[REQUIRED] --XLS (XLS filename containing App and Tag info)"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (Excel column representing App Name)"))
Console.WriteLine(fLine("", "[REQUIRED] --APPNAME (Excel column representing App Name)"))
Console.WriteLine(fLine("", "[REQUIRED] --MAP (Excel column representing App Name)"))
Console.WriteLine(fLine("", "[REQUIRED] -- MAPTYPE (Type of integration eg blackduck)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("readout", "Creates multiple documents for readout - requires MS Excel Powerpoint"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (Filenames of reports will begin with this prefix)"))
Console.WriteLine("-----------------")
Console.WriteLine(fLine("cvecsv", "Creates CSV of all CVEs"))
Console.WriteLine(fLine("", "[REQUIRED] --FILE (Filename of CSV output file)"))
Console.WriteLine("=======================================================================================================================================================")
End
Case "cvecsv"
Call cveList(args)
End
Case "gatecheck"
'Dim appId$ = argValue("appid", args)
Dim appName$ = argValue("appname", args)
Dim failCode$ = argValue("failcode", args)
Console.WriteLine(vbCrLf + "OX SECURITY GATE CHECK - WILL LOOK FOR RESULTS FROM RECENT SCAN FOR --appname SPECIFIED" + vbCrLf)
If appName = "" Then
Console.WriteLine("You must specify --appname")
Environment.ExitCode = 1
End
End If
Dim allIssues As List(Of issuesMedium) = New List(Of issuesMedium)
Console.WriteLine("Getting all issues High Severity & above for " + appName)
If failCode = "" Then
Console.WriteLine("No --failcode entered - this action will log vulnerabilities only")
Else
Console.WriteLine("--failcode provided - this action will return an exit code of " + failCode + " if issues exist in recent scheduled scan")
End If
'Console.WriteLine(vbCrLf)
Dim numFiles As Integer = getAllIssues(, "getIssuesMedium",, appName)
'Console.WriteLine(vbCrLf + vbCrLf + "Pulling " + numFiles.ToString + " files")
allIssues = buildMediumIssues("getIssuesMedium.json", numFiles)
'Console.WriteLine("# of issues: " + allIssues.Count.ToString)
Call consoleDump(allIssues)
If Len(failCode) And allIssues.Count > 0 Then
If Val(failCode) > 0 Then
Environment.ExitCode = Val(failCode)
Console.WriteLine(vbCrLf + "ERROR: EXIT CODE=" + failCode + " for " + allIssues.Count.ToString + " vulnerabilities")
End If
End If
End
Case "readout"
Call readoutSub(args)
End
Case "tagfromxls"
Call tagFromXLS(args)
End
Case "getirrelevantapps"
Dim allApps As List(Of oxAppIrrelevant) = New List(Of oxAppIrrelevant)
allApps = getAppListIrrelevant()
Console.WriteLine("# Of Apps: " + allApps.Count.ToString)
Dim fileN$ = argValue("file", args)
Dim csV$ = ""
Dim allReasons As Collection = New Collection
For Each I In allApps
For Each R In I.irrelevantReasons
If grpNDX(allReasons, R) = 0 Then allReasons.Add(R)
Next
Next
For Each I In allApps
Dim lDate$ = ""
Dim aName$ = ""
Dim iReason$ = ""
aName = I.appName + " [" + I.appId + "]"
lDate = CStr(jStoDate(CLng(I.lastCodeChange)))
For Each reasoN In allReasons
Dim foundReason As Boolean = False
For Each R In I.irrelevantReasons
If reasoN = R Then foundReason = True
Next
If foundReason = True Then
iReason += "1,"
Else
iReason += ","
End If
Next
iReason = Mid(iReason, 1, Len(iReason) - 1)
If fileN = "" Then
Console.WriteLine(aName + spaces(60 - Len(aName)) + lDate + spaces(25 - Len(lDate)) + iReason)
Else
csV += qT(I.appName) + "," + I.appId + "," + I.link + "," + lDate + "," + iReason + vbCrLf
End If
Next
If Len(csV) Then
safeKILL(fileN)
Dim hdR$ = "APP_NAME,APP_ID,LINK,LAST_CHANGE,"
'IRRELEVANT_REASON" + vbCrLf
For Each rsN In allReasons
hdR += rsN + ","
Next
hdR = Mid(hdR, 1, Len(hdR) - 1) + vbCrLf
safeKILL(fileN)
streamWriterTxt(fileN, hdR + csV)
Console.WriteLine("File written to " + fileN)
End If
End
Case "jsonfromxls"
Call createMappingJSON(args)
End
Case "strcompare"
Dim matcH As Single = 0
matcH = GetSimilarity("four score and seven years ago", "for scor and sevn yeres ago")
Console.WriteLine("Match = " + matcH.ToString)
End
Case "gitlab_tag_groups"
Dim reportOnly As Boolean = False
If LCase(argValue("reportonly", args)) = "true" Then reportOnly = True
Dim allApps As List(Of oxAppshort) = New List(Of oxAppshort)
If reportOnly = False Then
allApps = getAppListShort()
Console.WriteLine("# of Applications: " + allApps.Count.ToString)
End If
Dim fileJSON$ = argValue("glabjson", args)
Dim fileN$ = argValue("file", args)
Dim csV$ = ""
Dim glabRepos As List(Of glabRepo) = New List(Of glabRepo)
glabRepos = OX.returnGitLabRepos(fileJSON)
Console.WriteLine("# of OX Apps: " + allApps.Count.ToString)
Console.WriteLine("# of GitLab JSON Repos: " + glabRepos.Count.ToString)
'For Each gL In glabRepos
'Console.WriteLine(gL.name_with_namespace + "," + gL.name + "," + gL.namespace.name)
'Next
csV = "OX_ID,OX_APP_NAME,TAG,NS_KIND,WEB_URL,COUNT,EXCEPTION" + vbCrLf
For Each oApp In allApps
If Mid(oApp.appName, 1, 1) = "*" Then GoTo skipFakeApp
' If InStr(oApp.appName, "terraform-okta-group") > 0 Then
' Dim K As Integer
' K = 12
' End If
Dim cLine$ = qT(oApp.appId) + "," + qT(oApp.appName) + ","
Dim numEntries As Integer = 0
Dim groupsString$ = ""
For Each gL In glabRepos
' old way, resulted in numerous instances of no match or multiple matches
' If oApp.appName = gL.name Then
' groupsString += gL.namespace.name + ","
' numEntries += 1
' End If
' new way
' match OX link to GL web url
If oApp.link = gL.web_url Then
groupsString += qT(gL.namespace.name) + "," + gL.namespace.kind + ","
numEntries += 1
End If
Next
'If Len(groupsString) Then
' If Mid(groupsString, Len(groupsString), 1) = "," Then groupsString = Mid(groupsString, 1, Len(groupsString) - 1)
'End If
If groupsString = "" Then groupsString = "N/A,N/A,"
cLine += groupsString + qT(oApp.link) + ",1"
If numEntries = 0 Then cLine += ",NO EXACT MATCH"
If numEntries > 1 Then cLine += ",MORE THAN ONE MATCH"
csV += cLine + vbCrLf
Console.WriteLine(cLine)
skipFakeApp:
Next
Call safeKILL(fileN)
Call streamWriterTxt(fileN, csV)
End
Case "devdetail"
Call devDetailXLS(args)
End
Case "getapps"
Dim allApps As List(Of oxAppshort) = getAppListShort()
Console.WriteLine("# of Applications: " + allApps.Count.ToString)
Dim fileN$ = argValue("file", args)
Dim csV$ = ""
Dim maxNumTags As Integer = 0
For Each I In allApps
Dim aName$ = ""
Dim tagCsv$ = ""
aName = I.appName + " [" + I.appId + "]"
For Each R In I.tags
tagCsv$ += R.displayName + ","
Next
If I.tags.Count > maxNumTags Then maxNumTags = I.tags.Count
tagCsv$ = Mid(tagCsv$, 1, Len(tagCsv$) - 1)
If fileN = "" Then
Console.WriteLine(aName + spaces(60 - Len(aName)) + I.tags.Count.ToString + spaces(10) + tagCsv$)
Else
csV += I.appName + "," + I.appId + "," + tagCsv + vbCrLf
End If
Next
If Len(csV) Then
Dim hdR$ = "APP_NAME,APP_ID,"
Dim K As Integer
For K = 1 To maxNumTags
hdR += "TAG_" + K.ToString + ","
Next
hdR = Mid(hdR, 1, Len(hdR) - 1) + vbCrLf
Console.WriteLine("HEADERS:" + vbCrLf + hdR)
safeKILL(fileN)
streamWriterTxt(fileN, hdR + csV)
Console.WriteLine("File written to " + fileN)
End If
End
Case "getconnectors"
Dim allConnections As List(Of connectorFamily) = New List(Of connectorFamily)
allConnections = getOxConnectors()
Dim isConfig As Boolean = False
If LCase(argValue("configonly", args)) = "true" Then isConfig = True
Dim fileN$ = argValue("file", args)
Dim csV$ = ""
Dim cLine$ = ""
cLine = qT("FAMILY") + "," + qT("NAME") + vbCrLf ' + "," + qT("DESCRIPTION") + "," + qT("CREDENTIAL_TYPES") + vbCrLf
Console.WriteLine(cLine)
For Each F In allConnections
Dim aLine$ = ""
For Each CONN In F.connectors
If isConfig = True And CONN.connector.isConfigured = False Then GoTo skip
aLine = qT(F.familyDisplayName) + ","
Dim C As oxConnection = CONN.connector
aLine += qT(C.displayName) ' + "," + qT(C.description)
'Dim creD$ = ""
'If C.credentialsTypes IsNot Nothing Then
' If C.credentialsTypes.Count Then
' For Each CT In C.credentialsTypes
' creD += CT + ","
' Next
' End If
'End If
'aLine += "," + qT(creD) ' + vbCrLf
Console.WriteLine(aLine)
cLine += aLine + vbCrLf
skip:
Next
'cLine += aLine + vbCrLf
'Console.WriteLine(aLine)
Next
If Len(fileN) Then streamWriterTxt(fileN, cLine)
End
Case "checkme"
' removed folder structure due to nuances between different versions of *NIX (tested good on MacOS & Windows only, failed on DEBIAN BOOKWORM and BULLSEYE)
Dim findFile$ = Path.Combine(ogDir, "Newtonsoft.Json.dll")
Console.WriteLine("File System check - filesystem.curdir: " + ogDir)
Console.WriteLine("Path.GetFullPath(Directory.GetCurrentDirectory()= " + Path.GetFullPath(Directory.GetCurrentDirectory()))
If IO.File.Exists(findFile) = True Then
Console.WriteLine("OXcli files present")
Else
Console.WriteLine("OX dependencies missing - check folder contents or download new version")
End
End If
FileSystem.ChDir(pyDir)
If IO.File.Exists(".env") Then
Console.WriteLine("Environment file exists - credentials not verified")
Else
Console.WriteLine("Environment file (.env) is not present and is needed for credentials")
End If
Console.WriteLine("Python directory: " + pyDir)
If IO.File.Exists("python_examp.py") Then
Console.WriteLine("Python executable exists")
Else
Console.WriteLine("Python script to call APIs must be present - obtain python folder that accompanies this DOTNET executable")
End
End If
' Console.WriteLine("Path.GetFullPath(Directory.GetCurrentDirectory()= " + Path.GetFullPath(Directory.GetCurrentDirectory()))
FileSystem.ChDir(ogDir)
Console.WriteLine("Changing back to parent folder - " + ogDir)
If IO.File.Exists(findFile) = True Then
Console.WriteLine("Environment check complete")
Else
Console.WriteLine("Unable to revert to previous folder")
End
End If
End
Case "setenv"
Dim urL$ = argValue("url", args)
Dim apiKey$ = argValue("key", args)
If apiKey = "" Then
Console.WriteLine("To set environment, OXCLI needs the URL and API KEY. Submit using --URL and --KEY params")
End If
If urL = "" Then
urL = "https://api.cloud.ox.security/api/apollo-gateway"
Console.WriteLine("Will default to https://api.cloud.ox.security/api/apollo-gateway")
End If
Dim newEfile$ = "oxUrl='" + urL + "'" + Chr(13) + "oxKey='" + apiKey + "'"
FileSystem.ChDir(pyDir)
'Console.WriteLine("Checking for " + Path.Combine(pyDir, ".env"))
If RuntimeInformation.IsOSPlatform(OSPlatform.Windows) <> True Then
If IO.File.Exists(Path.Combine(pyDir, ".env")) = True Then
Console.WriteLine("On *NIX systems, either delete /python/.env and run this command again or edit your existing /python/.env file to keep only the latest values")
End If
Else
safeKILL(".env")
End If
Call streamWriterTxt(".env", newEfile)
Console.WriteLine("New environment variables set for " + urL)
FileSystem.ChDir(ogDir)
End
Case "dvusage_beta_filters"
Dim fileN As Collection = New Collection
fileN.Add("DV0523F")
fileN.Add("DV0623F")
fileN.Add("DV0723F")
fileN.Add("DV0823F")
fileN.Add("DV0923F")
fileN.Add("DV1023F")
fileN.Add("DV1123F")
fileN.Add("DV1223F")
fileN.Add("DV0124F")
' manual stuff
fileN = New Collection
fileN.Add("sbpoc.json")
OX = New oxWrapper("", "")
Dim fullCSV$ = ""
Dim writeFileN$ = ""
writeFileN = argValue("file", args)
If writeFileN = "" Then writeFileN = "sbusagecsv.csv"
Dim detailLvl$
detailLvl = argValue("detail", args)
If detailLvl = "" Then detailLvl = "summary"
For Each J In fileN
Dim jsoN$ = streamReaderTxt(J)
Dim oxFiltersForMonth As oxUserLogFilter = OX.getUserFilterEntries(jsoN)
With oxFiltersForMonth
Console.WriteLine(J + " logTypes: " + .logTypes.Count.ToString + " logNames: " + .logNames.Count.ToString + " emails: " + .userEmails.Count.ToString)
Dim newS$ = "2023-" + Mid(J, 3, 2) + ",ACTIVITY_TYPE,"
For Each L In .logTypes
If L.label <> "" Then fullCSV += newS + L.label + "," + L.count.ToString + vbCrLf
Next
newS$ = "2023-" + Mid(J, 3, 2) + ",ACTIVITY,"
For Each A In .logNames
If A.label <> "" Then fullCSV += newS + A.label + "," + A.count.ToString + vbCrLf
Next
newS$ = "2023-" + Mid(J, 3, 2) + ",USER_ACTIVITY,"
For Each A In .userEmails
If InStr(A.label, "double") > 0 Then fullCSV += newS + A.label + "," + A.count.ToString + vbCrLf
Next
End With
Next
streamWriterTxt(writeFileN, fullCSV)
End
Case "dvusage_beta_actions"
' This does not work as requests from browser are paged 50x
Console.WriteLine("Action requires API not yet exposed")
Dim fileN As Collection = New Collection
fileN.Add("DV0523A")
fileN.Add("DV0623A")
fileN.Add("DV0723A")
fileN.Add("DV0823A")
fileN.Add("DV0923A")
fileN.Add("DV1023A")
fileN.Add("DV1123A")
fileN.Add("DV1223A")
fileN.Add("DV0124A")
' manual stuff
fileN = New Collection
fileN.Add("sbpoc.json")
Dim fullList As List(Of oxUserLogEntry) = New List(Of oxUserLogEntry)
OX = New oxWrapper("", "")
For Each J In fileN
'If File.Exists(J) = False Then Console.WriteLine("Does not exist")
Dim jsoN$ = streamReaderTxt(J)
'Console.WriteLine(jsoN)
Dim monthList As List(Of oxUserLogEntry) = New List(Of oxUserLogEntry)
monthList = OX.getUserLogEntries(jsoN)
For Each A In monthList
fullList.Add(A)
Next
Console.WriteLine("Items in this list: " + monthList.Count.ToString)
Console.WriteLine("Total number items: " + fullList.Count.ToString)
Next
End
Case "policycsv"
Call policyCSV()
End
Case "addtag"
Call addTag(argValue("name", args), argValue("display", args), argValue("type", args))
End
Case "edittags"
Call editTags(args)
End
Case "getjson"
Dim apiCall$ = argValue("api", args)
Dim fileN$ = argValue("file", args)
If Len(apiCall) = 0 Then
Console.WriteLine("This command's parameters: getjson --api apiname --file 'file name.json'" + vbCrLf + "api [req] : the name of the API call (action getAPIs)" + vbCrLf + "file [opt]: the output filename to dump JSON")
End
End If
If Len(fileN) = 0 Then
Console.WriteLine(setUpAPICall(apiCall, "", True))
Else
Call setUpAPICall(apiCall, fileN)
Console.WriteLine("File created: " + Path.Combine(ogDir, fileN))
End If
End
Case "apptagsxls"
If LCase(actioN) = "apptagsxls" And osType <> "Windows" Then
Console.WriteLine("This command will only work on a Windows machine with Excel locally installed")
End
End If
Dim toFilename$ = argValue("file", args)
toFilename = Path.Combine(ogDir, toFilename)
Dim allApps As List(Of oxAppshort) = New List(Of oxAppshort)
allApps = getAppListShort()
Console.WriteLine("# of Applications: " + allApps.Count.ToString)
Call appTagRPT(allApps, toFilename)
End
Case "issuesdetailed"
' If osType <> "Windows" Then
'Console.WriteLine("This command will only work on a Windows machine with Excel locally installed")
'nd
'End If
Dim toFilename$ = argValue("file", args)
If Len(toFilename) = 0 Then
Console.WriteLine("This command's parameters: issuesdetailed --file 'filename.xlsx'" + vbCrLf + "file : the output Excel filename.")
Console.WriteLine("You must specify a filename for the Excel .xlsx - if you do not have Excel, issues will still be stored unless --cache false")
End
End If
Dim allIssues As List(Of issueS)
Dim loadList As Boolean = False
If LCase(argValue("loadlist", args)) = "true" Then loadList = True
If loadList = True Then
Call getAllIssues(True)
Else
allIssues = loadCachedList()
If allIssues.Count Then GoTo gotCached
End If
If loadList = False Then Call getAllIssues(True) ' this means tried to load cache and nothing there
allIssues = buildShortIssues("getIssuesShort.json", numResponseFiles - 1)
gotCached:
toFilename = Path.Combine(ogDir, toFilename)
fileNames = New List(Of String)
' building cache
Dim numCache As Long = 0
Dim numWrite As Long = 0
For Each I In allIssues
Dim cFile$ = I.issueId + ".json"
cFile = safeFilename(cFile)
' opportunity here to capture new scanIDs
If IO.File.Exists(Path.Combine(cacheDir, cFile)) = True Then
' Console.WriteLine("Found Issue cached -> " + cFile)
numCache += 1
GoTo skipthisone
End If
Call setIssueReqVars(I.issueId)
tryAgain:
Dim newJson$ = setUpAPICall("getSingleIssue",, True, True)
If newJson = "ERROR" Then
Console.WriteLine("Trying again in 15 minutes")
Sleep(900000)
GoTo tryAgain
End If
'Console.WriteLine(newJson)
Console.WriteLine(numCache.ToString + "/" + allIssues.Count.ToString + " " + I.issueId + " -----> " + cFile)
Call saveJSONtoFile(newJson, Path.Combine(cacheDir, cFile))
numWrite += 1
skipthisone:
fileNames.Add(Path.Combine(cacheDir, cFile))
Next
Console.WriteLine(vbCrLf + "# of Objects : " + allIssues.Count.ToString)
Console.WriteLine("# New Objects : " + numWrite.ToString)
Console.WriteLine("Objects Cached : " + numCache.ToString)
issuesCache = New List(Of singleIssue)
Dim issuesJSON As List(Of String)
issuesJSON = New List(Of String)
GoTo noMoreThreading
Call processCache() ', issuesCache)
Console.WriteLine("Back into main process")
GC.Collect()
Console.WriteLine("# IssuesCache " + issuesCache.Count.ToString + " files..")
Console.WriteLine("Cancelling lost threads")
issueCacheLoader.CancelAsync()
issueCacheLoader = New BackgroundWorker
GC.Collect()
Console.WriteLine("Grabbing remaining items")
Dim newfilenameS As List(Of String) = New List(Of String)
For Each I In issuesCache
Dim checkFile$ = Path.Combine(cacheDir, safeFilename(I.issueId) + ".json")
If ndxFilenames(checkFile) = -1 Then
Dim nD As JObject = JObject.Parse(streamReaderTxt(checkFile))
Dim newI As singleIssue = New singleIssue
newI = JsonConvert.DeserializeObject(Of singleIssue)(nD.SelectToken("data").SelectToken("getSingleIssueInfo").ToString)
issuesCache.Add(newI)
Console.WriteLine("Added " + newI.issueId)
End If
Next
noMoreThreading:
Dim pCnt As Integer = 0
For Each F In fileNames
pCnt += 1
issuesJSON.Add(streamReaderTxt(F))
If pCnt Mod 1000 = 0 Then
Thread.Sleep(10)
Console.WriteLine(CStr(Now.ToString("hh\:mm\:ss\:ff")) + "> Progress: Read " + pCnt.ToString + " files")
Thread.Sleep(10)
End If
Next
Console.WriteLine(vbCrLf + "Deserializing " + issuesJSON.Count.ToString + " JSON strings")
pCnt = 0
For Each S In issuesJSON
pCnt += 1
Dim nD As JObject = JObject.Parse(S)
Dim newI As singleIssue = New singleIssue
newI = JsonConvert.DeserializeObject(Of singleIssue)(nD.SelectToken("data").SelectToken("getSingleIssueInfo").ToString)
If IsNothing(newI) = True Then
Console.WriteLine("Skipping NDX " + (pCnt - 1).ToString + " -> file > " + fileNames(pCnt - 1))
GoTo skipThatOne
End If
issuesCache.Add(newI)
If pCnt Mod 5000 = 0 Then
Thread.Sleep(10)
Console.WriteLine(CStr(Now.ToString("hh\:mm\:ss\:ff")) + "> Progress: Deserialized " + pCnt.ToString + " JSONs")
Thread.Sleep(10)
End If
skipThatOne:
Next
'fileNames = Nothing
issuesJSON = Nothing
GC.Collect()
Console.WriteLine("Freeing up memory - sending to report construction" + vbCrLf)
Thread.Sleep(1000)
Console.WriteLine("# IssuesCache " + issuesCache.Count.ToString + " single issue objects..")
Console.WriteLine("Item 1: " + issuesCache(0).issueId + vbCrLf + "Last : " + issuesCache(issuesCache.Count - 1).issueId)
' If numCache > issuesCache.Count Then
' Call processCache()
' End If
' Call issueDetailRpt(issuesCache, toFilename)
End
Case "issuesxls", "issuescsv"
If LCase(actioN) = "issuesxls" And osType <> "Windows" Then
Console.WriteLine("This command will only work on a Windows machine with Excel locally installed")
End
End If
Dim toFilename$ = argValue("file", args)
If Len(toFilename) = 0 Then
Console.WriteLine("This command's parameters: issuesxls OR issuescsv --file 'filename.xlsx'" + vbCrLf + "file : the output Excel filename.")
Console.WriteLine("You must specify a filename for the CSV or Excel .xlsx.")
End
End If
Call getAllIssues()
Dim allIssues As List(Of issueS)
allIssues = buildIssues("getIssues.json", numResponseFiles - 1)
toFilename = Path.Combine(ogDir, toFilename)
If LCase(actioN) = "issuesxls" Then
Call issueRpt(allIssues, toFilename)
Else
Call issueCSV(allIssues, toFilename)
End If
End Select
End
End Function
Public Function addTag(tagName$, Optional ByVal dName$ = "", Optional ByVal tType$ = "simple") As String
addTag = "" ' returns empty if unsuccessful otherwise tagid of new tag
If dName = "" Then dName = tagName
If tType = "" Then tType = "simple"
Console.WriteLine("Adding tag:")
Call setAddTagVars(tagName, dName, tType)
Thread.Sleep(1000)
Dim jSon$ = setUpAPICall("addTag",, True)
addTag = OX.getTagId(jSon)
If addTag = "" Then
Console.WriteLine("ERROR: Could not add tag - return JSON=" + vbCrLf + jSon)
Else
Console.WriteLine("New Tag: " + tagName + " >> TagID: " + addTag)
End If
End Function
Public Sub editTags(args() As String)
Dim doRegExMatch As Boolean = False
Dim toMatch$ = argValue("match", args)
Dim regX As Regex
Dim regXmatch As Match
Dim matchStr$ = argValue("str", args)
Dim testingOnly As Boolean = False
Dim addedTag$ = argValue("addtag", args)
Dim remTag$ = argValue("remtag", args)
Dim addRepoTag$ = argValue("repotag", args)
Dim repoOnlyTag As Boolean = False
If Len(addRepoTag) Then repoOnlyTag = True
Dim newModTag As editTagsRequestVARS = New editTagsRequestVARS
Dim commitChanges As Boolean = False
If LCase(argValue("commit", args)) = "true" Then commitChanges = True
If Len(toMatch) > 0 Then
testingOnly = True
Console.WriteLine("STR=" + matchStr + " TOMATCH=" + toMatch)
End If
If Len(argValue("regex", args)) Then
doRegExMatch = True
regX = New Regex(argValue("regex", args))
Console.WriteLine("Performing REGEX matching using " + qT(argValue("regex", args)))
If testingOnly Then
Console.WriteLine("Testing match on " + qT(toMatch) + " using Regular Expression: " + qT(argValue("regex", args)))
regXmatch = regX.Match(toMatch)
Console.WriteLine("REGX_MATCH: " + CStr(regXmatch.Success) + " VALUE: " + regXmatch.Value)
End If
End If
Dim doStringMatch As Boolean = False
If Len(matchStr) Then
doStringMatch = True
Console.WriteLine("Performing STRING matching using " + qT(matchStr))
If testingOnly Then
Console.WriteLine("Testing match on " + qT(toMatch) + " by looking for string: " + qT(matchStr))
If InStr(toMatch, matchStr, CompareMethod.Text) Then
Console.WriteLine("STR_MATCH: True ")
Else
Console.WriteLine("MATCH: False ")
End If
End If
End If
If repoOnlyTag = True Then
Console.WriteLine("Will apply tag '" + addRepoTag + "' to all applications defined as repo folders")
End If
If testingOnly = True Then
End
End If
Console.WriteLine("Getting applications")
Dim appsWithTag As Integer = 0
Dim allAppsWithTag As Integer = 0
Dim allApps As List(Of oxAppshort) = getAppListShort()
Console.WriteLine("# of Applications: " + allApps.Count.ToString)
Dim allTags As List(Of oxTag) = getAllTags()
Console.WriteLine("# of Tags: " + allTags.Count.ToString)
If repoOnlyTag = True Then addedTag = addRepoTag
Dim tId$ = OX.returnTagId(addedTag, allTags)
If tId = "" Then
Console.WriteLine("This tag must be created before it can be applied")
If commitChanges = True Then tId = addTag(addedTag)
If tId = "" And commitChanges = True Then
Console.WriteLine("ERROR:Could not add this tag - exiting without changes")
End
Else
newModTag.addedTagsIds.Add(tId)
End If
Else
Console.WriteLine("Found TAG: " + tId)
newModTag.addedTagsIds.Add(tId)
End If
' for now - remTAG needs to be accounted for.. Is API smart enough to ignore REMOVE commands when TAG doesnt exist in first place?
' May need to separate ADD and REMOVE and separate operations, although API appears to account for both across multiple apps with a single call
If newModTag.addedTagsIds.Count + newModTag.removedTagsIds.Count = 0 Then
Console.WriteLine("You must either add or remove a tag for this operation to run, using --addtag and/or --remtag")
End
End If
Console.WriteLine(vbCrLf + "These applications to receive new tags:" + vbCrLf)
Console.WriteLine(fLine("Application Name" + spaces(44), "Link" + spaces(76) + "# Tags"))
Console.WriteLine(fLine("================" + spaces(44), "====" + spaces(76) + "======"))
For Each app In allApps
Dim addTag As Boolean = True
If doRegExMatch = True And addTag = True Then
regXmatch = regX.Match(app.appName)
If regXmatch.Success = False Then addTag = False
End If
If doStringMatch = True And addTag = True Then
If InStr(app.appName, matchStr, CompareMethod.Text) = 0 Then addTag = False
End If
If repoOnlyTag = True Then
'Console.WriteLine("Checking " + app.appName)
If Mid(app.appName, 1, 1) = "*" Then addTag = False
End If
If app.tagExist(, addedTag) Then
allAppsWithTag += 1
If addTag = True Then
appsWithTag += 1
addTag = False
End If
End If
' not yet - later ' If app.tagExist(, remTag) Then
If addTag Then
Console.WriteLine(fLine(app.appName + spaces(60 - Len(app.appName)), app.link + spaces(80 - Len(app.link)) + app.tags.Count.ToString))