Passed
Branch master (1e7e1e)
by Kris
01:37
created

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