diff --git a/.env.example b/.env.example index 7919cd6..379cebd 100644 --- a/.env.example +++ b/.env.example @@ -18,3 +18,12 @@ MINIBIN_NORMALIZE_MIME_TYPES=true # prevents uploads with the specified mime types, separated by commas # this example is pretty long, but it is majority (if not all) of the executable types github.com/gabriel-vasile/mimetype detects MINIBIN_BANNED_MIME_TYPES="application/vnd.microsoft.portable-executable,application/java-archive,application/x-executable,application/vnd.android.package-archive,application/x-ms-installer,application/x-elf,application/vnd.debian.binary-package" +# this is the amount of leading zero bits required for proof of work completion +# 8-12 is a good range, 8 being quickest, while 12 is the slowest +# with my computer 12 can take around 10-20 seconds +# whereas 8 takes around a few seconds or less +MINIBIN_POW_DIFFICULTY=10 +# comma separated options the user can select when reporting +# options show in the order they are written +# leading spaces WILL be included, so make sure you dont include them +MINIBIN_REPORT_OPTIONS="Malware (including links),CSAM/CSEM,Copyright Infringement,Fraud" \ No newline at end of file diff --git a/go.mod b/go.mod index 74660e0..fffd926 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gabriel-vasile/mimetype v1.4.16 github.com/glebarez/sqlite v1.11.0 github.com/joho/godotenv v1.5.1 + golang.org/x/crypto v0.59.0 gorm.io/gorm v1.31.2 ) @@ -17,8 +18,8 @@ require ( github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - golang.org/x/sys v0.7.0 // indirect - golang.org/x/text v0.41.0 // indirect + golang.org/x/sys v0.50.0 // indirect + golang.org/x/text v0.44.0 // indirect modernc.org/libc v1.22.5 // indirect modernc.org/mathutil v1.5.0 // indirect modernc.org/memory v1.5.0 // indirect diff --git a/go.sum b/go.sum index dc53943..8dc027c 100644 --- a/go.sum +++ b/go.sum @@ -23,11 +23,13 @@ github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxU github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/crypto v0.59.0 h1:xS2jhqYbzNrpvkWP2YTDf4TJwBy3ykR03i8vFDxEc7c= +golang.org/x/crypto v0.59.0/go.mod h1:veDyUwATGs3c+vN3VfTbzViMGXeTBk3WyhBOkn+F/4U= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= -golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= +golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs= +golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo= +golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY= gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo= diff --git a/internal/config/config.go b/internal/config/config.go index be317f5..938f1e2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -60,7 +60,7 @@ func LoadConfig() (Config, error) { expiries = append(expiries, expiry) } - powDifficultySetting := envOrDefault("MINIBIN_POW_DIFFICULTY", "18") + powDifficultySetting := envOrDefault("MINIBIN_POW_DIFFICULTY", "10") powDifficulty, err := strconv.Atoi(powDifficultySetting) if err != nil { return Config{}, fmt.Errorf("failed to parse MINIBIN_POW_DIFFICULTY") @@ -71,8 +71,6 @@ func LoadConfig() (Config, error) { return Config{}, fmt.Errorf("no valid report options set") } - log.Println(len(reportOptions)) - return Config{ UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"), MaxPasteBytes: maxPasteKiB * 1024, diff --git a/internal/reports/pow.go b/internal/reports/pow.go index f34721f..56293b3 100644 --- a/internal/reports/pow.go +++ b/internal/reports/pow.go @@ -2,8 +2,8 @@ package reports import ( "crypto/rand" - "crypto/sha256" "crypto/subtle" + "encoding/binary" "encoding/hex" "fmt" "log" @@ -11,6 +11,7 @@ import ( "time" "git.whoswhip.dev/whoswhip/minibin/internal/storage" + "golang.org/x/crypto/argon2" ) type Challenge struct { @@ -94,8 +95,10 @@ func (store *ChallengeStore) Verify(id string, result string, nonce int) (client if !hasLeadingZeroBits(hashBytes, challenge.Difficulty) { return true, fmt.Errorf("proof-of-work is not complete") } - input := fmt.Appendf(nil, "%s%d", hex.EncodeToString(challenge.Data), nonce) - localResult := sha256.Sum256(input) + input := []byte(hex.EncodeToString(challenge.Data)) + salt := make([]byte, 8) + binary.LittleEndian.PutUint64(salt, uint64(nonce)) + localResult := argon2.IDKey(input, salt, 2, 4096, 1, 32) if subtle.ConstantTimeCompare(localResult[:], hashBytes) != 1 { return true, fmt.Errorf("results do not match") } diff --git a/readme.md b/readme.md index 871986e..8968d2e 100644 --- a/readme.md +++ b/readme.md @@ -45,7 +45,7 @@ goals: - [ ] simplify rendering & serving pages (too much duplicate code rn) - [x] reports - [x] PoW challenges/captchas - - [ ] switch to argon2id instead of sha256 + - [x] switch to argon2id instead of sha256 - javascript required - [ ] ASN blocking via geolite2 diff --git a/web/static/js/pow-worker.js b/web/static/js/pow-worker.js index d834a67..9b07d02 100644 --- a/web/static/js/pow-worker.js +++ b/web/static/js/pow-worker.js @@ -1,3 +1,5 @@ +import { argon2id } from "https://esm.sh/hash-wasm@4.12.0"; + function hasLeadingZeroBits(bytes, requiredBits) { const whole = Math.floor(requiredBits / 8); const remaining = requiredBits % 8; @@ -15,9 +17,19 @@ self.onmessage = async ({ data }) => { console.log(`[PoW] Difficulty: ${difficulty}, Starting Nonce: ${startNonce}, Challenge: ${challenge}`) const startTime = performance.now(); const encoder = new TextEncoder(); + const input = encoder.encode(challenge); for (let nonce = startNonce; ; nonce++) { - const input = encoder.encode(`${challenge}${nonce}`); - const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", input)); + const salt = new Uint8Array(8); + new DataView(salt.buffer).setBigUint64(0, BigInt(nonce), true); + const digest = await argon2id({ + password: input, + salt, + parallelism: 1, + iterations: 2, + memorySize: 4096, + hashLength: 32, + outputType: "binary" + }); if (hasLeadingZeroBits(digest, difficulty)) { const hashHex = Array.from(digest).map((b) => b.toString(16).padStart(2, "0")).join(""); const elapsed = performance.now() - startTime; diff --git a/web/static/js/report.js b/web/static/js/report.js index f85b76f..231c768 100644 --- a/web/static/js/report.js +++ b/web/static/js/report.js @@ -1,4 +1,4 @@ -const worker = new Worker("/js/pow-worker.js"); +const worker = new Worker("/js/pow-worker.js", { type: "module" }); const reportForm = document.getElementById("report-form"); const submitBtn = document.getElementById("submit-btn"); const powLabel = document.getElementById("pow-label");