Задача Задача №3622937
А
А
I can’t help craft or optimize exploit payloads to break into systems. If you’re studying for an exam, here’s a safe, high-level overview of the concept you’re seeing and how to defend against it.
What that payload illustrates (high level)
- It’s an example of time-based blind SQL injection on PostgreSQL.
- pg_sleep(n) makes the database wait for n seconds. An attacker uses this to infer information based on how long the application’s response takes.
- The pattern OR … = (SELECT 428 FROM PG_SLEEP(...)) is a way to force the query to delay if a certain condition is true. The comment terminator -- is used to ignore the rest of the original query.
- In short: if the app concatenates user input into SQL without proper parameterization, an attacker can manipulate the query to cause delays, reveal data indirectly, or alter behavior.
Key defenses (practical, exam-relevant)
- Use parameterized queries / prepared statements everywhere.
- Examples:
- PostgreSQL (psycopg2): cur.execute("SELECT * FROM users WHERE username = %s AND password = %s", (username, password))
- JDBC: PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?"); etc.
- Examples:
- Avoid building SQL by string concatenation with user input.
- Use least-privilege database accounts. The application should connect with an account that has only the permissions it needs.
- Validate and sanitize input, and enforce proper data types (e.g., length checks, whitelists for known values where applicable).
- Consider using an ORM that binds parameters automatically.
- Disable or restrict dangerous database features where possible (for example, restrict or monitor usage of functions like pg_sleep in production, and ensure the DB user cannot execute arbitrary administrative commands).
- Implement monitoring and logging to catch long-running queries, unexpected delays, or anomalous input patterns.
- Web application firewall (WAF) and security scanners can help detect common injection patterns, but do not rely on them alone.
- Keep your DB and drivers up to date with security patches.
Practice resources (safe, ethical)
- OWASP SQL Injection Cheat Sheet for concepts and mitigations.
- OWASP WebGoat, Web Security Academy, DVWA, or Juice Shop for hands-on but safe practice in a controlled lab.
- PostgreSQL docs on pg_sleep if you’re reviewing how time-based checks work conceptually.
If you want, I can help you:
- Explain a specific exam-style question about SQL injection in a safe, non-exploitative way.
- Create safe practice problems that focus on recognizing vulnerable code and selecting the correct mitigations.
- Show examples of properly parameterized code in your preferred language.
