노현종

C언어 패치 전 함수 크롤링(OpenSSL 테스트)

...@@ -125,7 +125,7 @@ namespace VulnCrawler ...@@ -125,7 +125,7 @@ namespace VulnCrawler
125 // 패치 전 원본 함수 구하고 125 // 패치 전 원본 함수 구하고
126 string func = GetOriginalFunc(oldStream, methodName); 126 string func = GetOriginalFunc(oldStream, methodName);
127 // 주석 제거하고 127 // 주석 제거하고
128 - func = RemoveComment(func); 128 + // func = RemoveComment(func);
129 // 해쉬하고 129 // 해쉬하고
130 string md5 = MD5HashFunc(func); 130 string md5 = MD5HashFunc(func);
131 return (func, md5); 131 return (func, md5);
......
...@@ -33,46 +33,120 @@ namespace VulnCrawler ...@@ -33,46 +33,120 @@ namespace VulnCrawler
33 33
34 protected override string GetOriginalFunc(Stream oldStream, string methodName) { 34 protected override string GetOriginalFunc(Stream oldStream, string methodName) {
35 StringBuilder oldBuilder = new StringBuilder(); 35 StringBuilder oldBuilder = new StringBuilder();
36 + methodName = Regex.Escape(methodName);
36 using (var reader = new StreamReader(oldStream)) { 37 using (var reader = new StreamReader(oldStream)) {
37 - 38 + Console.WriteLine(methodName);
39 +
40 +
38 bool found = false; 41 bool found = false;
42 + bool found2 = false;
43 + bool commentLine = false;
39 int bracketCount = -1; 44 int bracketCount = -1;
45 + string stringPattern = @"[""].*[""]";
46 + string commentPattern = @"\/\*.+\*\/";
47 + string commentPattern2 = @"\/\*";
48 + string commentPattern3 = @"\*\/";
40 while (!reader.EndOfStream) { 49 while (!reader.EndOfStream) {
41 string line = reader.ReadLine(); 50 string line = reader.ReadLine();
42 - 51 +
52 + // 메서드를 찾은 경우
43 if (found) 53 if (found)
44 { 54 {
55 + Console.WriteLine("찾았었음");
56 + string trim = line.Trim();
45 57
46 - int openBracketCount = line.Count(c => c == '{'); 58 + if (commentLine)
47 - int closeBracketCount = line.Count(c => c == '}'); 59 + {
60 + if (Regex.IsMatch(trim, commentPattern3))
61 + {
62 + commentLine = false;
63 + trim = Regex.Split(trim, commentPattern3)[1];
64 + }
65 + }
48 66
49 - if (bracketCount == -1) 67 + if (string.IsNullOrWhiteSpace(trim))
50 { 68 {
69 + continue;
70 + }
71 + string removeString = Regex.Replace(trim, stringPattern, "");
51 72
73 + // /* ~ 패턴
74 + if (Regex.IsMatch(trim, commentPattern2))
75 + {
76 + trim = Regex.Split(trim, "/*")[0];
77 + // /* ~ */ 패턴이 아닌 경우
78 + if (!Regex.IsMatch(trim, commentPattern))
79 + {
80 + commentLine = true;
81 + }
82 + }
83 + int openBracketCount = removeString.Count(c => c == '{');
84 + int closeBracketCount = removeString.Count(c => c == '}');
85 + int subtract = openBracketCount - closeBracketCount;
86 + bracketCount += subtract;
87 + // 메서드 시작 괄호 찾은 경우
88 + if (found2)
89 + {
90 + // 괄호가 모두 닫혔으니 종료
91 + if (bracketCount < 0)
92 + {
93 + Console.WriteLine("괄호끝");
94 + break;
95 + }
96 + oldBuilder.AppendLine(line);
52 } 97 }
53 - if (line.Count(c => c == '{') > 0) 98 + else
54 { 99 {
100 + if (openBracketCount > 0)
101 + {
102 + found2 = true;
103 + }
55 104
56 } 105 }
57 - }
58 106
59 - if (Regex.Match(line, $@"{methodName}").Success) {
60 - found = true;
61 - int openBracketCount = line.Count(c => c == '{');
62 - int closeBracketCount = line.Count(c => c == '}');
63 - int subtract = openBracketCount - closeBracketCount;
64 - oldBuilder.AppendLine(line);
65 107
66 - if (subtract < 0) 108 + }
109 + else
110 + {
111 + if (Regex.Match(line, $"{methodName}").Success)
67 { 112 {
68 - break; 113 +
114 + string trim = line.Trim();
115 + if (trim.StartsWith("//"))
116 + {
117 + continue;
118 + }
119 +
120 + if (trim.StartsWith("/*"))
121 + {
122 + continue;
123 + }
124 +
125 + if (Regex.Match(trim, $@"""[\s]*({methodName})").Success)
126 + {
127 + continue;
128 + }
129 +
130 + if (Regex.Match(trim, $@"{methodName}\s*" + @"\{").Success)
131 + {
132 + if (trim.EndsWith("}"))
133 + {
134 + break;
135 + }
136 + found2 = true;
137 + }
138 + // 메서드 찾음
139 + found = true;
140 + oldBuilder.AppendLine(line);
69 } 141 }
70 - bracketCount = subtract;
71 } 142 }
72 -
73 } 143 }
74 144
75 } 145 }
146 + Console.WriteLine("찾음");
147 + Console.WriteLine(oldBuilder.ToString());
148 + Console.ReadLine();
149 +
76 return oldBuilder.ToString(); 150 return oldBuilder.ToString();
77 } 151 }
78 } 152 }
......
...@@ -61,13 +61,13 @@ namespace VulnCrawler ...@@ -61,13 +61,13 @@ namespace VulnCrawler
61 // 출력 61 // 출력
62 if (regs.Count > 0) 62 if (regs.Count > 0)
63 { 63 {
64 - // Console.BackgroundColor = ConsoleColor.DarkBlue; 64 + Console.BackgroundColor = ConsoleColor.DarkBlue;
65 - // Console.WriteLine($"Old Content: \n{oldContent}"); 65 + Console.WriteLine($"Old Content: \n{oldContent}");
66 - // Console.ResetColor(); 66 + Console.ResetColor();
67 67
68 - // Console.BackgroundColor = ConsoleColor.DarkMagenta; 68 + //Console.BackgroundColor = ConsoleColor.DarkMagenta;
69 - // Console.WriteLine($"New Content: \n{newContent}"); 69 + //Console.WriteLine($"New Content: \n{newContent}");
70 - // Console.ResetColor(); 70 + //Console.ResetColor();
71 71
72 Console.ForegroundColor = ConsoleColor.Blue; 72 Console.ForegroundColor = ConsoleColor.Blue;
73 Console.WriteLine($"status: {entry.Status.ToString()}"); 73 Console.WriteLine($"status: {entry.Status.ToString()}");
...@@ -111,12 +111,11 @@ namespace VulnCrawler ...@@ -111,12 +111,11 @@ namespace VulnCrawler
111 Console.WriteLine("methodName = " + methodName); 111 Console.WriteLine("methodName = " + methodName);
112 string originalFunc, md5; 112 string originalFunc, md5;
113 (originalFunc, md5) = self.Process(oldBlob.GetContentStream(), 113 (originalFunc, md5) = self.Process(oldBlob.GetContentStream(),
114 - match.Groups[VulnAbstractCrawler.MethodName].Value); 114 + methodName);
115 115
116 #region 현재 패치 엔트리 정보 출력(추가된 , 삭제된 , 패치 이전 경로, 패치 경로) 116 #region 현재 패치 엔트리 정보 출력(추가된 , 삭제된 , 패치 이전 경로, 패치 경로)
117 117
118 - 118 +
119 -
120 // 패치 전 원본 함수 119 // 패치 전 원본 함수
121 Console.WriteLine($"Original Func: {originalFunc}"); 120 Console.WriteLine($"Original Func: {originalFunc}");
122 // 해쉬 후 121 // 해쉬 후
...@@ -131,8 +130,10 @@ namespace VulnCrawler ...@@ -131,8 +130,10 @@ namespace VulnCrawler
131 130
132 } 131 }
133 } 132 }
134 - catch (Exception) 133 + catch (Exception e)
135 { 134 {
135 + Console.WriteLine(e.ToString());
136 + Console.ReadLine();
136 continue; 137 continue;
137 } 138 }
138 139
......