이경수
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>8b3ec7f4-6106-4f2f-9a57-d10e4bbd93e8</ProjectGuid>
<ProjectGuid>{8B3EC7F4-6106-4F2F-9A57-D10E4BBD93E8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AWS_Center</RootNamespace>
......@@ -30,24 +30,19 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Xml"/>
<Reference Include="MySql.Data, Version=8.0.11.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
\ No newline at end of file
......

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2037
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWS_Center", "AWS_Center.csproj", "{8B3EC7F4-6106-4F2F-9A57-D10E4BBD93E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8B3EC7F4-6106-4F2F-9A57-D10E4BBD93E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B3EC7F4-6106-4F2F-9A57-D10E4BBD93E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B3EC7F4-6106-4F2F-9A57-D10E4BBD93E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B3EC7F4-6106-4F2F-9A57-D10E4BBD93E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EEBEFAA7-7268-4494-ADAF-291BB2AF8588}
EndGlobalSection
EndGlobal
......@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
// 참고(C# mysql 연결)
#region MySql 연결
......@@ -40,6 +40,8 @@ namespace AWS_Center
{
public static class VulnRDS
{
public static MySqlConnection Conn { get; set; }
public class Vuln
{
public int Len { get; set; } /* 발견된 취약점 함수 PreFunc 부분의 코드 길이 */
......@@ -55,10 +57,30 @@ namespace AWS_Center
public Vuln()
{
}
}
//connect
public static void Connect()
{
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder()
{
Server = "vulndb.cby38wfppa7l.us-east-2.rds.amazonaws.com",
UserID = "yhackerbv",
Password = "guswhd12",
Database = "vuln",
Port = 3306,
};
string strConn = builder.ToString();
builder = null;
Conn = new MySqlConnection(strConn);
}
public static void InsertVulnData(int _len, string _repoName, string _cve, string _funcName,
string _preFunc, string _afterFunc, string _hash)
{
......@@ -66,11 +88,16 @@ namespace AWS_Center
* DB에 취약점 데이터가 이미 있는지 검사해야함
*
*/
}
public static IEnumerable<string> SearchVulnData(int _len)
{
Conn.Open();
}
//public static IEnumerable<string> SearchVulnData(int _len)
//{
//
//}
}
}
......
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace Tester
{
class Program
{
static void Main(string[] args)
{
}
}
}
namespace AWS_Center
{
public static class VulnRDS
{
public static MySqlConnection Conn { get; set; }
public class Vuln
{
public int VulnId { get; set; } /* 취약점 ID */
public int LenBlock { get; set; } /* 취약점 BLOCK 길이 */
public string RepositName { get; set; } /* 취약점 레파지토리 이름 */
public string Cve { get; set; } /* 취약점 CVE */
public string FuncName { get; set; } /* 취약점 함수 이름 */
public string Language { get; set; } /* 취약점 언어 종류 */
public string CodeOriBefore { get; set; } /* 취약점 패치 전 원본 코드 */
public string CodeOriAfter { get; set; } /* 취약점 패치 후 원본 코드 */
public string CodeAbsBefore { get; set; } /* 취약점 패치 전 추상화 코드 */
public string CodeAbsAfter { get; set; } /* 취약점 패치 후 추상화 코드 */
public string BlockHash { get; set; } /* 취약점 블록 해시 값 */
// 생성자
public Vuln()
{
}
}
public class User
{
public int UserId { get; set; } /* 유저 ID */
public string RepositName { get; set; } /* 유저 레파지토리 이름 */
public string Cve { get; set; } /* 취약점 CVE */
public string CodeOriBefore { get; set; } /* 취약점 패치 전 원본 코드 */
public string CodeOriAfter { get; set; } /* 취약점 패치 후 원본 코드 */
public string FuncName { get; set; } /* 취약점 함수 이름 */
public string DetectDate { get; set; } /* 검사 날짜 */
// 생성자
public User()
{
}
}
//connect
public static void Connect()
{
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder()
{
Server = "vulndb.cby38wfppa7l.us-east-2.rds.amazonaws.com",
UserID = "yhackerbv",
Password = "guswhd12",
Database = "vuln",
Port = 3306,
};
string strConn = builder.ToString();
builder = null;
Conn = new MySqlConnection(strConn);
}
public static void InsertVulnData(Vuln vuln)
{
/*
* DB에 취약점 데이터가 이미 있는지 검사해야함
*
*/
Conn.Open();
}
//public static IEnumerable<string> SearchVulnData(int _len)
//{
//
//}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해
// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면
// 이러한 특성 값을 변경하세요.
[assembly: AssemblyTitle("Tester")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Tester")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에
// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면
// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요.
[assembly: ComVisible(false)]
// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다.
[assembly: Guid("ac204ded-c47f-46fe-b357-01c71849ea76")]
// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다.
//
// 주 버전
// 부 버전
// 빌드 번호
// 수정 버전
//
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호가 자동으로
// 지정되도록 할 수 있습니다.
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AC204DED-C47F-46FE-B357-01C71849EA76}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Tester</RootNamespace>
<AssemblyName>Tester</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MySql.Data, Version=8.0.11.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
......@@ -67,15 +67,16 @@ namespace VulnCrawler
public static void Run() {
// Repository 폴더들이 있는 주소를 지정하면 하위 폴더 목록을 가져옴(Repository 목록)
// var fields = VulnWorker.GetCriticalVariant(@"return _is_safe_url(url, host) and _is_safe_url(url.replace('\\', '/'), host)");
var c = new VulnC();
var fields = c.ExtractCriticalVariant(@"!DoReadFile (infile, &ds64_chunk, sizeof (DS64Chunk), &bcount) ||/* aaaa */");
var fields = c.ExtractCriticalVariant(@"if (s->session->peer != s->session->sess_cert->peer_key->x509)");
foreach (var item in fields)
{
Console.WriteLine(item);
}
// return;
var directorys = Directory.GetDirectories(@"c:\VulnPy");
var directorys = Directory.GetDirectories(@"c:\VulnC");
if (directorys.Length == 0) {
Console.WriteLine("Repository 목록 찾기 실패");
return;
......@@ -83,7 +84,7 @@ namespace VulnCrawler
// Repository 목록 만큼 반복함.
foreach (var directory in directorys) {
// 템플릿 패턴화 T : VulnAbstractCrawler
VulnWorker.Run<VulnPython>(directory);
VulnWorker.Run<VulnC>(directory);
}
}
......
......@@ -10,14 +10,23 @@ using System.Threading.Tasks;
namespace VulnCrawler
{
// 추상 클래스
public abstract class VulnAbstractCrawler
{
public class Block
{
public int Num { get; set; }
public bool HasCritical { get; set; }
public string Code { get; set; }
public string Hash { get; set; }
public IEnumerable<string> CriticalList { get; set; }
}
protected Regex extractMethodLine;
protected HashSet<string> ReservedList { get; }
protected abstract string ReservedFileName { get; }
// = { "if", "return", "break", "while", "typedef" };
/// <summary>
/// 생성자
/// 경로를 입력받아서(path)
......@@ -25,20 +34,16 @@ namespace VulnCrawler
/// 커밋 목록을 검색함
/// </summary>
/// <param name="path"></param>
public VulnAbstractCrawler() {
public VulnAbstractCrawler()
{
extractMethodLine = new Regex(RegexFuncPattern);
ReservedList = new HashSet<string>();
LoadReservedList();
}
// 소멸자
~VulnAbstractCrawler() {
Repository?.Dispose();
}
private void LoadReservedList()
{
try
......@@ -46,13 +51,11 @@ namespace VulnCrawler
var lines = File.ReadLines(ReservedFileName, Encoding.Default);
foreach (var item in lines)
{
if (string.IsNullOrWhiteSpace(item))
{
continue;
}
ReservedList.Add(item);
}
}
catch(FileNotFoundException)
......@@ -61,7 +64,6 @@ namespace VulnCrawler
}
}
protected virtual Regex MethodExtractor => new Regex(RegexFuncPattern);
#region 메서드 패턴 정규식 그룹
// 정규식 그룹화
// @@ -oldStart,oldLines +newStart,newLines @@ MethodName():
......@@ -73,7 +75,9 @@ namespace VulnCrawler
#endregion
public void Init(string path) {
Console.WriteLine("로딩중");
Repository = new Repository(path);
Console.WriteLine("로딩 완료");
Commits = SearchCommits();
}
/// <summary>
......@@ -88,7 +92,7 @@ namespace VulnCrawler
/// <summary>
/// 커밋에서 검색할 정규식 문자열
/// </summary>
public string SearchCommitPattern => @"CVE-20\d\d-\d{4}";
public string SearchCommitPattern => @"CVE[ -]\d{4}[ -]\d{4}";
/// <summary>
/// 패치 코드에서 함수 찾을 정규식 패턴 문자열
/// </summary>
......@@ -112,21 +116,170 @@ namespace VulnCrawler
/// <returns>함수 문자열</returns>
protected abstract string GetOriginalFunc(Stream oldStream, string methodName);
protected abstract IList<Block> GetCriticalBlocks(string srcCode, IEnumerable<string> criticalList);
/// <summary>
/// 성능 개선을 위한
/// 코드 라인 위치 기반 취약 원본 함수 추출 테스트용 함수 곧 삭제 예정
/// </summary>
public string GetOriginalFuncTest(Stream oldStream, string methodName, int start)
{
StringBuilder oldBuilder = new StringBuilder();
using (var reader = new StreamReader(oldStream))
{
bool found = false;
bool found2 = false;
bool commentLine = false;
int bracketCount = -1;
string stringPattern = @"[""].*[""]";
string commentPattern = @"\/\*.+\*\/";
string commentPattern2 = @"\/\*";
string commentPattern3 = @"\*\/";
int readCount = 0;
Queue<string> tempQ = new Queue<string>();
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (readCount++ < start)
{
tempQ.Enqueue(line);
continue;
}
Stack<string> tempStack = new Stack<string>();
while (tempQ.Count > 0)
{
string s = tempQ.Dequeue();
tempStack.Push(s);
string method = Regex.Escape(methodName);
if (Regex.Match(s, $"{method}").Success)
{
break;
}
}
while (tempStack.Count > 0)
{
string s = tempStack.Pop();
string trim = s.Trim();
if (commentLine)
{
if (Regex.IsMatch(trim, commentPattern3))
{
commentLine = false;
trim = Regex.Split(trim, commentPattern3)[1];
}
continue;
}
string removeString = Regex.Replace(trim, stringPattern, "");
// /* ~ 패턴
if (Regex.IsMatch(trim, commentPattern2))
{
// /* ~ */ 패턴이 아닌 경우
if (!Regex.IsMatch(trim, commentPattern))
{
commentLine = true;
}
trim = Regex.Split(trim, "/*")[0];
}
if (string.IsNullOrWhiteSpace(trim))
{
continue;
}
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);
}
else
{
if (openBracketCount > 0)
{
found2 = true;
}
}
oldBuilder.AppendLine(s);
}
}
}
Console.WriteLine("찾음");
Console.WriteLine(oldBuilder.ToString());
Console.ReadLine();
return oldBuilder.ToString();
}
public abstract IDictionary<string, IEnumerable<string>> ExtractGitCriticalMethodTable(string srcCode);
/// <summary>
/// 실제 프로세스
/// 패치 전 코드 파일과 크리티컬 메서드 테이블로 부터 크리티컬 블록 추출
/// </summary>
/// <param name="oldStream"></param>
/// <param name="methodName"></param>
/// <param name="oldBlob">패치 전 파일 Blob</param>
/// <param name="table">크리티컬 메서드 테이블(Key: 메서드 이름, Value: 변수 리스트)</param>
/// <returns></returns>
public virtual (string originalFunc, string hash) Process(Stream oldStream, string methodName) {
public virtual IEnumerable<(string methodName, IList<Block> blocks)> Process(Blob oldBlob, IDictionary<string, IEnumerable<string>> table) {
foreach (var item in table)
{
// 메서드 이름
string methodName = item.Key;
// 패치 전 원본 파일 스트림
Stream oldStream = oldBlob.GetContentStream();
// 패치 전 원본 함수 구하고
string func = GetOriginalFunc(oldStream, methodName);
// 주석 제거하고
func = RemoveComment(func);
// 해쉬하고
string md5 = MD5HashFunc(func);
return (func, md5);
Console.WriteLine(func);
string bs = string.Empty;
string md5 = string.Empty;
if (item.Value.Count() != 0)
{
Console.WriteLine("크리티컬 변수 목록");
Console.ForegroundColor = ConsoleColor.Cyan;
foreach (var c in item.Value)
{
Console.WriteLine(c);
}
Console.ResetColor();
Console.WriteLine("-------------------");
// 크리티컬 블록 추출
var blocks = GetCriticalBlocks(func, item.Value).ToList();
if (blocks == null)
{
continue;
}
foreach (var block in blocks)
{
block.Hash = MD5HashFunc(block.Code);
block.CriticalList = item.Value;
}
yield return (methodName, blocks);
}
}
}
/// <summary>
/// 주석 제거 함수
......@@ -142,6 +295,7 @@ namespace VulnCrawler
/// <returns>커밋 목록</returns>
public virtual IEnumerable<Commit> SearchCommits() {
// where => 조건에 맞는 것을 찾음(CVE-20\d\d-\d{4}로 시작하는 커밋만 골라냄)
Console.WriteLine(Repository.Commits.Count());
var commits = Repository.Commits
.Where(c => Regex.Match(c.Message, SearchCommitPattern, RegexOptions.IgnoreCase).Success)
.ToList();
......@@ -183,7 +337,7 @@ namespace VulnCrawler
// 메서드 정규식 패턴
string methodPattern = @"([a-zA-Z0-9_\.]+)\s*\(";
// 변수 정규식 패턴
string fieldPattern = @"^*?[a-zA-Z0-9_\.\[\]]+";
string fieldPattern = @"^*?[a-zA-Z0-9_\.\[\]\-\>]+";
string invalidPattern = @"^[\d\.]+";
......@@ -216,11 +370,11 @@ namespace VulnCrawler
var method = met as Match;
if (method.Success)
{
Console.WriteLine(method.Groups[1].Value);
// Console.WriteLine(method.Groups[1].Value);
methodSets.Add(method.Groups[1].Value); // aaaa
}
}
Console.WriteLine("----");
// Console.WriteLine("----");
var vars = Regex.Matches(line, fieldPattern)
.Cast<Match>()
.Where(m => {
......@@ -243,6 +397,12 @@ namespace VulnCrawler
{
return false;
}
/* 알파벳이 하나도 없으면 넘어감 */
if(!m.Value.Any(c => char.IsLetter(c)))
{
return false;
}
return true;
})
.Distinct(new MatchComparer());
......
......@@ -10,18 +10,14 @@ namespace VulnCrawler
{
public class VulnC : VulnAbstractCrawler
{
protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ (?<{MethodName}>(static)? [\w]+ [\w]+)\([\w \*\,\t\n]*\)";
// protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ (?<{MethodName}>(static)?( const )? [\w]+ [\w]+\([\w \*\,\t\n]*[\)\,])";
protected override string RegexFuncPattern => $@"(?<{MethodName}>(unsigned|static)?( const )? [\w]+ [\w]+\(([\w \*\,\t\n])*[\)\,])";
protected override string Extension => ".c";
protected override string ReservedFileName => "CReserved.txt";
public override MatchCollection GetMatches(string patchCode) {
var regs = Regex.Matches(patchCode, RegexFuncPattern);
return regs;
}
public override string RemoveComment(string original) {
string txt = Regex.Replace(original, Environment.NewLine, "");
......@@ -36,49 +32,332 @@ namespace VulnCrawler
return replace;
}
public override IDictionary<string, IEnumerable<string>> ExtractGitCriticalMethodTable(string srcCode)
{
var table = new Dictionary<string, IEnumerable<string>>();
string prevMethodName = string.Empty;
StringBuilder builder = new StringBuilder();
// 라인으로 나누고 @@가 시작하는 곳까지 생략
var split = Regex.Split(srcCode, "\n").SkipWhile(s => !s.StartsWith("@@")).ToArray();
for(int i = 0; i < split.Length; i++)
{
string line = split[i].Trim();
// 문자열 제거
line = Regex.Replace(line, @""".+""", "");
var methodMatch = extractMethodLine.Match(line);
string methodName = methodMatch.Groups[MethodName].Value.Trim();
// 추가된, 제거된 라인인지 확인
if (Regex.IsMatch(line, @"^[+-]\s"))
{
// 주석문인지 확인
if (Regex.IsMatch(line, @"^[+-]\s*(\*|\/\*|\*\/)"))
{
continue;
}
Console.WriteLine(line);
builder.AppendLine(line);
continue;
}
// 메서드 매칭이 성공했거나 마지막 문단일 경우
if (methodMatch.Success || i == split.Length - 1)
{
if (string.IsNullOrWhiteSpace(prevMethodName))
{
builder.Clear();
prevMethodName = methodName;
continue;
}
if (methodName.Contains("return"))
{
continue;
}
if (methodName.Contains("="))
{
continue;
}
if (!table.ContainsKey(prevMethodName))
{
table[prevMethodName] = new HashSet<string>();
}
var list = table[prevMethodName] as HashSet<string>;
foreach (var b in Regex.Split(builder.ToString(), "\n"))
{
// 각 수집된 라인 별로 크리티컬 변수 선정
foreach (var var in ExtractCriticalVariant(b))
{
if (string.IsNullOrWhiteSpace(var))
{
continue;
}
list.Add(var);
}
}
prevMethodName = methodName;
builder.Clear();
}
}
return table;
}
protected override string GetOriginalFunc(Stream oldStream, string methodName) {
StringBuilder oldBuilder = new StringBuilder();
string method = Regex.Escape(methodName);
using (var reader = new StreamReader(oldStream)) {
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)
{
string trim = line.Trim();
// 범위 주석 진행되고 있으면 넘어감
if (commentLine)
{
// 혹시 범위 주석이 끝났는지 체크
if (Regex.IsMatch(trim, commentPattern3))
{
commentLine = false;
trim = Regex.Split(trim, commentPattern3)[1];
}
else
{
continue;
}
}
// "" 문자열 제거
string removeString = Regex.Replace(trim, stringPattern, "");
// /* ~ 패턴
if (Regex.IsMatch(trim, commentPattern2))
{
int openBracketCount = line.Count(c => c == '{');
int closeBracketCount = line.Count(c => c == '}');
if (bracketCount == -1)
// /* ~ */ 패턴이 아닌 경우
if (!Regex.IsMatch(trim, commentPattern))
{
commentLine = true;
}
trim = Regex.Split(trim, "/*")[0];
}
if (line.Count(c => c == '{') > 0)
// 비어있는 경우 넘어감
if (string.IsNullOrWhiteSpace(trim))
{
continue;
}
int openBracketCount = removeString.Count(c => c == '{');
int closeBracketCount = removeString.Count(c => c == '}');
int subtract = openBracketCount - closeBracketCount;
bracketCount += subtract;
// 메서드 시작 괄호 찾은 경우
if (found2)
{
oldBuilder.AppendLine(line);
// 괄호가 모두 닫혔으니 종료
if (bracketCount < 0)
{
break;
}
}
else // 메서드는 찾았으나 아직 시작 괄호를 못찾은 경우
{
oldBuilder.AppendLine(line);
if (openBracketCount > 0)
{
found2 = true;
}
else
{
//아직 { 괄호를 못찾았는데 );를 만났다면 메서드 선언 부분이니 넘어감
if (trim.EndsWith(");"))
{
found = false;
oldBuilder.Clear();
continue;
}
}
}
}
// 아직 메서드를 못찾은 경우
else
{
// 메서드 찾았는지 확인
if (Regex.Match(line, $"{method}").Success)
{
string trim = line.Trim();
// 주석으로 시작했다면 넘어감
if (trim.StartsWith("//"))
{
continue;
}
if (trim.StartsWith("/*"))
{
continue;
}
if (Regex.Match(line, $@"{methodName}").Success) {
// 혹시 메서드가 문자열 사이에 있다면 넘어감..
if (Regex.Match(trim, $@"""[.]*({method})").Success)
{
continue;
}
// 만약 찾은 메서드 라인에서 중괄호 {가 시작된 경우
if (Regex.Match(trim, $@"{method}\s*" + @"\{").Success)
{
// 동시에 } 닫히기까지 한 경우 드물겠지만..
if (trim.EndsWith("}"))
{
oldBuilder.AppendLine(line);
break;
}
found2 = true;
}
// 메서드 찾음
found = true;
int openBracketCount = line.Count(c => c == '{');
int closeBracketCount = line.Count(c => c == '}');
int subtract = openBracketCount - closeBracketCount;
oldBuilder.AppendLine(line);
}
}
}
}
return oldBuilder.ToString();
}
protected override IList<Block> GetCriticalBlocks(string srcCode, IEnumerable<string> criticalList)
{
// srcCode = Regex.Replace(srcCode, @"if.+\n\{", @"if.+\{", RegexOptions.Multiline);
if (subtract < 0)
var split = srcCode.Split('\n');
int bracketCount = 0;
var blockList = new List<Block>();
StringBuilder builder = new StringBuilder();
var crList = criticalList as HashSet<string>;
if (crList == null)
{
return null;
}
bool hasIf = false;
bool mainLine = true; /* 현재 라인이 메인 코드 라인인지 */
bool criticalBlock = false; /* 현재 라인이 크리티컬 블록 라인인지 */
int blockNum = 1; /* 블록 번호 */
foreach (var line in split)
{
bool hasRight = false;
string trim = line.Trim();
/* 중괄호 수 세기 */
int openBracketCount = trim.Count(c => c == '{');
int closeBracketCount = trim.Count(c => c == '}');
//if (!hasIf)
//{
// if (Regex.IsMatch(trim, @"^if.+\)$"))
// {
// // Console.WriteLine("if 들어감");
// hasIf = true;
// }
//}
//else
//{
// if (!Regex.IsMatch(trim, @"^\{"))
// {
// openBracketCount++;
// }
// hasIf = false;
//}
int subtract = openBracketCount - closeBracketCount;
bracketCount += subtract;
if (trim.Equals("}"))
{
builder.AppendLine(line);
hasRight = true;
}
/* 중괄호 연산 결과 1이라는 것은 메인 라인 */
if (bracketCount == 1)
{
/*
* 깊이가 1인데 mainLine이
* false 이면 넘어왔다는 것이니 현재까지 코드
* blockList에 추가
*/
if (!mainLine)
{
string s = builder.ToString();
if (!string.IsNullOrWhiteSpace(s))
{
blockList.Add(new Block() { HasCritical = criticalBlock, Code = s, Num = blockNum });
blockNum++;
criticalBlock = false;
builder.Clear();
}
}
mainLine = true;
}
/* 2 이상이라는 건 메인 라인 X */
else if(bracketCount >= 2)
{
/*
* 깊이가 2 이상인데 mainLine이
* true면 넘어왔다는 것이니 현재까지 코드
* blockList에 추가
*/
if (mainLine)
{
string s = builder.ToString();
if (!string.IsNullOrWhiteSpace(s))
{
blockList.Add(new Block() { HasCritical = criticalBlock, Code = s, Num = blockNum });
blockNum++;
criticalBlock = false;
builder.Clear();
}
}
mainLine = false;
}
/* 이도 저도 아니면 그냥 넘어감 */
else
{
continue;
}
/* 현재 코드 라인에서 변수 추출시켜서 크리티컬 리스트와 대조 */
foreach (var var in ExtractCriticalVariant(line))
{
/* 크리티컬 리스트에 추출한 변수가 들어있다면 추가 */
if (criticalList.Contains(var))
{
criticalBlock = true;
break;
}
bracketCount = subtract;
}
if (!hasRight)
{
builder.AppendLine(line);
}
}
return oldBuilder.ToString();
/* 마지막 남은게 있을 수 있으니 추가 */
string fs = builder.ToString();
if (!string.IsNullOrWhiteSpace(fs))
{
blockList.Add(new Block() { HasCritical = criticalBlock, Code = fs, Num = blockNum });
blockNum++;
criticalBlock = false;
builder.Clear();
}
return blockList;
}
}
}
......
......@@ -8,9 +8,6 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace VulnCrawler
{
/// <summary>
/// 파이썬 크롤러
/// </summary>
......@@ -68,5 +65,15 @@ namespace VulnCrawler
}
return replace;
}
public override IDictionary<string, IEnumerable<string>> ExtractGitCriticalMethodTable(string srcCode)
{
throw new NotImplementedException();
}
protected override IList<Block> GetCriticalBlocks(string srcCode, IEnumerable<string> criticalList)
{
throw new NotImplementedException();
}
}
}
......
......@@ -17,6 +17,7 @@ namespace VulnCrawler
var crawler = new T();
crawler.Init(dirPath);
var commits = crawler.Commits;
Console.WriteLine(commits.Count());
foreach (var commit in commits) {
// 커밋 메시지
string message = commit.Message;
......@@ -33,6 +34,8 @@ namespace VulnCrawler
// 현재 커밋에 대한 패치 엔트리 배열을 출력함
PrintPatchEntrys(entrys, crawler, message, cve);
}
Console.ReadLine();
}
}
......@@ -42,6 +45,9 @@ namespace VulnCrawler
foreach (var entry in entrys) {
// 기존 소스코드
var oldOid = entry.OldOid;
try
{
Blob oldBlob = self.Repository.Lookup<Blob>(oldOid);
string oldContent = oldBlob.GetContentText();
......@@ -49,46 +55,22 @@ namespace VulnCrawler
var newOid = entry.Oid;
Blob newBlob = self.Repository.Lookup<Blob>(newOid);
string newContent = newBlob.GetContentText();
var regs = self.GetMatches(entry.Patch);
#region 패치 코드 출력
// 패치 전 코드 (oldContent)
// 패치 후 코드 (newContent)
// 패치 코드 (entry.Patch)
// 출력
//if (regs.Count > 0) {
// 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.DarkRed;
// Console.WriteLine($"Patched: \n{entry.Patch}");
// Console.ResetColor();
// Console.WriteLine("-----------");
// Console.WriteLine(regs.Count);
//}
// 패치 코드에서 매칭된 파이썬 함수들로부터
// 패치 전 코드 파일(oldBlob)을 탐색하여 원본 파이썬 함수 가져오고(originalFunc)
//
#endregion
foreach (var reg in regs) {
var match = reg as Match;
string methodName = match.Groups[VulnAbstractCrawler.MethodName].Value;
string originalFunc, md5;
if (regs.Count > 0)
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine($"Old Content: \n{oldContent}");
Console.ResetColor();
(originalFunc, md5) = self.Process(oldBlob.GetContentStream(),
match.Groups[VulnAbstractCrawler.MethodName].Value);
//Console.BackgroundColor = ConsoleColor.DarkMagenta;
//Console.WriteLine($"New Content: \n{newContent}");
//Console.ResetColor();
#region 현재 패치 엔트리 정보 출력(추가된 , 삭제된 , 패치 이전 경로, 패치 경로)
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"status: {entry.Status.ToString()}");
Console.WriteLine($"added: {entry.LinesAdded.ToString()}, deleted: {entry.LinesDeleted.ToString()}");
......@@ -104,20 +86,103 @@ namespace VulnCrawler
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Commit Message: {commitMsg}");
Console.ResetColor();
// 패치 전 원본 함수
Console.WriteLine($"Original Func: {originalFunc}");
// 해쉬 후
Console.WriteLine($"Original Func MD5: {md5}");
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"Patched: \n{entry.Patch}");
Console.ResetColor();
var table = self.ExtractGitCriticalMethodTable(entry.Patch);
foreach (var tuple in self.Process(oldBlob, table))
{
(var methodName, var blocks) = tuple;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"메서드 이름 : {methodName}");
Console.ResetColor();
//Console.ForegroundColor = ConsoleColor.Blue;
//foreach (var c in )
//{
// Console.WriteLine(c);
//}
//Console.ResetColor();
foreach (var block in blocks)
{
if (block.HasCritical)
{
Console.BackgroundColor = ConsoleColor.DarkMagenta;
}
else
{
Console.BackgroundColor = ConsoleColor.DarkGreen;
}
Console.WriteLine($"=====block({block.Num}, {block.HasCritical.ToString()})");
Console.WriteLine(block.Code);
Console.ResetColor();
Console.WriteLine("==============================");
Console.WriteLine($"MD5 = {block.Hash}");
}
}
//foreach (var item in table)
//{
// Console.WriteLine($"Method : {item.Key}");
// //foreach (var b in item.Value)
// //{
// // Console.WriteLine($"--{b}");
// //}
//}
// Console.ReadLine();
}
else
{
continue;
}
// 패치 코드에서 매칭된 파이썬 함수들로부터
// 패치 전 코드 파일(oldBlob)을 탐색하여 원본 파이썬 함수 가져오고(originalFunc)
//
#endregion
//foreach (var reg in regs)
//{
// var match = reg as Match;
// string methodName = match.Groups[VulnAbstractCrawler.MethodName].Value.Trim();
// string originalFunc, md5;
// (originalFunc, md5) = self.Process(oldBlob.GetContentStream(),
// methodName);
// #region 현재 패치 엔트리 정보 출력(추가된 줄 수, 삭제된 줄 수, 패치 이전 경로, 패치 후 경로)
// // 패치 전 원본 함수
// Console.WriteLine($"Original Func: {originalFunc}");
// // 해쉬 후
// Console.WriteLine($"Original Func MD5: {md5}");
// //Console.BackgroundColor = ConsoleColor.DarkRed;
// //Console.WriteLine($"Patched: \n{entry.Patch}");
// Console.ResetColor();
// Console.ForegroundColor = ConsoleColor.Red;
// Console.WriteLine("==============================");
// Console.ResetColor();
// #endregion
//}
//Console.ReadLine();
}
catch (Exception e)
{
// Console.WriteLine(entry.Patch);
// Console.WriteLine(e.ToString());
// Console.ReadLine();
continue;
}
}
}
......
theme: jekyll-theme-midnight
\ No newline at end of file