노현종
Committed by 노현종

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

......@@ -125,7 +125,7 @@ namespace VulnCrawler
// 패치 전 원본 함수 구하고
string func = GetOriginalFunc(oldStream, methodName);
// 주석 제거하고
func = RemoveComment(func);
// func = RemoveComment(func);
// 해쉬하고
string md5 = MD5HashFunc(func);
return (func, md5);
......
......@@ -33,46 +33,120 @@ namespace VulnCrawler
protected override string GetOriginalFunc(Stream oldStream, string methodName) {
StringBuilder oldBuilder = new StringBuilder();
methodName = Regex.Escape(methodName);
using (var reader = new StreamReader(oldStream)) {
Console.WriteLine(methodName);
bool found = false;
bool found2 = false;
bool commentLine = false;
int bracketCount = -1;
string stringPattern = @"[""].*[""]";
string commentPattern = @"\/\*.+\*\/";
string commentPattern2 = @"\/\*";
string commentPattern3 = @"\*\/";
while (!reader.EndOfStream) {
string line = reader.ReadLine();
// 메서드를 찾은 경우
if (found)
{
Console.WriteLine("찾았었음");
string trim = line.Trim();
int openBracketCount = line.Count(c => c == '{');
int closeBracketCount = line.Count(c => c == '}');
if (commentLine)
{
if (Regex.IsMatch(trim, commentPattern3))
{
commentLine = false;
trim = Regex.Split(trim, commentPattern3)[1];
}
}
if (bracketCount == -1)
if (string.IsNullOrWhiteSpace(trim))
{
continue;
}
string removeString = Regex.Replace(trim, stringPattern, "");
// /* ~ 패턴
if (Regex.IsMatch(trim, commentPattern2))
{
trim = Regex.Split(trim, "/*")[0];
// /* ~ */ 패턴이 아닌 경우
if (!Regex.IsMatch(trim, commentPattern))
{
commentLine = true;
}
}
int openBracketCount = removeString.Count(c => c == '{');
int closeBracketCount = removeString.Count(c => c == '}');
int subtract = openBracketCount - closeBracketCount;
bracketCount += subtract;
// 메서드 시작 괄호 찾은 경우
if (found2)
{
// 괄호가 모두 닫혔으니 종료
if (bracketCount < 0)
{
Console.WriteLine("괄호끝");
break;
}
oldBuilder.AppendLine(line);
}
if (line.Count(c => c == '{') > 0)
else
{
if (openBracketCount > 0)
{
found2 = true;
}
}
}
if (Regex.Match(line, $@"{methodName}").Success) {
found = true;
int openBracketCount = line.Count(c => c == '{');
int closeBracketCount = line.Count(c => c == '}');
int subtract = openBracketCount - closeBracketCount;
oldBuilder.AppendLine(line);
if (subtract < 0)
}
else
{
if (Regex.Match(line, $"{methodName}").Success)
{
break;
string trim = line.Trim();
if (trim.StartsWith("//"))
{
continue;
}
if (trim.StartsWith("/*"))
{
continue;
}
if (Regex.Match(trim, $@"""[\s]*({methodName})").Success)
{
continue;
}
if (Regex.Match(trim, $@"{methodName}\s*" + @"\{").Success)
{
if (trim.EndsWith("}"))
{
break;
}
found2 = true;
}
// 메서드 찾음
found = true;
oldBuilder.AppendLine(line);
}
bracketCount = subtract;
}
}
}
Console.WriteLine("찾음");
Console.WriteLine(oldBuilder.ToString());
Console.ReadLine();
return oldBuilder.ToString();
}
}
......
......@@ -61,13 +61,13 @@ namespace VulnCrawler
// 출력
if (regs.Count > 0)
{
// Console.BackgroundColor = ConsoleColor.DarkBlue;
// Console.WriteLine($"Old Content: \n{oldContent}");
// Console.ResetColor();
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine($"Old Content: \n{oldContent}");
Console.ResetColor();
// Console.BackgroundColor = ConsoleColor.DarkMagenta;
// Console.WriteLine($"New Content: \n{newContent}");
// Console.ResetColor();
//Console.BackgroundColor = ConsoleColor.DarkMagenta;
//Console.WriteLine($"New Content: \n{newContent}");
//Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"status: {entry.Status.ToString()}");
......@@ -111,12 +111,11 @@ namespace VulnCrawler
Console.WriteLine("methodName = " + methodName);
string originalFunc, md5;
(originalFunc, md5) = self.Process(oldBlob.GetContentStream(),
match.Groups[VulnAbstractCrawler.MethodName].Value);
methodName);
#region 현재 패치 엔트리 정보 출력(추가된 , 삭제된 , 패치 이전 경로, 패치 경로)
// 패치 전 원본 함수
Console.WriteLine($"Original Func: {originalFunc}");
// 해쉬 후
......@@ -131,8 +130,10 @@ namespace VulnCrawler
}
}
catch (Exception)
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
continue;
}
......