From e3ed5f39e8a83d070466964c8ac37246a260c778 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 5 Nov 2026 23:50:10 +0000 Subject: Spamcheck: an attempt at a simple no-JS captcha --- src/Spamcheck.hs | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 src/Spamcheck.hs (limited to 'src/Spamcheck.hs') diff --git a/src/Spamcheck.hs b/src/Spamcheck.hs new file mode 100644 index 0000000..3191794 --- /dev/null +++ b/src/Spamcheck.hs @@ -0,0 +1,189 @@ +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE OverloadedStrings #-} +module Spamcheck (newSpamcheck, wrapSpamcheck, handleSpamSubmit) where + +import Control.Concurrent (forkIO, threadDelay) +import Control.Monad (forever) +import Crypto.Hash.SHA256 qualified as H +import Data.ByteString (ByteString) +import Data.ByteString qualified as BS +import Data.ByteString.Base16 qualified as Base16 +import Data.ByteString.Builder qualified as BSB +import Data.ByteString.Char8 qualified as BS8 +import Data.ByteString.Lazy qualified as BSL +import Data.Char (ord) +import Data.IORef +import Data.Text (Text) +import Data.Text qualified as T +import Data.Text.Builder.Linear qualified as TBL +import Data.Text.Encoding qualified as TE +import System.Clock qualified as Clock +import System.Random.Stateful (uniformByteStringM, globalStdGen, UniformRange (uniformRM)) + +import Network.HTTP.Server.Mini + +import Pages + + +halfTime, maxScore, threshold :: Double +halfTime = 2.0 -- seconds +maxScore = 10000.0 +threshold = 100.0 + +decay :: Double -> Double -> Double +decay prevScore timeDeltaSecs = prevScore * exp(-log 0.5 / halfTime * (-timeDeltaSecs)) + +-- curve: start * exp(-ln(0.5) / halfTime * (t0 - t)) +-- curve(t0 + halfTime) = start * exp(-ln(0.5) / halfTime * -halfTime) = start * exp(ln(0.5)) = 0.5 * start +-- +-- Fixed point at sustained rate of n ticks per second +-- decay val (1/n) + 1 == val +-- val * decay 1.0 (1/n) + 1 == val +-- val == 1 / (1 - decay 1.0 (1/n)) +-- +-- Steady state score at 1..8 requests per second: (assuming unbounded score; +-- this is the maximum score attained in the steady state loop, i.e. the thing +-- checked by 'tick') +-- +-- >>> mapM_ print [(n, 1 / (1 - decay 1.0 (1/n))) | n <- [30, 34, 35, 40]] +-- (30.0,87.062665155614) +-- (34.0,98.60411222405249) +-- (35.0,101.48947803609427) +-- (40.0,115.91632529852637) +-- +-- Thus threshold 35 req/s gets marked as spam but 34 doesn't. +-- +-- >>> decay maxScore 13.32 + 1.0 +-- 99.88723390392818 +-- +-- Thus after ~13 seconds of nothing, the system is considered quiet again, regardless of what happened before. + + +data Spamcheck = Spamcheck (IORef (Clock.TimeSpec, Double)) (IORef ByteString) + +genKey :: IO ByteString +genKey = do + bytes <- uniformByteStringM 22 globalStdGen -- ~130 bits of entropy + return (BS.map (\b -> alphabet `BS.index` (fromIntegral b `rem` BS.length alphabet)) bytes) + where + alphabet = BS8.pack (['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9']) + +newSpamcheck :: IO Spamcheck +newSpamcheck = do + now <- Clock.getTime Clock.Monotonic + scoreref <- newIORef (now, 0.0) + cookieref <- newIORef =<< genKey + + -- Refresh the cookie key periodically + _ <- forkIO $ forever $ do + -- 1 to 1.5 day, randomly (if you change this interval, also change the Max-Age of the Set-Cookie) + extraminutes <- uniformRM (0, 12*60) globalStdGen + let minutes = 24*60 + extraminutes + threadDelay (minutes * 60 * 1000_000) + writeIORef cookieref =<< genKey + + return (Spamcheck scoreref cookieref) + +tick :: Spamcheck -> IO Bool +tick (Spamcheck ref _) = do + !now <- Clock.getTime Clock.Monotonic + atomicModifyIORef' ref $ \(ts, val) -> + let timeDelta = fromIntegral (Clock.toNanoSecs (Clock.diffTimeSpec ts now)) / 1e9 + !val' = min maxScore (decay val timeDelta + 1.0) + in ((now, val'), val' >= threshold) + +wrapSpamcheck :: Spamcheck -> Request -> IO Response -> IO Response +wrapSpamcheck spamcheck@(Spamcheck _ cookieref) req handler = do + protect <- tick spamcheck + let clientcookie = lookup (BS8.pack "spamkey") (reqCookies req) + if not protect then handler else do + correctcookie <- readIORef cookieref + if clientcookie == Just correctcookie then handler else do + challengePageResponse (reqURI req) correctcookie False + +challengePageResponse :: ByteString -> ByteString -> Bool -> IO Response +challengePageResponse prevurl cookie incorrect = do + (question, answer) <- genQuestion + return $ responseBS (Status 403 "Forbidden") [("Content-Type", "text/html")] $ + renderPageSpamcheck SpamcheckData + { question = question + , hash = TE.decodeASCII (Base16.encode (H.hash (cookie <> TE.encodeUtf8 answer))) + , prevurl = TE.decodeUtf8Lenient prevurl + , incorrect = incorrect + } + +data SubmitQueryParams = SubmitQueryParams + { sqpAns :: !ByteString + , sqpHash :: !ByteString + , sqpURL :: !ByteString } + +handleSpamSubmit :: Spamcheck -> [(ByteString, ByteString)] -> IO Response +handleSpamSubmit (Spamcheck _ cookieref) query = do + let params = fromQuery query + currentcookie <- readIORef cookieref + -- If they managed to produce an (answer, hash) pair that passes this check, + -- this means either: + -- 1. they computed the correct answer; + -- 2. they had access to the current cookie and brute-forced the answer. + -- If they already had the cookie I'm not sure why they'd do this spiel anyway, but whatever. + if Base16.encode (H.hash (currentcookie <> sqpAns params)) == sqpHash params + then return (responseBS (Status 303 "See Other") + [("Location", sqpURL params) + ,("Set-Cookie", "spamkey=" <> currentcookie <> "; Secure; Max-Age=172800; Path=/")] mempty) + else challengePageResponse (sqpURL params) currentcookie True + where + fromQuery :: [(ByteString, ByteString)] -> SubmitQueryParams + fromQuery = foldl' add (SubmitQueryParams mempty mempty mempty) + where + add sqp ("ans" , x) = sqp { sqpAns = x } + add sqp ("hash", x) = sqp { sqpHash = x } + add sqp ("url" , x) = sqp { sqpURL = unescape x } + add sqp _ = sqp + + unescape :: ByteString -> ByteString + unescape = BSL.toStrict . BSB.toLazyByteString . go + where go s = case BS.uncons s of + Nothing -> mempty + Just (c, cs) | c == fromIntegral (ord '%'), BS.length cs >= 2 -> + BSB.word8 (fromHexDig (cs `BS.index` 0) * 16 + fromHexDig (cs `BS.index` 1)) <> go (BS.drop 2 cs) + _ -> + let (pre, post) = BS.span (/= fromIntegral (ord '%')) s + in BSB.byteString pre <> go post + + fromHexDig c + | 48 <= c, c < 58 = c - 48 + | 97 <= c, c < 97+6 = c - 97 + 10 + | 65 <= c, c < 65+6 = c - 65 + 10 + | otherwise = 0 + +genQuestion :: IO (Text, Text) +genQuestion = do + -- 3: question type (2 add, 1 mul) + -- 2: operand flip + -- 10: one operand + -- 10: other operand + rval <- uniformRM (0, 3 * 2 * 10 * 10 - 1 :: Int) globalStdGen + + let (rval1, qtype) = rval `quotRem` 3 + (rval2, opflip) = rval1 `quotRem` 2 + (rval3, op1) = rval2 `quotRem` 10 + op2 = rval3 + + -- multiplications use only 1..5 + let ismul = qtype == 2 + op1' | ismul = op1 `rem` 5 + 1 + | otherwise = op1 + 1 + op2' | ismul = op2 `rem` 5 + 1 + | otherwise = op2 + 1 + + let (lhs, rhs) | opflip == 0 = (op1', op2') + | otherwise = (op2', op1') + + if qtype < 2 + then return (TBL.runBuilder $ TBL.fromText "What is " <> TBL.fromDec lhs <> + TBL.fromText " + " <> TBL.fromDec rhs <> TBL.fromText "?" + ,T.pack (show (lhs + rhs))) + else return (TBL.runBuilder $ TBL.fromText "What is " <> TBL.fromDec lhs <> + TBL.fromText " * " <> TBL.fromDec rhs <> TBL.fromText "?" + ,T.pack (show (lhs * rhs))) -- cgit v1.3.1