-
Notifications
You must be signed in to change notification settings - Fork 9
/
Program.cs
207 lines (173 loc) · 7.05 KB
/
Program.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
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
/*
* .Net MVC Enumerator
* revision 1.0 2015-07-30
* author: Priyank Nigam, Gotham Digital Science
* contact: labs@gdssecurity.com
* blog post:
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DotNetMVCEnumerator.source;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using System.Data;
namespace DotNetMVCEnumerator
{
static class Program
{
private static void Main(string[] args)
{
string attribute = "";
string csvOutputFile = "";
string directoryToScan = "";
string negativeSearch = "";
Dictionary<String, List<Result>> results = new Dictionary<string, List<Result>>();
try
{
var options = new Options();
bool isFlagValid = options.OptionParser(args, out csvOutputFile, out attribute, out directoryToScan, out negativeSearch);
// If Command-line options not set correctly, show default message and exit
if (!isFlagValid)
{
System.Environment.Exit(0);
}
if(String.IsNullOrEmpty(csvOutputFile))
{
DateTime dt = DateTime.Now;
String timestamp = dt.ToString("yyyyMMddHHmmss");
csvOutputFile = "enumerated_controllers_"+timestamp+".csv";
}
string curDir = Directory.GetCurrentDirectory();
if (String.IsNullOrEmpty(directoryToScan))
{
directoryToScan = curDir;
}
string[] paths = Directory.GetFiles(directoryToScan, "*.cs", SearchOption.AllDirectories);
if (paths.Length > 0)
{
foreach (var path in paths)
{
using (var stream = File.OpenRead(path))
{
var tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
SyntaxNode root = tree.GetRoot();
// Check if the Class inherits Apicontroller or Controller and print out all the public entry points
ControllerChecker controllerchk = new ControllerChecker();
if (controllerchk.inheritsFromController(root, attribute))
{
controllerchk.enumerateEntrypoints(root, attribute, negativeSearch, path, results);
}
}
}
string[] controllerPaths = results.Keys.ToArray();
String pathToTrim = getPathToTrim(controllerPaths);
if (!String.IsNullOrEmpty(attribute) || !String.IsNullOrEmpty(negativeSearch))
{
printCommandLineResults(results, pathToTrim);
}
printCSVResults(results, csvOutputFile, pathToTrim);
}
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("Invalid Path");
}
catch (IndexOutOfRangeException)
{
//Shoudn't Reach this, but in case
Console.WriteLine("No Arguments passed");
}
catch (UnauthorizedAccessException e)
{
e.GetBaseException();
Console.WriteLine("You do not seem to have appropiate Permissions on this direcctory");
}
catch (NotSupportedException)
{
Console.WriteLine("The operating system is Windows CE, which does not have current directory functionality.");
}
catch (ArgumentException)
{
Console.WriteLine("Illegal characters passed as arguments! ");
}
catch(Exception)
{
Console.WriteLine("Unexpected error");
}
}
public static void printCommandLineResults(Dictionary<String, List<Result>> results, String pathToTrim)
{
foreach( KeyValuePair<String, List<Result>> pair in results)
{
Console.WriteLine("Controller: \n" + pair.Key.Replace(pathToTrim, ""));
Console.WriteLine("\nMethods: ");
foreach(Result result in pair.Value)
{
Console.WriteLine(result.MethodName);
}
Console.WriteLine("\n======================\n");
}
}
public static void printCSVResults(Dictionary<String, List<Result>> results, String filename, String pathToTrim)
{
var csvExport = new CsvExport();
foreach (KeyValuePair<String, List<Result>> pair in results)
{
foreach (Result result in pair.Value)
{
csvExport.AddRow();
csvExport["Controller"] = pair.Key.Replace(pathToTrim, "");
csvExport["Method Name"] = result.MethodName;
csvExport["Route"] = result.Route;
csvExport["HTTP Method"] = string.Join(", ", result.HttpMethods.ToArray());
csvExport["Attributes"] = string.Join(", ", result.Attributes.ToArray());
}
}
File.Create(filename).Dispose();
csvExport.ExportToFile(filename);
Console.WriteLine("CSV output written to: " + filename);
}
public static String getPathToTrim(String[] paths)
{
string pathToTrim = "";
try
{
String aPath = paths.First();
string[] dirsInFilePath = aPath.Split('\\');
int highestMatchingIndex = 0;
for (int i = 0; i < dirsInFilePath.Length; i++)
{
for (int j = 0; j < paths.Length; j++)
{
string[] splitPath = paths[j].Split('\\');
if (!dirsInFilePath[i].Equals(splitPath[i]))
{
highestMatchingIndex = i - 1;
break;
}
}
}
if (highestMatchingIndex == 0)
{
pathToTrim = "." + "\\";
}
else
{
pathToTrim = string.Join("\\", dirsInFilePath, 0, highestMatchingIndex);
}
}
catch (InvalidOperationException)
{
Console.WriteLine("No Entrypoints with the specified search parameters found.");
}
catch( Exception e)
{
Console.WriteLine(e.ToString());
}
return pathToTrim;
}
}
}