BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loginwatch.class.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * Adapted from
  5. * https://github.com/OPSnet/Gazelle/blob/master/app/LoginWatch.php
  6. *
  7. * Unknown status as of 2020-12-12
  8. */
  9. class LoginWatch
  10. {
  11. protected $watchId;
  12. /**
  13. * Set the context of a watched IP address (to save passing it in to each method call).
  14. * On a virgin login with no previous errors there may not even be a watch yet
  15. * @param int ID of the watch
  16. */
  17. public function setWatch($watchId)
  18. {
  19. if (!is_null($watchId)) {
  20. $this->watchId = $watchId;
  21. }
  22. return $this;
  23. }
  24. /**
  25. * Find a login watch by IP address
  26. * @param string IPv4 address
  27. * @return array [watchId, nrAttemtps, nrBans, bannedUntil]
  28. */
  29. public function findByIp(string $ipaddr): ?array
  30. {
  31. # Default: remote host
  32. if (empty($ipaddr)) {
  33. $ipaddr = $_SERVER['REMOTE_ADDR'];
  34. }
  35. return G::$DB->row("
  36. SELECT
  37. `ID`,
  38. `Attempts`,
  39. `Bans`,
  40. `BannedUntil`
  41. FROM
  42. `login_attempts`
  43. WHERE
  44. `IP` = '$ipaddr'
  45. ");
  46. }
  47. /**
  48. * Create a new login watch on an userid/username/ipaddress
  49. * @param string IPv4 address
  50. * @param string|null $capture The username captured on the form
  51. * @param int $userId
  52. * @return int ID of watch
  53. */
  54. public function create(string $ipaddr, ?string $capture, int $userId = 0)
  55. {
  56. G::$DB->prepare_query("
  57. INSERT INTO `login_attempts`(`IP`, `Capture`, `UserID`)
  58. VALUES('$ipaddr', '$capture', '$userId')
  59. ");
  60. G::$DB->exec_prepared_query();
  61. return ($this->watchId = G::$DB->inserted_id());
  62. }
  63. /**
  64. * Ban subsequent attempts to login from this watched IP address for 6 hours
  65. * @param int $attempts How many attempts so far?
  66. * @param string the username captured on the form (which may not even be a valid user)
  67. * @param int $userId user ID of a valid user (or 0 if invalid username)
  68. * @return int 1 if the watch was banned
  69. */
  70. public function ban(int $attempts, ?string $capture, int $userId = 0): int
  71. {
  72. G::$DB->prepare_query("
  73. UPDATE
  74. `login_attempts`
  75. SET
  76. `Bans` = `Bans` + 1,
  77. `LastAttempt` = NOW(),
  78. `BannedUntil` = NOW() + INTERVAL 6 HOUR,
  79. `Attempts` = '$attempts',
  80. `Capture` = '$capture',
  81. `UserID` = '$userId'
  82. WHERE
  83. `ID` = '$this->watchId'
  84. ");
  85. G::$DB->exec_prepared_query();
  86. return G::$DB->affected_rows();
  87. }
  88. /**
  89. * When does the login ban expire?
  90. * @return string datestamp of expiry
  91. */
  92. public function bannedUntil(): ?string
  93. {
  94. return G::$DB->scalar("
  95. SELECT
  96. `BannedUntil`
  97. FROM
  98. `login_attempts`
  99. WHERE
  100. `ID` = '$this->watchId'
  101. ");
  102. }
  103. /**
  104. * If the login ban was in the past then they get 6 more shots
  105. * @return int 1 if a prior ban was cleared
  106. */
  107. public function clearPriorBan(): int
  108. {
  109. G::$DB->prepare_query("
  110. UPDATE
  111. `login_attempts`
  112. SET
  113. `BannedUntil` = NULL,
  114. `Attempts` = 0
  115. WHERE
  116. `BannedUntil` < NOW()
  117. AND `ID` = '$this->watchId'
  118. ");
  119. G::$DB->exec_prepared_query();
  120. return G::$DB->affected_rows();
  121. }
  122. /**
  123. * If the login was successful, clear prior attempts
  124. * @return int 1 if an update was made
  125. */
  126. public function clearAttempts(): int
  127. {
  128. G::$DB->prepare_query("
  129. UPDATE
  130. `login_attempts`
  131. SET
  132. `Attempts` = 0
  133. WHERE
  134. `ID` = '$this->watchId'
  135. ");
  136. G::$DB->exec_prepared_query();
  137. return $this->db->affected_rows();
  138. }
  139. /**
  140. * How many attempts have been made on this watch?
  141. * @return int Number of attempts
  142. */
  143. public function nrAttempts(): int
  144. {
  145. return (int) G::$DB->scalar("
  146. SELECT
  147. `Attempts`
  148. FROM
  149. `login_attempts`
  150. WHERE
  151. `ID` = '$this->watchId'
  152. ") ?? 0;
  153. }
  154. /**
  155. * Get the list of login failures
  156. * @return array list [ID, ipaddr, userid, LastAttempt (datetime), Attempts, BannedUntil (datetime), Bans]
  157. */
  158. public function activeList(string $orderBy, string $orderWay): array
  159. {
  160. G::$DB->prepare_query("
  161. SELECT
  162. w.`ID` AS id,
  163. w.`IP` AS ipaddr,
  164. w.`UserID` AS user_id,
  165. w.`LastAttempt` AS last_attempt,
  166. w.`Attempts` AS attempts,
  167. w.`BannedUntil` AS banned_until,
  168. w.`Bans` AS bans,
  169. w.`Capture`,
  170. um.`Username` AS username,
  171. (ip.`FromIP` IS NOT NULL) AS banned
  172. FROM
  173. `login_attempts` w
  174. LEFT JOIN `users_main` um ON
  175. (um.`ID` = w.`UserID`)
  176. LEFT JOIN `ip_bans` ip ON
  177. (ip.`FromIP` = INET_ATON(w.`IP`))
  178. WHERE
  179. (
  180. w.`BannedUntil` > NOW()
  181. OR w.`LastAttempt` > NOW() - INTERVAL 6 HOUR
  182. )
  183. ORDER BY
  184. '$orderBy' '$orderWay'
  185. ");
  186. G::$DB->exec_prepared_query();
  187. return G::$DB->to_array('id', MYSQLI_ASSOC, false);
  188. }
  189. /**
  190. * Ban the IP addresses pointed to by the IDs that are on login watch.
  191. * @param array list of IDs to ban.
  192. * @return number of addresses banned
  193. */
  194. public function setBan(int $userId, string $reason, array $list): int
  195. {
  196. if (!$list) {
  197. return 0;
  198. }
  199. $reason = trim($reason);
  200. $n = 0;
  201. foreach ($list as $id) {
  202. $ipv4 = G::$DB->scalar("
  203. SELECT
  204. INET_ATON(`IP`)
  205. FROM
  206. `login_attempts`
  207. WHERE
  208. `ID` = '$id'
  209. ");
  210. G::$DB->prepared_query("
  211. INSERT IGNORE
  212. INTO `ip_bans`(`UserID`, `Reason`, `FromIP`, `ToIP`)
  213. VALUES('$userId', '$reason', '$ipv4', '$ipv4')
  214. ");
  215. G::$DB->exec_prepared_query();
  216. $n += $this->db->affected_rows();
  217. }
  218. return $n;
  219. }
  220. /**
  221. * Clear the list of IDs that are on login watch.
  222. * @param array list of IDs to clear.
  223. * @return number of rows removed
  224. */
  225. public function setClear(array $list): int
  226. {
  227. if (!$list) {
  228. return 0;
  229. }
  230. G::$DB->prepare_query("
  231. DELETE
  232. FROM
  233. `login_attempts`
  234. WHERE
  235. `ID` IN(".placeholders($list).")
  236. ", ...$list);
  237. G::$DB->exec_prepared_query();
  238. return G::$DB->affected_rows();
  239. }
  240. }