/
SourceCodeAnalyzer.cs
149 lines (122 loc) · 4.54 KB
/
SourceCodeAnalyzer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace Scat
{
public class SourceCodeAnalyzer
{
public string SourceCodeDirectory
{
get;
private set;
}
private Action<SourceCodeAnalyzer> completed;
private Action<string> debug;
public List<string> SourceFiles
{
get;
private set;
}
public SourceCodeAnalyzer(string sourceCodeDirectory, Action<SourceCodeAnalyzer> completed, Action<string> debug)
{
if(string.IsNullOrEmpty(sourceCodeDirectory))
{
throw new ArgumentNullException("sourceCodeDirectory");
}
SourceCodeDirectory = sourceCodeDirectory;
this.completed = completed;
this.debug = debug;
this.SourceFiles = new List<string>();
this.FileAnalyzers = new List<FileAnalyzer>();
this.Vulnerabilities = new List<IVulnerability>();
}
private static bool filtered(string filename)
{
bool retval = false;
string lwr = filename.ToLower();
if (lwr.Contains("crm"))
{
if (lwr.Contains("qa") || lwr.Contains("test") || lwr.Contains("tool"))
{
retval = true;
}
}
return retval;
}
private void EnumerateFiles()
{
this.debug("Discovering source files.");
IEnumerable<string> hFiles = Directory.EnumerateFiles(this.SourceCodeDirectory, "*.h", SearchOption.AllDirectories);
IEnumerable<string> cppFiles = Directory.EnumerateFiles(this.SourceCodeDirectory, "*.cpp", SearchOption.AllDirectories);
IEnumerable<string> csFiles = Directory.EnumerateFiles(this.SourceCodeDirectory, "*.cs", SearchOption.AllDirectories);
IEnumerable<string> aspxFiles = Directory.EnumerateFiles(this.SourceCodeDirectory, "*.aspx", SearchOption.AllDirectories);
IEnumerable<string> sqlFiles = Directory.EnumerateFiles(this.SourceCodeDirectory, "*.sql", SearchOption.AllDirectories);
IEnumerable<string> allFiles = csFiles.Union(aspxFiles).Union(hFiles).Union(cppFiles).Union(sqlFiles);
foreach (string sourceFile in allFiles)
{
if (!filtered(sourceFile))
{
this.SourceFiles.Add(sourceFile);
}
}
this.debug("Finished discovering source files. Discovered: " + this.SourceFiles.Count);
}
public List<FileAnalyzer> FileAnalyzers
{
get;
private set;
}
public List<IVulnerability> Vulnerabilities
{
get;
private set;
}
public void OnThread()
{
this.EnumerateFiles();
this.debug("Loading source files...");
foreach (string file in this.SourceFiles)
{
if (!filtered(file))
{
FileAnalyzers.Add(new FileAnalyzer(file));
}
}
this.debug("Finished loading source files (" + FileAnalyzers.Count + " loaded).");
this.debug("Starting file analysis...");
Parallel.ForEach(FileAnalyzers, currentAnalyzer =>
{
currentAnalyzer.Analyze();
});
this.debug("Finished file analysis.");
this.debug("Aggregating results...");
foreach (var analyzer in FileAnalyzers)
{
foreach (var vuln in analyzer.Vulnerabilities)
{
this.Vulnerabilities.Add(vuln);
}
}
this.debug("Finished aggregating results.");
//
//
//
this.debug("Starting experimental GlobalScaryRule test code...");
List<IRule> globalRules = new List<IRule>();
globalRules.Add(new GlobalScaryCsRule(this.FileAnalyzers, this.debug));
globalRules.Add(new TraceGlobalRule(this.FileAnalyzers, this.debug));
foreach (IRule r in globalRules)
{
List<IVulnerability> vulns = r.Test();
foreach (var v in vulns)
{
this.Vulnerabilities.Add(v);
}
}
this.completed(this);
}
}
}