PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / known-bots / bots / class-abstract-bot.php

class-abstract-bot.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at src/component/known-bots/bots/class-abstract-bot.php

116 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract bot class.
4 *
5 * @package WP_Defender\Component\Known_Bots\Bots
6 */
7
8 namespace WP_Defender\Component\Known_Bots\Bots;
9
10 use WP_Defender\Component\User_Agent;
11 use WP_Defender\Traits\IP;
12
13 /**
14 * Abstract class for Bots.
15 */
16 abstract class Abstract_Bot implements Bots_Interface {
17 use IP;
18
19 /**
20 * Normalizes a user agent string.
21 *
22 * @param string $user_agent The user agent to clean.
23 *
24 * @return string
25 */
26 protected function normalize_ua( string $user_agent = '' ): string {
27 if ( '' === $user_agent ) {
28 $user_agent = defender_get_data_from_request( 'HTTP_USER_AGENT', 's' );
29 if ( '' === $user_agent ) {
30 return '';
31 }
32 $user_agent = User_Agent::fast_cleaning( $user_agent );
33 }
34
35 if ( function_exists( 'mb_strtolower' ) ) {
36 return mb_strtolower( $user_agent, 'UTF-8' );
37 }
38
39 return strtolower( $user_agent );
40 }
41
42 /**
43 * Validates and normalizes an IP address for DNS comparisons.
44 *
45 * @param mixed $ip The IP address to normalize.
46 *
47 * @return string|false
48 */
49 protected function normalize_ip_address( $ip ) {
50 if ( ! is_string( $ip ) ) {
51 return false;
52 }
53
54 $ip = trim( $ip );
55 if ( false === filter_var( $ip, FILTER_VALIDATE_IP ) ) {
56 return false;
57 }
58
59 $packed_ip = inet_pton( $ip );
60
61 return false === $packed_ip ? false : inet_ntop( $packed_ip );
62 }
63
64 /**
65 * Verifies an IP address using reverse and forward DNS lookup.
66 *
67 * @param string $ip The IP address to verify.
68 * @param string $domain_pattern Regex pattern to match hostnames.
69 *
70 * @return bool
71 */
72 protected function verify_dns( string $ip, string $domain_pattern ): bool {
73 $normalized_ip = $this->normalize_ip_address( $ip );
74 if ( false === $normalized_ip ) {
75 return false;
76 }
77
78 $hostname = gethostbyaddr( $normalized_ip );
79 if ( ! is_string( $hostname ) || '' === $hostname || $hostname === $normalized_ip ) {
80 return false;
81 }
82
83 $hostname = rtrim( trim( strtolower( $hostname ) ), '.' );
84 if ( preg_match( '/' . $domain_pattern . '$/i', $hostname ) ) {
85 $resolved_ips = array();
86 if ( function_exists( 'dns_get_record' ) ) {
87 $records = dns_get_record( $hostname, DNS_A | DNS_AAAA );
88 if ( is_array( $records ) ) {
89 foreach ( $records as $record ) {
90 if ( isset( $record['ip'] ) ) {
91 $resolved_ips[] = $record['ip'];
92 } elseif ( isset( $record['ipv6'] ) ) {
93 $resolved_ips[] = $record['ipv6'];
94 }
95 }
96 }
97 }
98
99 if ( array() === $resolved_ips ) {
100 $hosts = gethostbynamel( $hostname );
101 if ( is_array( $hosts ) ) {
102 $resolved_ips = $hosts;
103 }
104 }
105
106 foreach ( $resolved_ips as $host ) {
107 if ( $normalized_ip === $this->normalize_ip_address( $host ) ) {
108 return true;
109 }
110 }
111 }
112
113 return false;
114 }
115 }
116