SqlInjection::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Aszone\Vulnerabilities;
4
5
use GuzzleHttp\ClientInterface;
6
use Psr\Log\LoggerInterface;
7
use Aszone\Vulnerabilities\Log\Logger;
8
9
class SqlInjection implements VulnerabilityScanner
10
{
11
    const EXPLOIT = "'";
12
13
    private $errors;
14
15
    private $client;
16
17
    private $logger;
18
19 View Code Duplication
    public function __construct(ClientInterface $client, array $errors, LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $this->client = $client;
22
        $this->errors = $errors;
23
24
        if (empty($logger)) {
25
            $logger = new Logger;
26
        }
27
28
        $this->logger = $logger;
29
    }
30
31
    public function isVulnerable($target)
32
    {
33
        if ($this->isSqlInjectionPossible($target)) {
34
            return $this->verify($target);
35
        }
36
37
        return false;
38
    }
39
40
    protected function isSqlInjectionPossible($target)
41
    {
42
        return isset(parse_url($target)['query']);
43
    }
44
45 View Code Duplication
    protected function verify($target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $urls = $this->generateUrlByExploit($target);
48
49
        foreach ($urls as $url) {
50
            $this->logger->info("\n url =>".$url."\n");
51
52
            if ($this->attack($url)) {
53
                $this->logger->info('Is Vull');
54
55
                return true;
56
            }
57
        }
58
59
        return false;
60
    }
61
62
    public function generateUrlByExploit($target)
63
    {
64
        $explodeUrl = parse_url($target);
65
        $explodeQuery = explode('&', $explodeUrl['query']);
66
67 View Code Duplication
        foreach ($explodeQuery as $keyQuery => $query) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
            $explodeQueryEqual = explode('=', $query);
69
            $wordsValue[$explodeQueryEqual[0]] = $explodeQueryEqual[1];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$wordsValue was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wordsValue = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
70
        }
71
72 View Code Duplication
        foreach ($wordsValue as $keyValue => $value) {
0 ignored issues
show
Bug introduced by
The variable $wordsValue does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            $urls[] = str_replace($keyValue.'='.$value, $keyValue.'='.$value.static::EXPLOIT, $target);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$urls was never initialized. Although not strictly required by PHP, it is generally a good practice to add $urls = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
74
        }
75
76
        return $urls;
0 ignored issues
show
Bug introduced by
The variable $urls does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
77
    }
78
79
    public function attack($url)
80
    {
81
        try {
82
            $body = $this->client->get($url)->getBody()->getContents();
83
84
            if ($body) {
85
                if ($this->checkError($body)) {
86
                    return $url;
87
                }
88
            }
89
        } catch (\Exception $e) {
90
            if ($e->getCode() != '404' and $e->getCode() != '403') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
                return $url;
92
            }
93
94
            $this->logger->error('Error code => '.$e->getCode()."\n");
95
        }
96
97
        return false;
98
    }
99
100
    public function checkError($body)
101
    {
102
        foreach ($this->errors as $error) {
103
            $isValid = strpos($body, $error);
104
105
            if ($isValid !== false) {
106
                return true;
107
            }
108
        }
109
110
        return false;
111
    }
112
}
113