노현종

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

auto
bool
break
case
char
const
continue
default
defined
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
\ No newline at end of file
......@@ -67,16 +67,14 @@ 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 fields = VulnAbstractCrawler.GetCriticalVariant(@"if(i + inl < bl) {");
// var fields = VulnWorker.GetCriticalVariant(@"return _is_safe_url(url, host) and _is_safe_url(url.replace('\\', '/'), host)");
var c = new VulnC();
var fields = c.GetCriticalVariant(@"if(i + inl < bl) {");
foreach (var item in fields)
{
Console.WriteLine(item);
}
// return;
return;
var directorys = Directory.GetDirectories(@"c:\VulnPy");
if (directorys.Length == 0) {
Console.WriteLine("Repository 목록 찾기 실패");
......
......@@ -14,8 +14,8 @@ namespace VulnCrawler
public abstract class VulnAbstractCrawler
{
protected abstract string[] ReservedList { get; }
protected HashSet<string> ReservedList { get; }
protected abstract string ReservedFileName { get; }
// = { "if", "return", "break", "while", "typedef" };
/// <summary>
......@@ -26,15 +26,39 @@ namespace VulnCrawler
/// </summary>
/// <param name="path"></param>
public VulnAbstractCrawler() {
ReservedList = new HashSet<string>();
LoadReservedList();
}
// 소멸자
~VulnAbstractCrawler() {
Repository.Dispose();
Repository?.Dispose();
}
private void LoadReservedList()
{
try
{
var lines = File.ReadLines(ReservedFileName, Encoding.Default);
foreach (var item in lines)
{
if (string.IsNullOrWhiteSpace(item))
{
continue;
}
ReservedList.Add(item);
}
}
catch(FileNotFoundException)
{
Console.WriteLine($"{this.GetType().ToString()} 예약어 파일 목록이 없습니다. 파일 이름 : {ReservedFileName}");
}
}
protected virtual Regex MethodExtractor => new Regex(RegexFuncPattern);
#region 메서드 패턴 정규식 그룹
......@@ -138,7 +162,12 @@ namespace VulnCrawler
return string.Empty;
}
public static IEnumerable<string> GetCriticalVariant(string line)
/// <summary>
/// 크리티컬 변수 목록 추출
/// </summary>
/// <param name="line">현재 코드줄</param>
/// <returns></returns>
public IEnumerable<string> GetCriticalVariant(string line)
{
// 메서드 정규식 패턴
......@@ -166,10 +195,17 @@ namespace VulnCrawler
var field = x as Match;
if (field.Success)
{
/* 전 단계에서 구한 메서드 목록에 있으면 넘어감 */
if (methodSets.Contains(field.Value))
{
continue;
}
/* 예약어 목록에 있으면 넘어감 */
if (ReservedList.Contains(field.Value))
{
continue;
}
yield return field.Value;
}
}
......
......@@ -10,12 +10,12 @@ namespace VulnCrawler
{
public class VulnC : VulnAbstractCrawler
{
protected override string[] ReservedList => new string[] { "if", "return", "break", "while", "typedef" };
protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ (?<{MethodName}>(static)? [\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);
......
......@@ -66,6 +66,9 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="CReserved.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
......
......@@ -20,7 +20,8 @@ namespace VulnCrawler
protected override string Extension => ".py";
protected override string RegexFuncPattern => $@"@@ \-(?<{OldStart}>\d+),(?<{OldLines}>\d+) \+(?<{NewStart}>\d+),(?<{NewLines}>\d+) @@ def (?<{MethodName}>\w+)";
protected override string[] ReservedList => throw new NotImplementedException();
protected override string ReservedFileName => throw new NotImplementedException();
// protected override Regex MethodExtractor => new Regex(RegexFuncPattern);
......