Showing
3 changed files
with
150 additions
and
33 deletions
| ... | @@ -210,6 +210,72 @@ namespace VulnCrawler | ... | @@ -210,6 +210,72 @@ namespace VulnCrawler |
| 210 | } | 210 | } |
| 211 | } | 211 | } |
| 212 | } | 212 | } |
| 213 | + | ||
| 214 | + /// <summary> | ||
| 215 | + /// 패치 전 코드 파일과 크리티컬 메서드 테이블로 부터 크리티컬 블록 추출 | ||
| 216 | + /// </summary> | ||
| 217 | + /// <param name="oldBlob">패치 전 파일 Blob</param> | ||
| 218 | + /// <param name="table">크리티컬 메서드 테이블(Key: 메서드 이름, Value: 변수 리스트)</param> | ||
| 219 | + /// <returns></returns> | ||
| 220 | + public virtual IEnumerable<(string methodName, IList<Block> blocks)> ProcessBlocks(Blob oldBlob, IDictionary<string, IEnumerable<string>> table) | ||
| 221 | + { | ||
| 222 | + foreach (var item in table) | ||
| 223 | + { | ||
| 224 | + var methodTable = new Dictionary<string, string>(); | ||
| 225 | + var varTable = new Dictionary<string, string>(); | ||
| 226 | + // 메서드 이름 | ||
| 227 | + string methodName = item.Key; | ||
| 228 | + // 패치 전 원본 파일 스트림 | ||
| 229 | + Stream oldStream = oldBlob.GetContentStream(); | ||
| 230 | + // 패치 전 원본 함수 구하고 | ||
| 231 | + string func = GetOriginalFunc(oldStream, methodName); | ||
| 232 | + Console.WriteLine(func); | ||
| 233 | + string bs = string.Empty; | ||
| 234 | + string md5 = string.Empty; | ||
| 235 | + if (item.Value.Count() != 0) | ||
| 236 | + { | ||
| 237 | + Console.WriteLine("크리티컬 변수 목록"); | ||
| 238 | + Console.ForegroundColor = ConsoleColor.Cyan; | ||
| 239 | + foreach (var c in item.Value) | ||
| 240 | + { | ||
| 241 | + Console.WriteLine(c); | ||
| 242 | + } | ||
| 243 | + Console.ResetColor(); | ||
| 244 | + Console.WriteLine("-------------------"); | ||
| 245 | + // 크리티컬 블록 추출 | ||
| 246 | + var blocks = GetCriticalBlocks(func, item.Value).ToList(); | ||
| 247 | + if (blocks == null) | ||
| 248 | + { | ||
| 249 | + continue; | ||
| 250 | + } | ||
| 251 | + foreach (var block in blocks) | ||
| 252 | + { | ||
| 253 | + | ||
| 254 | + block.CriticalList = item.Value; | ||
| 255 | + block.AbsCode = Abstract(block.Code, varTable, methodTable); | ||
| 256 | + block.Hash = MD5HashFunc(block.AbsCode); | ||
| 257 | + | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + /* 추상화 및 정규화 */ | ||
| 261 | + foreach (var block in blocks) | ||
| 262 | + { | ||
| 263 | + string code = block.Code; | ||
| 264 | + | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + | ||
| 268 | + foreach (var var in varTable) | ||
| 269 | + { | ||
| 270 | + Console.WriteLine($"{var.Key}, {var.Value}"); | ||
| 271 | + } | ||
| 272 | + yield return (methodName, blocks); | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + | ||
| 213 | /// <summary> | 279 | /// <summary> |
| 214 | /// 주석 제거 함수 | 280 | /// 주석 제거 함수 |
| 215 | /// </summary> | 281 | /// </summary> | ... | ... |
| ... | @@ -65,7 +65,7 @@ namespace VulnCrawler | ... | @@ -65,7 +65,7 @@ namespace VulnCrawler |
| 65 | break; | 65 | break; |
| 66 | 66 | ||
| 67 | } | 67 | } |
| 68 | - catch(Exception e) | 68 | + catch(Exception) |
| 69 | { | 69 | { |
| 70 | break; | 70 | break; |
| 71 | //Console.WriteLine(e.ToString()); | 71 | //Console.WriteLine(e.ToString()); |
| ... | @@ -95,42 +95,92 @@ namespace VulnCrawler | ... | @@ -95,42 +95,92 @@ namespace VulnCrawler |
| 95 | // 출력 | 95 | // 출력 |
| 96 | if (regs.Count > 0) | 96 | if (regs.Count > 0) |
| 97 | { | 97 | { |
| 98 | - /* 패치된 코드들에서 Method로 나누고 크리티컬 변수로 뽑아옴 Dictionary 구조 (키 = 함수명) */ | 98 | + ///* 패치된 코드들에서 Method로 나누고 크리티컬 변수로 뽑아옴 Dictionary 구조 (키 = 함수명) */ |
| 99 | + //var table = self.ExtractGitCriticalMethodTable(entry.Patch); | ||
| 100 | + ///* 크리티컬 메서드 테이블과 패치 전 파일에서 Process 하고 tuple로 가져옴 */ | ||
| 101 | + //foreach (var tuple in self.Process(oldBlob, table)) | ||
| 102 | + //{ | ||
| 103 | + // /* 메서드 이름, 원본 함수 코드, 블록 리스트(크리티컬 포함) */ | ||
| 104 | + // (var methodName, var oriFunc, var blocks) = tuple; | ||
| 105 | + | ||
| 106 | + // if (string.IsNullOrWhiteSpace(oriFunc)) | ||
| 107 | + // { | ||
| 108 | + // continue; | ||
| 109 | + // } | ||
| 110 | + | ||
| 111 | + | ||
| 112 | + // string abstractCode = self.Abstract(oriFunc, new Dictionary<string, string>(), new Dictionary<string, string>()); | ||
| 113 | + | ||
| 114 | + // byte[] funcNameBytes = Encoding.Unicode.GetBytes(methodName); | ||
| 115 | + // byte[] absCodeBytes = Encoding.Unicode.GetBytes(abstractCode); | ||
| 116 | + // byte[] commitUrlBytes = Encoding.Unicode.GetBytes(commitUrl); | ||
| 117 | + // byte[] funcBytes = Encoding.Unicode.GetBytes(oriFunc); | ||
| 118 | + | ||
| 119 | + // string absCodeBase64 = Convert.ToBase64String(absCodeBytes); | ||
| 120 | + | ||
| 121 | + // VulnRDS._Vuln vuln = new VulnRDS._Vuln() | ||
| 122 | + // { | ||
| 123 | + // LenFunc = absCodeBase64.Length, | ||
| 124 | + // Cve = cve, | ||
| 125 | + // BlockHash = VulnAbstractCrawler.MD5HashFunc(absCodeBase64), | ||
| 126 | + // FuncName = Convert.ToBase64String(funcNameBytes), | ||
| 127 | + // Code = Convert.ToBase64String(funcBytes), | ||
| 128 | + // Url = Convert.ToBase64String(commitUrlBytes), | ||
| 129 | + // }; | ||
| 130 | + | ||
| 131 | + // /* VulnDB에 추가 */ | ||
| 132 | + // //VulnRDS._InsertVulnData(vuln); | ||
| 133 | + | ||
| 134 | + //} | ||
| 135 | + Console.BackgroundColor = ConsoleColor.DarkBlue; | ||
| 136 | + Console.WriteLine($"Old Content: \n{oldContent}"); | ||
| 137 | + Console.ResetColor(); | ||
| 138 | + Console.ForegroundColor = ConsoleColor.Blue; | ||
| 139 | + Console.WriteLine($"status: {entry.Status.ToString()}"); | ||
| 140 | + Console.WriteLine($"added: {entry.LinesAdded.ToString()}, deleted: {entry.LinesDeleted.ToString()}"); | ||
| 141 | + Console.WriteLine($"old path: {entry.OldPath.ToString()}, new path: {entry.Path.ToString()}"); | ||
| 142 | + Console.ResetColor(); | ||
| 143 | + Console.Write($"CVE: "); | ||
| 144 | + Console.ForegroundColor = ConsoleColor.Red; | ||
| 145 | + Console.Write($"{cve}"); | ||
| 146 | + Console.WriteLine(""); | ||
| 147 | + Console.ResetColor(); | ||
| 148 | + Console.ForegroundColor = ConsoleColor.Yellow; | ||
| 149 | + Console.WriteLine($"Commit Message: {commitMsg}"); | ||
| 150 | + Console.ResetColor(); | ||
| 151 | + Console.BackgroundColor = ConsoleColor.DarkRed; | ||
| 152 | + Console.WriteLine($"Patched: \n{entry.Patch}"); | ||
| 153 | + Console.ResetColor(); | ||
| 99 | var table = self.ExtractGitCriticalMethodTable(entry.Patch); | 154 | var table = self.ExtractGitCriticalMethodTable(entry.Patch); |
| 100 | - /* 크리티컬 메서드 테이블과 패치 전 파일에서 Process 하고 tuple로 가져옴 */ | 155 | + foreach (var tuple in self.ProcessBlocks(oldBlob, table)) |
| 101 | - foreach (var tuple in self.Process(oldBlob, table)) | ||
| 102 | { | 156 | { |
| 103 | - /* 메서드 이름, 원본 함수 코드, 블록 리스트(크리티컬 포함) */ | 157 | + (var methodName, var blocks) = tuple; |
| 104 | - (var methodName, var oriFunc, var blocks) = tuple; | 158 | + Console.BackgroundColor = ConsoleColor.DarkRed; |
| 105 | - | 159 | + Console.WriteLine($"메서드 이름 : {methodName}"); |
| 106 | - if (string.IsNullOrWhiteSpace(oriFunc)) | 160 | + Console.ResetColor(); |
| 161 | + //Console.ForegroundColor = ConsoleColor.Blue; | ||
| 162 | + //foreach (var c in ) | ||
| 163 | + //{ | ||
| 164 | + // Console.WriteLine(c); | ||
| 165 | + //} | ||
| 166 | + //Console.ResetColor(); | ||
| 167 | + foreach (var block in blocks) | ||
| 107 | { | 168 | { |
| 108 | - continue; | 169 | + if (block.HasCritical) |
| 170 | + { | ||
| 171 | + Console.BackgroundColor = ConsoleColor.DarkMagenta; | ||
| 172 | + } | ||
| 173 | + else | ||
| 174 | + { | ||
| 175 | + Console.BackgroundColor = ConsoleColor.DarkGreen; | ||
| 176 | + } | ||
| 177 | + Console.WriteLine($"=====block({block.Num}, {block.HasCritical.ToString()})"); | ||
| 178 | + Console.WriteLine(block.Code); | ||
| 179 | + Console.ResetColor(); | ||
| 180 | + Console.WriteLine($"AbsCode = \n{block.AbsCode}"); | ||
| 181 | + Console.WriteLine($"MD5 = {block.Hash}"); | ||
| 109 | } | 182 | } |
| 110 | - | 183 | + Console.ReadLine(); |
| 111 | - | ||
| 112 | - string abstractCode = self.Abstract(oriFunc, new Dictionary<string, string>(), new Dictionary<string, string>()); | ||
| 113 | - | ||
| 114 | - byte[] funcNameBytes = Encoding.Unicode.GetBytes(methodName); | ||
| 115 | - byte[] absCodeBytes = Encoding.Unicode.GetBytes(abstractCode); | ||
| 116 | - byte[] commitUrlBytes = Encoding.Unicode.GetBytes(commitUrl); | ||
| 117 | - byte[] funcBytes = Encoding.Unicode.GetBytes(oriFunc); | ||
| 118 | - | ||
| 119 | - string absCodeBase64 = Convert.ToBase64String(absCodeBytes); | ||
| 120 | - | ||
| 121 | - VulnRDS._Vuln vuln = new VulnRDS._Vuln() | ||
| 122 | - { | ||
| 123 | - LenFunc = absCodeBase64.Length, | ||
| 124 | - Cve = cve, | ||
| 125 | - BlockHash = VulnAbstractCrawler.MD5HashFunc(absCodeBase64), | ||
| 126 | - FuncName = Convert.ToBase64String(funcNameBytes), | ||
| 127 | - Code = Convert.ToBase64String(funcBytes), | ||
| 128 | - Url = Convert.ToBase64String(commitUrlBytes), | ||
| 129 | - }; | ||
| 130 | - | ||
| 131 | - /* VulnDB에 추가 */ | ||
| 132 | - //VulnRDS._InsertVulnData(vuln); | ||
| 133 | - | ||
| 134 | } | 184 | } |
| 135 | } | 185 | } |
| 136 | else | 186 | else | ... | ... |
| ... | @@ -144,6 +144,7 @@ namespace VulnUserCodeAnalyzer | ... | @@ -144,6 +144,7 @@ namespace VulnUserCodeAnalyzer |
| 144 | } | 144 | } |
| 145 | static void Main(string[] args) | 145 | static void Main(string[] args) |
| 146 | { | 146 | { |
| 147 | + Console.WriteLine("Start: Code Analyze Server"); | ||
| 147 | /* 연도별 CVE JSON 파일 로드 */ | 148 | /* 연도별 CVE JSON 파일 로드 */ |
| 148 | CVE_JSON.AutoLoad(); | 149 | CVE_JSON.AutoLoad(); |
| 149 | /* 크롤러 타입 */ | 150 | /* 크롤러 타입 */ | ... | ... |
-
Please register or login to post a comment