public override void EnterExpression([NotNull] CSharpParser.ExpressionContext context) { string pattern = @"log.*Pass"; Regex regex = new Regex(pattern); //MatchCollection logList = regex.Matches(context.GetText()); if (regex.IsMatch(context.GetText())) //if (context.GetText().Contains("log.Info")) { //Console.WriteLine(context.GetText()); MethodInfor tmpMethod = new MethodInfor(); tmpMethod.BaselineItem = 700; ParserRuleContext parrentOfTree = (ParserRuleContext)context.Parent; while (!(parrentOfTree is CSharpParser.Method_declarationContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Method_declarationContext) { tmpMethod.methodName = parrentOfTree.GetChild(0).GetText(); tmpMethod.startLine = parrentOfTree.Start.Line; } if (listMethod != null) { if (!listMethod.Any(x => x.startLine == tmpMethod.startLine)) { listMethod.Add(tmpMethod); } } else { listMethod.Add(tmpMethod); } } }
public override void EnterExpression([NotNull] CSharpParser.ExpressionContext context) { if (context.GetText().Contains(".SaveAs(")) { MethodInfor tmpMethod = new MethodInfor(); tmpMethod.BaselineItem = 412; ParserRuleContext parrentOfTree = (ParserRuleContext)context.Parent; while (!(parrentOfTree is CSharpParser.Method_declarationContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Method_declarationContext) { tmpMethod.methodName = parrentOfTree.GetChild(0).GetText(); tmpMethod.startLine = parrentOfTree.Start.Line; } if (listMethod != null) { if (!listMethod.Any(x => x.startLine == tmpMethod.startLine)) { listMethod.Add(tmpMethod); } } else { listMethod.Add(tmpMethod); } } }
public override void EnterExpression([NotNull] CSharpParser.ExpressionContext context) { foreach (var item in vulnContext) { if (context.GetText().Contains(item)) { isVuln = false; //tmpMethod. } } if (tmpMethod == null) { tmpMethod = new MethodInfor(); tmpMethod.BaselineItem = 418; ParserRuleContext parrentOfTree = (ParserRuleContext)context.Parent; while (!(parrentOfTree is CSharpParser.Method_declarationContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Method_declarationContext) { tmpMethod.methodName = parrentOfTree.GetChild(0).GetText(); tmpMethod.startLine = parrentOfTree.Start.Line; } } //Console.WriteLine(context.GetText()); }
public override void EnterFormal_parameter_list([NotNull] CSharpParser.Formal_parameter_listContext context) { ParserRuleContext ancesstorOfTree = (ParserRuleContext)context.Parent; if (!(ancesstorOfTree is CSharpParser.Method_declarationContext)) { return; } int childLength = context.GetChild(0).ChildCount; string type = ""; string varId = ""; for (int i = 0; i < childLength; i = i + 2) { IParseTree childTree = context.GetChild(0).GetChild(i).GetChild(0); if (childTree is CSharpParser.Arg_declarationContext) { for (int j = 0; j < childTree.ChildCount; j++) { if (childTree.GetChild(j) is CSharpParser.TypeContext) { type = childTree.GetChild(j).GetText(); } else if (childTree.GetChild(j) is CSharpParser.IdentifierContext) { varId = childTree.GetChild(j).GetText(); } } if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(varId)) { return; } VariableDefine variableDefine = new VariableDefine(type, varId); if (listVariable != null) { if (!(listVariable.Any(x => x.name == variableDefine.name))) { listVariable.Add(variableDefine); } } else { listVariable = new List <VariableDefine>(); listVariable.Add(variableDefine); } methodInfor = new MethodInfor(); List <string> paramx = new List <string>(); foreach (var item in listVariable) { paramx.Add(item.name); } methodInfor.listArgs = paramx; int startLine = ancesstorOfTree.Start.Line; methodInfor.startLine = startLine; ParserRuleContext parrentOfTree = (ParserRuleContext)childTree.Parent; while (!(parrentOfTree is CSharpParser.Method_declarationContext) && !(parrentOfTree is CSharpParser.Constructor_declarationContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Method_declarationContext) { methodInfor.methodName = parrentOfTree.GetChild(0).GetText(); } else if (parrentOfTree is CSharpParser.Constructor_declarationContext) { methodInfor.methodName = parrentOfTree.GetChild(0).GetText(); } parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree is CSharpParser.Typed_member_declarationContext) { methodInfor.outputType = parrentOfTree.GetChild(0).GetText(); } parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; while (!(parrentOfTree is CSharpParser.Class_definitionContext)) { try { parrentOfTree = (ParserRuleContext)parrentOfTree.Parent; if (parrentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parrentOfTree is CSharpParser.Class_definitionContext) { classTree = parrentOfTree; methodInfor.className = parrentOfTree.GetChild(1).GetText(); } } } }
public override void EnterExpression([NotNull] CSharpParser.ExpressionContext context) { //foreach(var item in method.lineList) //{ // if (context.Start.Line != item) // { // return; // } //} if (!method.lineList.Contains(context.Start.Line)) { return; } foreach (var item in listCase) { tmpExpression = new List <TempExpression>(); if (context.GetText().Contains(item.signal)) { string[] part = context.GetText().Split(char.Parse(item.partition)); for (int i = 0; i < part.Length; i++) { //Console.WriteLine(part[i].Replace(")", "")); tmpExpression.Add(new TempExpression(part[i].Replace(")", ""), context.Start.Line)); } if (tmpExpression.Count == 0) { continue; } ParserRuleContext parentOfTree = (ParserRuleContext)context.Parent; while (!(parentOfTree is CSharpParser.Method_declarationContext)) { try { parentOfTree = (ParserRuleContext)parentOfTree.Parent; if (parentOfTree == null) { return; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } if (parentOfTree is CSharpParser.Method_declarationContext) { TraceForCase102 tracer = new TraceForCase102(parser, parentOfTree, tmpExpression); if (!tracer.isVulnMethod) { continue; } if (listResult != null) { if (!(listResult.Any(x => x.methodName == tracer.methodInfor.methodName))) { listResult.Add(tracer.methodInfor); } else { if (!(listResult.SingleOrDefault(x => x.methodName == tracer.methodInfor.methodName).listExp.Any(x => x.line == tracer.methodInfor.listExp[0].line))) { MethodInfor tmpMethod = listResult.SingleOrDefault(x => x.methodName == tracer.methodInfor.methodName); foreach (var tmpItem in tracer.methodInfor.listExp) { tmpMethod.listExp.Add(tmpItem); } listResult.RemoveAll(x => x.methodName == tracer.methodInfor.methodName); listResult.Add(tmpMethod); } } } else { listResult = new List <MethodInfor>(); listResult.Add(tracer.methodInfor); } } //printResult(tracer.methodInfor); } } }
private void printResult(MethodInfor methodInfor) { throw new NotImplementedException(); }