【软件安全】Web Security(Cookies / Session / XSS / SQL Injection / CSRF)概念介绍
🌐 Web Security(网页安全基础:Cookies / Session / XSS / SQL Injection / CSRF)
一、概念总览 / Concept Overview
English:
Web security is about protecting websites and users from attacks that exploit web technologies — especially how browsers, servers, and databases communicate.
中文:
网页安全是为了防止攻击者利用网站技术漏洞进行攻击,特别是保护浏览器、服务器与数据库之间的通信安全。
English:
Key areas include Cookies, Sessions, XSS (Cross-Site Scripting), SQL Injection, and CSRF (Cross-Site Request Forgery).
中文:
核心领域包括 Cookies、Session(会话)、跨站脚本(XSS)、SQL 注入(SQLi) 与 跨站请求伪造(CSRF)。
Cookies
English:
A cookie is a small piece of data stored in the browser to remember user preferences or login state.
中文:
Cookie 是存储在浏览器中的一小段数据,用于记住用户偏好或登录状态。
Example: login info, language settings.
例子: 登录信息、语言偏好。
💡 Metaphor:
A cookie is like a claim ticket at a dry cleaner — it tells the system who you are when you return.
比喻: Cookie 就像干洗店的取衣票,用来告诉系统“你是谁”。
Session
English:
A session stores user data temporarily on the server after login, often linked to a session ID stored in a cookie.
中文:
Session 是服务器端的临时数据,用于保存用户登录后的状态,通常与存储在 Cookie 中的 session ID 关联。
💡 Metaphor:
Like a cloakroom token — you give a ticket (session ID) and the server holds your coat (data).
比喻: Session 就像衣帽间的取衣牌,你拿着票(session ID),服务器保存你的外套(数据)。
XSS (Cross-Site Scripting)
English:
XSS happens when an attacker injects malicious scripts into a webpage viewed by other users.
中文:
跨站脚本攻击(XSS) 指攻击者向网页注入恶意脚本,当其他用户访问该网页时,脚本会在他们的浏览器中执行。
Impact: Stealing cookies, hijacking sessions, defacing sites.
影响: 窃取 Cookie、劫持会话、篡改网页内容。
💡 Metaphor:
Like sneaking a fake ad into a newspaper so readers see the attacker’s message instead.
比喻: 就像有人偷偷在报纸上贴假广告,让读者误以为是真的。
💉 SQL Injection (SQLi)
English:
SQL Injection happens when user input is inserted into database queries without proper validation, allowing attackers to manipulate SQL commands.
中文:
SQL 注入攻击(SQLi) 是指用户输入未经校验直接拼接到数据库查询语句中,导致攻击者能修改或窃取数据库内容。
Example:
SELECT * FROM users WHERE name = 'admin' OR '1'='1';
→ Logs in anyone.
💡 Metaphor:
Like giving someone a form to fill in, but they rewrite the form’s rules before submitting it.
比喻: 就像你让人填表,但他偷偷改了表格的规则。
CSRF (Cross-Site Request Forgery)
English:
CSRF tricks a logged-in user’s browser into sending unwanted actions (like money transfers) to a trusted site without their knowledge.
中文:
跨站请求伪造(CSRF) 通过欺骗已登录用户的浏览器,在用户不知情的情况下向可信网站发送恶意请求。
💡 Metaphor:
Like forging your signature on a check — the action looks valid but you didn’t authorize it.
比喻: 就像有人伪造了你的签名支票,看似合法但并非你本人操作。
二、选择题(Multiple Choice Questions)×5
Q1:
EN: What is stored in a browser to remember user preferences or login state?
CN: 浏览器中用于记住用户偏好或登录状态的是什么?
A. Session
B. Cookie ✅
C. Token
D. Cache
✅ Correct: B
Why: Cookies are small client-side data pieces used for remembering preferences or authentication.
为什么对: Cookie 是存储在客户端的小数据,用来记住登录或偏好信息。
Why wrong:
A ❌ Session 在服务器端。
C ❌ Token 是另一种认证机制。
D ❌ Cache 用于性能优化。
Q2:
EN: Which attack injects malicious scripts into web pages?
CN: 哪种攻击向网页注入恶意脚本?
A. SQLi
B. XSS ✅
C. CSRF
D. DoS
✅ Correct: B
Why: XSS allows attackers to run scripts in victims’ browsers.
为什么对: XSS 让攻击者能在受害者浏览器中执行恶意脚本。
Why wrong:
A ❌ SQLi 是数据库注入。
C ❌ CSRF 伪造请求。
D ❌ DoS 使服务不可用。
Q3:
EN: What is the best way to prevent SQL Injection?
CN: 防止 SQL 注入的最佳方法是什么?
A. Escape HTML
B. Use prepared statements ✅
C. Disable cookies
D. Hide database errors
✅ Correct: B
Why: Prepared statements separate SQL logic from user input.
为什么对: 预编译语句能将 SQL 逻辑与用户输入分离。
Why wrong:
A ❌ HTML 转义防 XSS。
C ❌ 与 SQL 无关。
D ❌ 只能掩盖,不是防护。
Q4:
EN: What does a CSRF attack rely on?
CN: CSRF 攻击依赖什么?
A. User’s active session ✅
B. Weak passwords
C. Open ports
D. Server downtime
✅ Correct: A
Why: CSRF works because the user is already authenticated in the target site.
为什么对: CSRF 成功依赖于用户已登录状态。
Why wrong:
B ❌ 密码强度无关。
C ❌ 与端口无关。
D ❌ 服务器宕机不构成攻击。
Q5:
EN: Which mechanism keeps user data temporarily after login?
CN: 哪种机制在登录后暂时保存用户数据?
A. Cookie
B. Session ✅
C. Cache
D. Local Storage
✅ Correct: B
Why: Sessions store user info server-side for ongoing interactions.
为什么对: Session 在服务器端保存用户信息,维持交互状态。
Why wrong:
A ❌ Cookie 存在浏览器。
C ❌ Cache 用于性能。
D ❌ Local Storage 永久保存数据。
✍️ 三、简答题(Short Answer Questions)×5
Q1:
EN: Explain the difference between Cookie and Session.
CN: 解释 Cookie 与 Session 的区别。
A: Cookie is stored on the client and holds small data like preferences; Session is stored on the server and manages user login state.
中文: Cookie 存在客户端,保存偏好信息;Session 存在服务器端,管理登录状态。
Q2:
EN: What makes XSS dangerous?
CN: 为什么 XSS 很危险?
A: It runs malicious code in the victim’s browser, stealing cookies or hijacking sessions.
中文: 它能在受害者浏览器中执行恶意代码,窃取 Cookie 或劫持 Session。
Q3:
EN: How does SQL Injection work in simple terms?
CN: 用简单的语言解释 SQL 注入的原理。
A: It inserts malicious SQL commands into a query by abusing unsanitized user input.
中文: 通过未过滤的输入插入恶意 SQL 命令,操纵数据库查询。
Q4:
EN: How can CSRF be prevented?
CN: 如何防止 CSRF?
A: By using anti-CSRF tokens, checking request origin, and requiring user re-authentication for sensitive actions.
中文: 使用防 CSRF 令牌、检查请求来源、敏感操作时重新验证用户身份。
Q5:
EN: Give a real-world analogy for XSS and CSRF.
CN: 给出 XSS 和 CSRF 的现实比喻。
A:
- XSS: Someone sneaks a malicious note into your notebook.
- CSRF: Someone forges your handwriting to sign a check.
中文: - XSS: 有人偷偷在你的笔记本里夹了恶意纸条。
- CSRF: 有人模仿你的笔迹伪造签名支票。
🧠 四、总结 / Summary
English:
Web security focuses on protecting communication and user trust. Attacks like XSS, SQLi, and CSRF exploit how websites handle user data, cookies, and sessions. Understanding these concepts is key to building secure web apps.
中文:
网页安全的核心是保护通信与用户信任。攻击如 XSS、SQL 注入和 CSRF 都是通过滥用网站对数据、Cookie 与 Session 的处理方式实现的。理解这些概念是构建安全网站的基础。
✅ Best Practices / 防护建议
- Use HTTPS for all communications.
- Sanitize input and escape output.
- Use prepared statements for database queries.
- Implement anti-CSRF tokens.
- Set cookie security flags (
HttpOnly,Secure,SameSite).
