Skip to main content

A Python package with tools for the detection of SQL injection vulnerabilities in projects.

Project description

CI Tests

Evase Analysis Library

This library intends to help users detect SQL Injection vulnerabilities from their source code. It has several structures that take-in Python source code and use abstract-syntax trees (ASTs) to analyze for such vulnerabilities. The code was initially part of a much bigger project, but as to provide separation of concerns, the functionality for detecting the SQL injection-related vulnerabilities was separated out into this package.

What's New

In the latest version of the library:

  • The performance of the code has been slightly improved by the removal of creating unnecessary expensive NodeVisitor and NodeTransformer objects.
    • Instead, the objects are created as a single instance and have their states reset.
  • Most classes in the system now use @property annotations for getters and setters now.
  • The ProjectAnalysisStruct class can now be queried for module structures like a dictionary.
    • By passing the name of the module, you can retrieve the corresponding ModuleAnalysisStruct.
  • The functions are able to fail more gracefully when encountering unexpected AST nodes.
  • The Breadth-First search functions keep track of paths traversed.
    • The main traversal function (traversal_from_exec) now outputs a mapping of ending nodes to their possible paths.
  • The dependency graph generated by ProjectAnalysisStruct has been tested for many use cases and seems to work very well.
  • Some bugs in the module imports generated by ModuleImportResolver have been fixed.
    • The resolver now better handles variant imports like:
      • from <one or more (.)> import <function|module|*>
      • If we can backtrack to a module (through the count of '.' characters) we assume the object being imported is a module, otherwise it is a function.
  • The graph node grouping mechanism in AnalysisPerformer has had some bugs resolved.
  • Nodes in the graph now appear with the form <package style name>:<function name> for functions found WITHIN the analysis of the code (not external libraries).

Issues

There are still a variety of issues with the handling of the various errors that our traversal mechanisms encounter.

  • Those Flask endpoints that do not accept function parameters, (POST-type requests) aren't considered vulnerable due to inadequacies with the collect_vulnerable_vars function.
  • There are many ways of writing imports, and it is likely that the ModuleImportResolver doesn't consider all of these.

Usage

This package was developed initially with the intention that it be used in the Backend for the Evase web-application, so it is structured as such. To use it in a program, the user first needs to specify information pertaining to the project.

The user is able to analyze the project with an instance of the AnalysisPerformer class.

from evase.structures.analysisperformer import AnalysisPerformer

code_analyzer = AnalysisPerformer(
    project_name="myProject",
    project_root="<filepath to root>"
)

code_analyzer.perform_analysis()

print(code_analyzer.get_results())

# optionally, output to JSON
code_analyzer.results_to_JSON("<output directory>")

Behind the scenes, this instance is performing multiple traversals of the abstract syntax trees (ASTs) generated from the source code in the project.

Important Information

The code made in this package relies on various functions applied to Abstract Syntax Trees (ASTs).

Many of the functions in the code are required to perform other functions. The functions directly relating to ASTs at a low level require that the input ASTs be modified. The functions inside of input ASTs must have their scopes resolved.

# example.py

def foo():
  print("FOO")
  
class Bar:
  
  def foo(self):
    print("FOO")

In this example, the AST output would look something like:

<FunctionDef name='foo'>
<ClassDef name='Bar'>
  <FunctionDef name='foo'>

As you can see, while traversing using a NodeVisitor it would be difficult to determine the scope of the inner function. As such we created the ScopeResolver such that the output AST would look something like:

<FunctionDef name='foo'>
<ClassDef name='Bar'>
  <FunctionDef name='Bar.foo'>

For functions involving the analysis of dependencies, the import nodes inside of input ASTs must also have their module attribute be absolute rather than relative. The importable items from the imported module must also be directly specified if an import in the form of <from | import> ... * is being used. Take the following script for example:

# package/imported.py
def foo():
  print("FOO")
  
def bar():
  print("BAR")
  
class Bar:
  
  def bar(self):
    print("BAR")

# package/inner/example.py

from ..imported import *

Imports in this form are very hard to analyze because ASTs don't provide any information other than the form of the import. The AST for package/inner/example.py would look something like:

<Import module=None names="*">

It's clear that this doesn't help much when analyzing dependencies. For this reason we created the SurfaceLevelVisitor and ModuleImportResolver that work in tandem. The SurfaceLevelVisitor collects all the surface level importable items from a module. We then combine these into a list and make an instance of ModuleImportResolver that resolves module names in import nodes and their imported items. Using these two instances properly retrieves the following AST.

<Import module="package.imported" names=["foo", "bar", "Bar"]>

Most of the classes within this library rely on the fact that the scopes will be resolved, and that the imports are correctly resolved.

Installation

This package is available on PyPI! You can install it using:

pip install evase-analysis

Or you can simply clone the repository and run:

pip install .

(in the directory with pyproject.toml)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

evase-analysis-1.0.8.5.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

evase_analysis-1.0.8.5-py3-none-any.whl (48.5 kB view details)

Uploaded Python 3

File details

Details for the file evase-analysis-1.0.8.5.tar.gz.

File metadata

  • Download URL: evase-analysis-1.0.8.5.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for evase-analysis-1.0.8.5.tar.gz
Algorithm Hash digest
SHA256 4a0105f10592492f4ad237b1f4e639b6db50f4d928a1dbf831f5fe95e1145050
MD5 63ab35d3cc2b3099cb7835a76863ea73
BLAKE2b-256 95e8abf08ad0e00ac26510a6affe67b540a87ecd367cc8910eede8d48b14fb08

See more details on using hashes here.

File details

Details for the file evase_analysis-1.0.8.5-py3-none-any.whl.

File metadata

File hashes

Hashes for evase_analysis-1.0.8.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b0a9991e5a5868b69cb2de952aa86aed81fcb678d5d739c584c884552721ef6b
MD5 2d5ae4b10f93ae50d1ffb28e4c4e77d0
BLAKE2b-256 ac140070fd71ffe62050d0242ca63ef34f7a83cd828c445ba94477c608a09264

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page