Passed
Push — master ( 77c285...adff27 )
by Kris
02:09
created

ApiBase::getCategoryIdByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 *     _    _                    ___ ____  ____  ____
5
 *    / \  | |__  _   _ ___  ___|_ _|  _ \|  _ \| __ )
6
 *   / _ \ | '_ \| | | / __|/ _ \| || |_) | | | |  _ \
7
 *  / ___ \| |_) | |_| \__ \  __/| ||  __/| |_| | |_) |
8
 * /_/   \_\_.__/ \__,_|___/\___|___|_|   |____/|____/
9
 *
10
 * This file is part of Kristuff\AbsuseIPDB.
11
 *
12
 * (c) Kristuff <[email protected]>
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @version    0.9.11
18
 * @copyright  2020-2021 Kristuff
19
 */
20
21
namespace Kristuff\AbuseIPDB;
22
23
/**
24
 * Class ApiBase
25
 * 
26
 * Abstract base class for ApiHanlder
27
 * Contains main hard coded api settings
28
 */
29
abstract class ApiBase
30
{
31
    /**
32
     * AbuseIPDB API v2 Endpoint
33
     * @var string  
34
     */
35
    protected $aipdbApiEndpoint = 'https://api.abuseipdb.com/api/v2/'; 
36
37
    /**
38
     * AbuseIPDB API key
39
     *  
40
     * @access protected
41
     * @var string $aipdbApiKey  
42
     */
43
    protected $aipdbApiKey = null; 
44
  
45
    /**
46
     * AbuseIPDB API v2 categories
47
     * shorname, id (string), long name
48
     * last paramter is false when the category cant' be used alone
49
     * 
50
     * @static
51
     * @var array
52
     */
53
    protected static $aipdbApiCategories = [
54
        
55
        // Altering DNS records resulting in improper redirection.        
56
        ['dns-c'           , '1', 'DNS Compromise', true],    
57
        
58
        // Falsifying domain server cache (cache poisoning).
59
        ['dns-p'           , '2', 'DNS Poisoning', true],     
60
        
61
        // Fraudulent orders.
62
        ['fraud-orders'    , '3', 'Fraud Orders', true],      
63
64
        // Participating in distributed denial-of-service (usually part of botnet).        
65
        ['ddos'            , '4', 'DDoS Attack', true],       
66
        
67
        // 
68
        ['ftp-bf'          , '5', 'FTP Brute-Force', true],   
69
        
70
        // Oversized IP packet.
71
        ['pingdeath'       , '6', 'Ping of Death', true],     
72
73
        // Phishing websites and/or email.
74
        ['phishing'        , '7', 'Phishing', true],          
75
        
76
        //
77
        ['fraudvoip'       , '8', 'Fraud VoIP', true],        
78
79
        // Open proxy, open relay, or Tor exit node.
80
        ['openproxy'       , '9', 'Open Proxy', true],        
81
82
         // Comment/forum spam, HTTP referer spam, or other CMS spam.
83
         ['webspam'         , '10', 'Web Spam', true],        
84
85
        // Spam email content, infected attachments, and phishing emails. Note: Limit comments to only relevent
86
        // information (instead of log dumps) and be sure to remove PII if you want to remain anonymous.
87
        ['emailspam'       , '11', 'Email Spam', true],                                                   
88
             
89
        // CMS blog comment spam.
90
        ['blogspam'        , '12', 'Blog Spam', true],      
91
        
92
        // Conjunctive category.
93
        ['vpnip'           , '13', 'VPN IP', false], // to check alone ??           
94
95
        // Scanning for open ports and vulnerable services.
96
        ['scan'            , '14', 'Port Scan', true],        
97
       
98
        // 
99
        ['hack'            , '15', 'Hacking', true],           
100
101
        // Attempts at SQL injection.
102
        ['sql'             , '16', 'SQL Injection', true],     
103
        
104
        // Email sender spoofing.
105
        ['spoof'           , '17', 'Spoofing', true],         
106
107
        // Credential brute-force attacks on webpage logins and services like SSH, FTP, SIP, SMTP, RDP, etc. 
108
        // This category is seperate from DDoS attacks.
109
        ['brute'           , '18', 'Brute-Force', true],     
110
111
        // Webpage scraping (for email addresses, content, etc) and crawlers that do not honor robots.txt.                                  
112
        // Excessive requests and user agent spoofing can also be reported here.                        
113
        ['badbot'          , '19', 'Bad Web Bot', true],      
114
                                                         
115
        // Host is likely infected with malware and being used for other attacks or to host malicious content. 
116
        // The host owner may not be aware of the compromise. This category is often used in combination 
117
        // with other attack categories.
118
        ['explhost'        , '20', 'Exploited Host', true],
119
        
120
        // Attempts to probe for or exploit installed web applications such as a CMS 
121
        // like WordPress/Drupal, e-commerce solutions, forum software, phpMyAdmin and 
122
        // various other software plugins/solutions.                                                         
123
        ['webattack'       , '21', 'Web App Attack', true ],   
124
        
125
        // Secure Shell (SSH) abuse. Use this category in combination 
126
        // with more specific categories.
127
        ['ssh'             , '22', 'SSH', false],              
128
129
        // Abuse was targeted at an "Internet of Things" type device. Include 
130
        // information about what type of device was targeted in the comments.         
131
        ['iot'             , '23', 'IoT Targeted', true],     
132
    ];
133
134
    /**
135
     * Get the list of report categories
136
     * 
137
     * @access public
138
     * @static
139
     *  
140
     * @return array
141
     */
142
    public static function getCategories(): array
143
    {
144
        return self::$aipdbApiCategories;
145
    }
146
147
    /**
148
     * Get the category id corresponding to given name
149
     * 
150
     * @access public
151
     * @static
152
     * @param string $categoryName    The report category name
153
     * 
154
     * @return string|bool            The category id in string format if found, otherwise false
155
     */
156
    public static function getCategoryIdByName(string $categoryName)
157
    {
158
        foreach (self::$aipdbApiCategories as $cat){
159
            if ($cat[0] === $categoryName) {
160
                return $cat[1];
161
            }
162
         }
163
164
        // not found
165
        return false;
166
    }
167
168
    /**
169
     * Get the category name corresponding to given id
170
     * 
171
     * @access public
172
     * @static
173
     * @param string    $categoryId   The report category id
174
     * 
175
     * @return string|bool            The category name if found, otherwise false
176
     */
177
    public static function getCategoryNameById(string $categoryId)
178
    {
179
        foreach (self::$aipdbApiCategories as $cat){
180
           if ($cat[1] === $categoryId) {
181
               return $cat[0];
182
           }
183
        }
184
185
        // not found
186
        return false;
187
    }
188
189
    /**
190
     * Get the index of category corresponding to given value
191
     * 
192
     * @access protected
193
     * @static
194
     * @param string        $value          The report category id or name
195
     * @param string        $index          The index in value array 
196
     * 
197
     * @return int|bool     The category index if found, otherwise false
198
     */
199
    protected static function getCategoryIndex(string $value, int $index)
200
    {
201
        $i = 0;
202
        foreach (self::$aipdbApiCategories as $cat){
203
            if ($cat[$index] === $value) {
204
                return $i;
205
            }
206
            $i++;
207
         }
208
209
        // not found
210
        return false;
211
    }
212
213
    /**
214
     * Check if the category(ies) given is/are valid
215
     * Check for shortname or id, and categories that can't be used alone 
216
     * 
217
     * @access protected
218
     * @param array         $categories       The report categories list
219
     *
220
     * @return string       Formatted string id list ('18,2,3...')
221
     * @throws \InvalidArgumentException
222
     */
223
    protected function validateReportCategories(string $categories)
224
    {
225
        // the return categories string
226
        $catsString = ''; 
227
228
        // used when cat that can't be used alone
229
        $needAnother = null;
230
231
        // parse given categories
232
        $cats = explode(',', $categories);
233
234
        foreach ($cats as $cat) {
235
236
            // get index on our array of categories
237
            $catIndex    = is_numeric($cat) ? self::getCategoryIndex($cat, 1) : self::getCategoryIndex($cat, 0);
238
239
            // check if found
240
            if ($catIndex === false ){
241
                throw new \InvalidArgumentException('Invalid report category was given.');
242
            }
243
244
            // get Id
245
            $catId = self::$aipdbApiCategories[$catIndex][1];
246
247
            // need another ?
248
            if ($needAnother !== false){
249
250
                // is a standalone cat ?
251
                if (self::$aipdbApiCategories[$catIndex][3] === false) {
252
                    $needAnother = true;
253
254
                } else {
255
                    // ok, continue with other at least one given
256
                    // no need to reperform this check
257
                    $needAnother = false;
258
                }
259
            }
260
261
            // set or add to cats list 
262
            $catsString = ($catsString === '') ? $catId : $catsString .','.$catId;
263
        }
264
265
        if ($needAnother !== false){
0 ignored issues
show
introduced by
The condition $needAnother !== false is always true.
Loading history...
266
            throw new \InvalidArgumentException('Invalid report category parameter given: this category can\'t be used alone.');
267
        }
268
269
        // if here that ok
270
        return $catsString;
271
    }
272
}