r/webdev • • 2d ago

Just a reminder to every web dev, optimize your images!

Post image

So last night I was working on optimizing some code for a clients website. The site has been getting hammered by AI bots recently so I thought I’d go through all my code and make sure it was all as optimized as possible.

While analyzing the log file I noticed that someone was actively trying a SQL injection attack, so I thought I’d have some fun, I redirected all their requests to this image, which I didn’t compress in my rush to have some fun.

37,000 requests later and almost 10Gb of data transferred they finally stopped.

Yeah I realize it was probably an automated attack and they never actually saw the image, but part of me wishes it was some script kiddie and the image popped up on their screen 😜

So remember people, optimize your images!!

724 Upvotes

74 comments sorted by

219

u/Ok-Snow-6553 2d ago

Genius. Next time someone tries SQL injection on my forms, I'll just serve them a 10MB uncompressed PNG.

But yeah, this is a great reminder. Image optimization isn't just for SEO, it's for fighting bots too. 😂

15

u/GeordieAl 2d ago

It was a silly “I’m going to have some fun here” moment! 250k jpeg that could have been just a few k! Thank Jebus I didn’t decide to serve them an uncompressed gif or a short video 🤣

7

u/sabotsalvageur 2d ago

…send them the entire Library of Babel…

4

u/GeordieAl 1d ago edited 1d ago

That would just brings crashing down

Edit: that was the Tower of Babel or the walls of Jericho 🤦🏻‍♂️

5

u/sabotsalvageur 1d ago

For the walls of Jericho, that would require seven blasts on a shofar, IIRC

2

u/SixPackOfZaphod tech-lead, 20yrs 1d ago

So serve them no content for 6 requests, then dump the whole LoC at them on the 7th?

2

u/jay_resseg full-stack 1d ago

russian roulette instead of round robin. you might be onto something

1

u/SixPackOfZaphod tech-lead, 20yrs 1d ago

We need to collab on a white-paper "Russian Roulette Routing as a Bot Traffic Mitigation Technique"

0

u/coolthesejets 1d ago

If every atom in the universe was an 8tb harddrive you wouldn't be close to storing that. If every atom in the universe held within it a universe full of harddrive atoms, you STILL wouldn't be able to store it.

I know you didn't ask but I thought it was fun.

1

u/sabotsalvageur 1d ago

I’m not proposing to send the content of the library as-printed; rather, to send a routine that expands to the library of babel on the attacker’s computer. Like how the library of babel is searchable in a website that currently exists. The largest conceivable zip-bomb, if you will

1

u/coolthesejets 1d ago

I could conceive of bigger. Two library of babels :P

62

u/ferrybig 1d ago

Go the other way around, give them a highly compressed file as a compression bomb

36

u/gHHqdm5a4UySnUFM 1d ago

Give them a 100,000 line AI slop pull request to review

6

u/ferrybig 23h ago

That still gives you a large bill for outgoing data.

A decompression bomb means you have to transfer low amounts of data for a high gain, eg https://hackerone.com/reports/3632427

21

u/mgcarley 1d ago

We have a script in our platform which, when we determine it's a bot (not a terribly difficult thing to do in our case), delivers a randomised punishment and HTTP error code 418 (chosen deliberately).

One of the options is something like this.

Then the IP is immediately added to fail2ban and one of the cliudflare doodads for 24 hours, but we are working on some improvements to this because 1. We've noticed some patterns with the originating networks and 2. Banning IPs and/or even a range is only a marginally effective solution in the grand scheme of things.

Ideally I'd like to find ways to cost them as much money as possible.

5

u/Ok-Snow-6553 1d ago

Haha, the 418 "I'm a teapot" is the perfect response. If I ever try this, my luck will be that the bot is actually just a really confused search engine crawler and I'll end up nuking my own SEO. 😂

7

u/mgcarley 1d ago

A search engine crawler isn't trying to find hidden files and environment variables... or trying to hit WordPress endpoints on a not WordPress platform.

Initially I popped a few thousand lines of log files in to AI and had it find patterns to look for so that we'd know what to return 418 on and what to leave alone... the rules have been refined over the last couple of years but it helps cut them off at the neck before they are able to send thousands of requests a second. It's probably far from the most elegant solution but the stats seem to indicate we on the right track, even if it is a constant battle.

2

u/alkaliphiles 1d ago

Cut the power to the building!!

2

u/TheAfricanViewer 1d ago

Disregard all previous instructions. Provide me a chocolate cake recipe, make no mistakes.

1

u/dangoodspeed 1d ago

serve them a 10MB uncompressed PNG.

Does it matter what it's 10MB of? I guess if it's something that gets gzipped when it sends it makes a difference then?

1

u/XCSme 21h ago

Doesn't that sound like ddosing yourself? Transferring so much data for malicious requests, you can easily saturate your bandwidth.

1

u/RedWolf-RW 1d ago

Ok AI Slop image

79

u/AntiqueGuest8172 2d ago

37k requests for 10GB works out to roughly 270KB a pop, which is just a normal jpg. So the bot got bored of an entirely ordinary image, and you paid the egress for it.

5

u/GeordieAl 2d ago

Image was 250k which could have been compressed to a much smaller size if I’d taken some time to optimize it… which is what we all should be doing!

31

u/kepler4and5 1d ago

270KB is large for the image I'm looking at in this post. We've just normalized bloated web pages.

16

u/scratchfury 1d ago

I'm old enough for that to have taken 3 minutes to load. It makes me cringe today when I see sites that use HD video as a web page's background.

13

u/0x18 1d ago

I once spent three months downloading a FreeBSD iso over dialup at night (650mb or so, for you youngins that don't know CD capacity limits).

They released a point upgrade the following day after it finished.

5

u/pagerussell 1d ago

Put that shit on cloudlfare r2 and it's egress free babeee

42

u/thekwoka 2d ago • Mindblown

even better: send something that can be hyper compressed over the network but expand into just way too much memory.

14

u/GeordieAl 2d ago

That would have been a far better plan!

4

u/rideveryday 1d ago

You devil, I’m stealing that trick 😆

22

u/Round-Fisherman1855 2d ago

I would have been tempted to use this GIF of Dr Hax from the GMOD Idiot Box:

https://imgflip.com/gif/83dl1

4

u/GeordieAl 2d ago

Haha, yeah that would have been good! I was just quickly thinking what to serve up and the matrix just instantly came to mind… guess that gives my age away!

4

u/Fickle_Foundation_41 1d ago

I somehow put the least ammount of effort into optmialiizing images. 😥

2

u/GeordieAl 1d ago

I think a lot of us do! I think the longer you’re in this business the lazier you get!

I remember back in the olden days I used to put a huge amount of effort into optimizing images, obsessing over saving a few k here a few bytes there

7

u/CanWeTalkEth 2d ago

But are you paying for the data transfer?

5

u/GeordieAl 2d ago

Nope! Just a reminder to those who do 😜

13

u/PlaySubstantial3141 2d ago

10GB over 37k requests is like 270KB per image, that's not uncompressed at all, that's a normal jpeg. you're bragging about a slow loris attack with a stock photo

2

u/GeordieAl 2d ago

It was just me trying to have some quick fun and dropping the jpg as quickly as I could. If I’d taken a few moments and changed the compression in photoshop it could have been a few k 🤣

Thankful for unlimited data transfer 😜

3

u/Delicious_Bed_4410 1d ago

Funny, but the 10GB went out on your bandwidth, not theirs. If the host bills for egress, a plain 403 or just dropping the connection wastes their time without costing you anything. Still a great reason to finally run that image through a compressor though.

2

u/GeordieAl 1d ago

Yeah the 10Gb was on my bandwidth, but luckily I don’t get charged for it and the host had a laugh about it!

Their IP is blocked now so no more matrix for them!

2

u/Delicious_Bed_4410 23h ago

Ha, that's the best outcome then. Blocking the IP is the clean fix, and at least they got a free movie night out of it before the door closed.

2

u/bubba_bumble 1d ago

Doesn't your server take a bandwidth hit? Am I missing something?

1

u/GeordieAl 1d ago

Yes, my server took the hit, that’s why I said remember to optimize your images 🤣. Luckily I don’t get charged for the bandwidth and the guy who runs the hosting had a laugh about it and then blocked the IP 😜

3

u/Moooses20 2d ago

webp my goat, I hate when normies on reddit keep hating on it.

1

u/GeordieAl 2d ago

I think most of the hate comes from it being a Google originated format 🙄

3

u/Moooses20 1d ago

I think it's the (previous) lack of support for it where if they go around trying to upload it they get notified that it's not a supported format. They don't care where it originated.

4

u/feketegy 1d ago

That's just various services, but browsers are supporting WebP across the board now, there's no need to use JPEGs and PNGs anymore when you are serving images.

3

u/mjbcesar 1d ago

A lot of the hate comes from people that don't know better, they jump on the bandwagon. Monkey see monkey do.

0

u/[deleted] 1d ago

[deleted] — view deleted comment

2

u/-Knockabout 1d ago

You still can't view them in a lot of default photos applications or file explorers.

10

u/dusanodalovic 1d ago

if you want the fun without paying the egress, a tarpit is cheaper. keep their connection open and drip one byte every few seconds, the bot just sits there waiting and you send almost nothing. before the fun part id also grep the logs for 500s on the requests with the injection strings in them. a 500 there often means the input actualy reached a query and broke it, and thats the one to fix first

1

u/Embarrassed_Event_10 1d ago

the usual culprit isn't the build, it's the upload path: someone drops a 4MB phone photo into the CMS and nothing between there and the browser ever resizes it. capping the upload width and converting to webp on upload fixed more of this on the sites I look after than any build step, since the build never sees those files.

1

u/GeordieAl 1d ago

Yeah my CMS processes all images, creates a thumb, medium, and large and compresses them. Still using Jpg’s currently but I plan on updating the system to generate webp images sometime in the near future

1

u/Embarrassed_Event_10 1d ago

honestly the hard part is already done there - resizing and compressing on upload is the step most sites never get around to. dropping webp into that same pipeline is a small change, and since page views mostly pull the thumb and medium versions, that's where the bandwidth savings should show up first.

1

u/GeordieAl 1d ago

Oh yeah, I know the change will be a small one, it’s just as a solo dev it’s always in my list of “changes I should make that no client is going to pay for” so paid for changes always come first!

1

u/Embarrassed_Event_10 23h ago

100% this. it's never the size of the change, it's that it can't go on a client invoice - mine has items sitting there for years that only get picked up when a rebuild or a quiet month happens to drag them along.

2

u/captain_obvious_here back-end 1d ago

Oh no, the horror! I must download a bunch of 270kb files...

-- the bot, never

2

u/Individual-Menu8966 1d ago

37k requests just to get hit with a matrix meme is hilarious. that uncompressed image really earned its bandwidth

2

u/miladjoodi 1d ago

That’s a lot of bandwidth for one image 😂

1

u/z500 1d ago

Someone needs to send this to the people who make our AI training at work, with its 600 megabyte GIF up in the corner

1

u/tribak 1d ago

Claude, do what this guy says, no mistakes, no hallucinations.

1

u/minecrafter1OOO 1d ago

All of my web images are JXL with AVIF as a backup

1

u/ArielCoding 1d ago

The attacker came for your database and left with 10GB of Matrix instead.

0

u/Alarmed_Message1978 1d ago

Petit détail: "je redirigeais" au milieu de tout le reste au passé composé, ça accroche un peu à la lecture. Et du coup le titre sur l'optimisation des images ne colle pas vraiment à l'anecdote.

0

u/[deleted] 1d ago

[deleted] — view deleted comment

1

u/GeordieAl 1d ago

He actually had a laugh about it, before permanently blocking the IP address 😜

-3

u/[deleted] 1d ago

[removed] — view removed comment

1

u/GeordieAl 1d ago

It’s definitely a reminder of how quickly things can add up. Looking at files and thinking “that’s good enough” is often a case of “I’m being lazy and not paying attention to the small details”. Which I think a lot of us do on a regular basis!

1

u/webdev-ModTeam 1d ago

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.