노현종

크리티컬 변수 선정시 예약어 제외 기능 추가

1 +auto
2 +bool
3 +break
4 +case
5 +char
6 +const
7 +continue
8 +default
9 +defined
10 +do
11 +double
12 +else
13 +enum
14 +extern
15 +float
16 +for
17 +goto
18 +if
19 +int
20 +long
21 +register
22 +return
23 +short
24 +signed
25 +sizeof
26 +static
27 +struct
28 +switch
29 +typedef
30 +union
31 +unsigned
32 +void
33 +volatile
34 +while
...\ No newline at end of file ...\ No newline at end of file
...@@ -67,16 +67,14 @@ namespace VulnCrawler ...@@ -67,16 +67,14 @@ namespace VulnCrawler
67 public static void Run() { 67 public static void Run() {
68 // Repository 폴더들이 있는 주소를 지정하면 하위 폴더 목록을 가져옴(Repository 목록) 68 // Repository 폴더들이 있는 주소를 지정하면 하위 폴더 목록을 가져옴(Repository 목록)
69 69
70 - // var fields = VulnWorker.GetCriticalVariant(@"return _is_safe_url(url, host) and _is_safe_url(url.replace('\\', '/'), host)"); 70 + // var fields = VulnWorker.GetCriticalVariant(@"return _is_safe_url(url, host) and _is_safe_url(url.replace('\\', '/'), host)");
71 - var fields = VulnAbstractCrawler.GetCriticalVariant(@"if(i + inl < bl) {"); 71 + var c = new VulnC();
72 - 72 + var fields = c.GetCriticalVariant(@"if(i + inl < bl) {");
73 -
74 foreach (var item in fields) 73 foreach (var item in fields)
75 { 74 {
76 Console.WriteLine(item); 75 Console.WriteLine(item);
77 } 76 }
78 - 77 + return;
79 - // return;
80 var directorys = Directory.GetDirectories(@"c:\VulnPy"); 78 var directorys = Directory.GetDirectories(@"c:\VulnPy");
81 if (directorys.Length == 0) { 79 if (directorys.Length == 0) {
82 Console.WriteLine("Repository 목록 찾기 실패"); 80 Console.WriteLine("Repository 목록 찾기 실패");
......
...@@ -14,8 +14,8 @@ namespace VulnCrawler ...@@ -14,8 +14,8 @@ namespace VulnCrawler
14 public abstract class VulnAbstractCrawler 14 public abstract class VulnAbstractCrawler
15 { 15 {
16 16
17 - protected abstract string[] ReservedList { get; } 17 + protected HashSet<string> ReservedList { get; }
18 - 18 + protected abstract string ReservedFileName { get; }
19 // = { "if", "return", "break", "while", "typedef" }; 19 // = { "if", "return", "break", "while", "typedef" };
20 20
21 /// <summary> 21 /// <summary>
...@@ -26,15 +26,39 @@ namespace VulnCrawler ...@@ -26,15 +26,39 @@ namespace VulnCrawler
26 /// </summary> 26 /// </summary>
27 /// <param name="path"></param> 27 /// <param name="path"></param>
28 public VulnAbstractCrawler() { 28 public VulnAbstractCrawler() {
29 + ReservedList = new HashSet<string>();
30 + LoadReservedList();
29 31
30 } 32 }
31 33
34 +
32 // 소멸자 35 // 소멸자
33 ~VulnAbstractCrawler() { 36 ~VulnAbstractCrawler() {
34 37
35 - Repository.Dispose(); 38 + Repository?.Dispose();
39 +
36 } 40 }
37 41
42 + private void LoadReservedList()
43 + {
44 + try
45 + {
46 + var lines = File.ReadLines(ReservedFileName, Encoding.Default);
47 + foreach (var item in lines)
48 + {
49 + if (string.IsNullOrWhiteSpace(item))
50 + {
51 + continue;
52 + }
53 + ReservedList.Add(item);
54 + }
55 +
56 + }
57 + catch(FileNotFoundException)
58 + {
59 + Console.WriteLine($"{this.GetType().ToString()} 예약어 파일 목록이 없습니다. 파일 이름 : {ReservedFileName}");
60 + }
61 + }
38 protected virtual Regex MethodExtractor => new Regex(RegexFuncPattern); 62 protected virtual Regex MethodExtractor => new Regex(RegexFuncPattern);
39 63
40 #region 메서드 패턴 정규식 그룹 64 #region 메서드 패턴 정규식 그룹
...@@ -138,7 +162,12 @@ namespace VulnCrawler ...@@ -138,7 +162,12 @@ namespace VulnCrawler
138 return string.Empty; 162 return string.Empty;
139 } 163 }
140 164
141 - public static IEnumerable<string> GetCriticalVariant(string line) 165 + /// <summary>
166 + /// 크리티컬 변수 목록 추출
167 + /// </summary>
168 + /// <param name="line">현재 코드줄</param>
169 + /// <returns></returns>
170 + public IEnumerable<string> GetCriticalVariant(string line)
142 { 171 {
143 172
144 // 메서드 정규식 패턴 173 // 메서드 정규식 패턴
...@@ -166,10 +195,17 @@ namespace VulnCrawler ...@@ -166,10 +195,17 @@ namespace VulnCrawler
166 var field = x as Match; 195 var field = x as Match;
167 if (field.Success) 196 if (field.Success)
168 { 197 {
198 + /* 전 단계에서 구한 메서드 목록에 있으면 넘어감 */
169 if (methodSets.Contains(field.Value)) 199 if (methodSets.Contains(field.Value))
170 { 200 {
171 continue; 201 continue;
172 } 202 }
203 + /* 예약어 목록에 있으면 넘어감 */
204 + if (ReservedList.Contains(field.Value))
205 + {
206 + continue;
207 + }
208 +
173 yield return field.Value; 209 yield return field.Value;
174 } 210 }
175 } 211 }
......
...@@ -10,12 +10,12 @@ namespace VulnCrawler ...@@ -10,12 +10,12 @@ namespace VulnCrawler
10 { 10 {
11 public class VulnC : VulnAbstractCrawler 11 public class VulnC : VulnAbstractCrawler
12 { 12 {
13 - protected override string[] ReservedList => new string[] { "if", "return", "break", "while", "typedef" };
14 13
15 protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ (?<{MethodName}>(static)? [\w]+ [\w]+)\([\w \*\,\t\n]*\)"; 14 protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ (?<{MethodName}>(static)? [\w]+ [\w]+)\([\w \*\,\t\n]*\)";
16 15
17 protected override string Extension => ".c"; 16 protected override string Extension => ".c";
18 17
18 + protected override string ReservedFileName => "CReserved.txt";
19 19
20 public override MatchCollection GetMatches(string patchCode) { 20 public override MatchCollection GetMatches(string patchCode) {
21 var regs = Regex.Matches(patchCode, RegexFuncPattern); 21 var regs = Regex.Matches(patchCode, RegexFuncPattern);
......
...@@ -66,6 +66,9 @@ ...@@ -66,6 +66,9 @@
66 <None Include="App.config" /> 66 <None Include="App.config" />
67 <None Include="packages.config" /> 67 <None Include="packages.config" />
68 </ItemGroup> 68 </ItemGroup>
69 + <ItemGroup>
70 + <Content Include="CReserved.txt" />
71 + </ItemGroup>
69 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 72 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
70 <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> 73 <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
71 <PropertyGroup> 74 <PropertyGroup>
......
...@@ -20,7 +20,8 @@ namespace VulnCrawler ...@@ -20,7 +20,8 @@ namespace VulnCrawler
20 protected override string Extension => ".py"; 20 protected override string Extension => ".py";
21 protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ def (?<{MethodName}>\w+)"; 21 protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ def (?<{MethodName}>\w+)";
22 22
23 - protected override string[] ReservedList => throw new NotImplementedException(); 23 +
24 + protected override string ReservedFileName => throw new NotImplementedException();
24 25
25 // protected override Regex MethodExtractor => new Regex(RegexFuncPattern); 26 // protected override Regex MethodExtractor => new Regex(RegexFuncPattern);
26 27
......