paint-brush
Botiem patīk tērēt jūsu servera resursus — lūk, kā cīnītiesautors@wasteofserver
Jauna vēsture

Botiem patīk tērēt jūsu servera resursus — lūk, kā cīnīties

autors Frankie6m2025/02/20
Read on Terminal Reader

Pārāk ilgi; Lasīt

Izmantojiet HAProxy, lai ierobežotu robotprogrammatūras, kas pastāvīgi ietekmē jūsu serveri uz 404 URL.
featured image - Botiem patīk tērēt jūsu servera resursus — lūk, kā cīnīties
Frankie HackerNoon profile picture
0-item

Ja pārvaldāt publisku tīmekļa serveri, būsiet pamanījis, ka robotprogrammatūra izmēģina savu veiksmi neskaitāmās neeksistējošās lapās. Izmantojiet HAProxy, lai apturētu tos pie durvīm.


Piezīme. Šajā rakstā esošās saites ietver saistīto uzņēmumu saites uz Amazon. Par noklikšķināšanu uz šīm saitēm jums netiks iekasēta papildu maksa, taču jūs mani atbalstīsit, ja izvēlēsities kaut ko iegādāties, izmantojot kādu no šīm saitēm. Paldies!


Jūsu žurnāli ir aizpildīti ar 404 trāpījumiem vietnēs /.DS_Store , /backup.sql , /.vscode/sftp.json un daudzos citos URL. Lai gan šie pieprasījumi lielākoties ir nekaitīgi, ja vien, protams, jūsu serverim patiešām nav ko piedāvāt šajās vietās, jums vajadzētu nomierināt robotprogrammatūras.


Kāpēc?


Servera trāpīšana ir resursietilpīgs uzdevums, un, ņemot vērā, ka šiem robotiem ir plašs dažādu URL saraksts, nav neviena kešatmiņas mehānisma, kas varētu jums palīdzēt. Turklāt robotu apturēšana vienmēr ir drošības pasākums.


Mēs iepriekš esam izmantojuši HAProxy, lai mazinātu uzbrukumus Wordpress pieteikšanās lapai , ideja ir paplašināt šo pieeju, lai aptvertu arī 404 kļūdas.


Bots will try their best to create havoc in your server

Boti darīs visu iespējamo, lai radītu postījumus jūsu serverī

Esmu iedvesmojies no Sasa Tekovic , proti, nebloķējot faktiskās meklētājprogrammas rāpuļprogrammas un ļaujot 404 statiskajiem resursiem, lai nepieļautu, ka faktiski trūkstošie resursi nebloķē likumīgos lietotājus.


Pirms ieviešanas vienmēr ir lietderīgi izveidot vietējo testa vidi. Sāksim HAProxy un Apache , izmantojot Docker . Mums ir nepieciešams faktisks aizmugursistēmas serveris, lai sniegtu mums šos 404 .


 version : '3' services: haproxy: image: haproxy:3.1.3-alpine ports: - "8100:80" volumes: - "./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg" networks: - webnet apache: image: httpd:latest container_name: apache1 ports: - "8080:80" volumes: - ./html:/usr/local/apache2/htdocs/ networks: - webnet networks: webnet:


Pēc tam vienkārši palaidiet docker-compose up un savā pārlūkprogrammā varēsiet piekļūt localhost:8100 .


Fails haproxy.cfg ir diezgan pašsaprotams:


 global log stdout format raw daemon debug defaults log global mode http frontend main bind *:80 acl static_file path_end .css .js .jpg .jpeg .gif .ico .png .bmp .webp .csv .ttf .woff .svg .svgz acl excluded_user_agent hdr_reg(user-agent) -i (yahoo|yandex|kagi|(google|bing)bot) # tracks IPs but exclude hits on static files and search engine crawlers http-request track-sc0 src table mock_404_tracking if !static_file !excluded_user_agent # increment gpc0 if response code was 404 http-response sc-inc-gpc0(0) if { status 404 } # checks if the 404 error rate limit was exceeded http-request deny deny_status 403 content-type text/html lf-string "404 abuse" if { sc0_gpc0_rate(mock_404_tracking) ge 5 } # whatever backend you're using use_backend apache_servers backend apache_servers server apache1 apache1:80 maxconn 32 # mock backend to hold a stick table backend mock_404_tracking stick-table type ip size 100k expire 10m store gpc0,gpc0_rate(1m)


Ja vienā minūtē saņemat vairāk nekā 5 trāpījumus 404 pieprasījumos, robots tiks bloķēts uz 10 minūtēm.




Pašreizējā stāvoklī šī iestatīšana efektīvi ierobežo robotu ātrumu, kas rada pārmērīgu 404 s. Tomēr mēs vēlamies to integrēt arī mūsu iepriekšējā piemērā, kur izmantojām HAProxy , lai bloķētu uzbrukumus WordPress .


 global log stdout format raw daemon debug defaults log global mode http frontend main bind *:80 # We may, or may not, be running this with Cloudflare acting as a CDN. # If Cloudflare is in front of our servers, user/bot IP will be in # 'CF-Connecting-IP', otherwise user IP with be in 'src'. So we make # sure to set a variable 'txn.actual_ip' that has the IP, no matter what http-request set-var(txn.actual_ip) hdr_ip(CF-Connecting-IP) if { hdr(CF-Connecting-IP) -m found } http-request set-var(txn.actual_ip) src if !{ hdr(CF-Connecting-IP) -m found } # gets the actual IP on logs log-format "%ci\ %hr\ %ft\ %b/%s\ %Tw/%Tc/%Tt\ %B\ %ts\ %r\ %ST\ %Tr IP:%{+Q}[var(txn.actual_ip)]" # common static files where we may get 404 errors and also common search engine # crawlers that we don't want blocked acl static_file path_end .css .js .jpg .jpeg .gif .ico .png .bmp .webp .csv .ttf .woff .svg .svgz acl excluded_user_agent hdr_reg(user-agent) -i (yahoo|yandex|kagi|google|bing) # paths where we will rate limit users to prevent Wordpress abuse acl is_wp_login path_end -i /wp-login.php /xmlrpc.php /xmrlpc.php acl is_post method POST # 404 abuse blocker # track IPs but exclude hits on static files and search engine crawlers # increment gpc0 counter if response status was 404 and deny if rate exceeded http-request track-sc0 var(txn.actual_ip) table mock_404_track if !static_file !excluded_user_agent http-response sc-inc-gpc0(0) if { status 404 } http-request deny deny_status 403 content-type text/html lf-string "404 abuse" if { sc0_gpc0_rate(mock_404_track) ge 5 } # wordpress abuse blocker # track IPs if the request hits one of the monitored paths with a POST request # increment gpc1 counter if path was hit and deny if rate exceeded http-request track-sc1 var(txn.actual_ip) table mock_wplogin_track if is_wp_login is_post http-request sc-inc-gpc1(1) if is_wp_login is_post http-request deny deny_status 403 content-type text/html lf-string "login abuse" if { sc1_gpc1_rate(mock_wplogin_track) ge 5 } # your backend, here using apache for demonstration purposes use_backend apache_servers backend apache_servers server apache1 apache1:80 maxconn 32 # mock backends for storing sticky tables backend mock_404_track stick-table type ip size 100k expire 10m store gpc0,gpc0_rate(1m) backend mock_wplogin_track stick-table type ip size 100k expire 10m store gpc1,gpc1_rate(1m)


Skriešana ar diviem stick tables un abu draudu apturēšana.


Un tur jums tas ir. HAProxy atkal tiek izmantots daudz vairāk nekā vienkāršs reversais starpniekserveris. Tas ir mazs Šveices nazis!



Šis galvenais lukturis ir mainījis spēli remontdarbu laikā.


Man tāds bija, bet, kad tas salūza, vilcinājos to nomainīt, ķērās pie tālruņa lukturīša. Un, protams, tas darbojas, taču, tiklīdz jūs atkal izjutīsit abas rokas brīvas , atpakaļceļa vairs nav. Ja jums ir nepieciešams uzticams brīvroku apgaismojums, tas ir obligāts!


Šī ziņa sākotnēji tika ievietota vietnē https://wasteofserver.com/stop-404-prying-bots-with-haproxy/ , iespējams, tur atradīsit jaunākas versijas un papildu komentārus.


L O A D I N G
. . . comments & more!

About Author

Frankie HackerNoon profile picture
I can still remember when you could activate CPU turbo by pressing a button on the case.

PAKARINĀT TAGUS

ŠIS RAKSTS TIKS PĀRSTRĀDĀTS...