<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>CS 161: Computer Security on</title><link>https://notes.bencuan.me/cs161/</link><description>Recent content in CS 161: Computer Security on</description><generator>Hugo</generator><language>en</language><atom:link href="https://notes.bencuan.me/cs161/index.xml" rel="self" type="application/rss+xml"/><item><title>Security Principles</title><link>https://notes.bencuan.me/cs161/Security-Principles/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Security-Principles/</guid><description>&lt;h2 id="what-is-security"&gt;
 What is security?
 &lt;a class="anchor" href="#what-is-security"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Security is enforcing a desired property (confidentiality, privacy, integrity, authentication, availability) in the presence of attackers.&lt;/p&gt;
&lt;p&gt;Some things that security helps protect:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Physical safety (prevent disruption of cars, planes, etc)&lt;/li&gt;
&lt;li&gt;Personal information (health records&amp;hellip;)&lt;/li&gt;
&lt;li&gt;National security&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Everything is hackable,&lt;/strong&gt; especially if it&amp;rsquo;s connected to the internet.&lt;/p&gt;
&lt;h2 id="threat-model"&gt;
 Threat Model
 &lt;a class="anchor" href="#threat-model"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;It is important to understand the motives and methods behind attacks, and make general assumptions about them to be safe. A &lt;strong&gt;threat model&lt;/strong&gt; is a model of the attacker and the resources they have.&lt;/p&gt;</description></item><item><title>x86</title><link>https://notes.bencuan.me/cs161/x86/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/x86/</guid><description>&lt;h2 id="memory-layout"&gt;
 Memory Layout
 &lt;a class="anchor" href="#memory-layout"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Code/text: program code&lt;/li&gt;
&lt;li&gt;Data: static variables allocated on start&lt;/li&gt;
&lt;li&gt;Heap: dynamic memory allocated with &lt;code&gt;alloc&lt;/code&gt; and &lt;code&gt;free&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Stack: local variables and stack frames&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;![[/cs161/img/x86/Untitled.png]]&lt;/p&gt;
&lt;h2 id="basics"&gt;
 Basics
 &lt;a class="anchor" href="#basics"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;x86 is a commonly used instruction set in consumer computers.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;x86 is &lt;strong&gt;little endian&lt;/strong&gt; (smallest bit on lowest memory address)&lt;/li&gt;
&lt;li&gt;x86 has &lt;strong&gt;variable length instructions&lt;/strong&gt; that can be from 1 to 16 bytes long.&lt;/li&gt;
&lt;li&gt;x86 has &lt;strong&gt;8 general purpose registers&lt;/strong&gt; (EAX, EBX, ECX, EDX, ESI, EDI), a stack pointer ESP (sp in riscv), a base pointer EBP (fp in riscv), and an instruction pointer register EIP (pc in riscv)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="syntax"&gt;
 Syntax
 &lt;a class="anchor" href="#syntax"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Registers: mod signs (&lt;code&gt;%eax, %esp, %edi&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Immediates: dollar signs (&lt;code&gt;$1, $0x4&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Memory reference: &lt;code&gt;8(%esp)&lt;/code&gt; gets 8 bytes past esp location&lt;/li&gt;
&lt;li&gt;Example instruction: &lt;code&gt;add $0x8, %ebx&lt;/code&gt; adds 8 to ebx&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="stack-frames"&gt;
 Stack Frames
 &lt;a class="anchor" href="#stack-frames"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;stack frame&lt;/strong&gt; of a function is the space on the stack allocated for local variables. It goes away upon return from the function.&lt;/p&gt;</description></item><item><title>Memory Safety Vulnerabilities</title><link>https://notes.bencuan.me/cs161/Memory-Safety-Vulnerabilities/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Memory-Safety-Vulnerabilities/</guid><description>&lt;h1 id="buffer-overflow-attacks"&gt;
 Buffer Overflow Attacks
 &lt;a class="anchor" href="#buffer-overflow-attacks"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;p&gt;(For the underlying C memory model — pointers, addresses, stack layout — see [[cs61c/Memory, Pointers, Addresses]] and [[cs61c/Memory Management]]. Most of these attacks exploit unchecked pointer arithmetic on the runtime stack.)&lt;/p&gt;
&lt;h2 id="out-of-bounds-write"&gt;
 out-of-bounds write
 &lt;a class="anchor" href="#out-of-bounds-write"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Overwriting bytes in a later position by making the current value much larger than anticipated.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This works because C doesn&amp;rsquo;t check memory boundaries.&lt;/li&gt;
&lt;li&gt;Vulnerable code: &lt;code&gt;gets()&lt;/code&gt; writes bytes until it encounters a newline. If there&amp;rsquo;s no newline, then it may overwrite far more than intended.&lt;/li&gt;
&lt;li&gt;The following is an example. Attackers can simply write 21 ones into &lt;code&gt;name&lt;/code&gt; and write a 1 into &lt;code&gt;authenticated&lt;/code&gt;, thus logging in.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;char&lt;/span&gt; name[&lt;span style="color:#ae81ff"&gt;20&lt;/span&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; authenticated &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;bad&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	...
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#a6e22e"&gt;gets&lt;/span&gt;(name);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ...
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="stack-smashing"&gt;
 Stack Smashing
 &lt;a class="anchor" href="#stack-smashing"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When overflows overwrite RIP to jump to a new address and run a custom script.&lt;/p&gt;</description></item><item><title>Heap Vulnerabilities</title><link>https://notes.bencuan.me/cs161/Heap-Vulnerabilities/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Heap-Vulnerabilities/</guid><description>&lt;h2 id="c-vtables"&gt;
 C++ Vtables
 &lt;a class="anchor" href="#c-vtables"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;C++ is an object oriented language that has polymorphism, instance variables, methods, etc.&lt;/p&gt;
&lt;p&gt;To support these features, every class has a &lt;strong&gt;vtable&lt;/strong&gt; (function pointer table). Each object then points to its vtable, where pointers can be dereferenced from.&lt;/p&gt;
&lt;p&gt;![[/cs161/img/Heap-Vulnerabilities/Untitled.png]]&lt;/p&gt;
&lt;p&gt;Vtables exist in the heap. Vulnerability: add shellcode and point to it&lt;/p&gt;
&lt;p&gt;![[/cs161/img/Heap-Vulnerabilities/Untitled 1.png]]&lt;/p&gt;
&lt;h2 id="heap-overflow"&gt;
 Heap Overflow
 &lt;a class="anchor" href="#heap-overflow"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Objects are allocated in the heap (malloc), but writes to a buffer in the heap are not checked. Attackers can overflow the heap and overwrite the vtable pointer of the next object to point to a malicious vtable.&lt;/p&gt;</description></item><item><title>Memory Safety Mitigations</title><link>https://notes.bencuan.me/cs161/Memory-Safety-Mitigations/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Memory-Safety-Mitigations/</guid><description>&lt;p&gt;&lt;strong&gt;How do we protect against memory safety vulnerabilities?&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="memory-safe-languages"&gt;
 Memory Safe Languages
 &lt;a class="anchor" href="#memory-safe-languages"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Memory safe languages check bounds and prevent undefined memory accesses, and therefore are immune to memory safety vulnerabilities.&lt;/p&gt;
&lt;p&gt;Examples: Java, Python, C#&amp;hellip;&lt;/p&gt;
&lt;p&gt;However, memory safe languages come at the cost of performance: &lt;code&gt;malloc&lt;/code&gt; takes amortized constant time, but having memory checking and garbage collection can add additional overhead. These days, this is fairly insignificant and languages like Go, Rust have comparable performance.&lt;/p&gt;</description></item><item><title>Cryptography</title><link>https://notes.bencuan.me/cs161/Cryptography/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Cryptography/</guid><description>&lt;h2 id="what-is-cryptography"&gt;
 What is Cryptography?
 &lt;a class="anchor" href="#what-is-cryptography"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Cryptography is the study of secure communication over insecure channels, and often involves providing guarantees on the security of resources in the presence of an attacker. (For the number-theory foundations behind RSA and Diffie–Hellman — modular exponentiation, Fermat&amp;rsquo;s little theorem, multiplicative inverses — see [[cs70/discrete-math/modular-arithmetic]] and [[cs70/discrete-math/rsa-cryptography]].)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Cast&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Alice: sender&lt;/li&gt;
&lt;li&gt;Bob: recipient&lt;/li&gt;
&lt;li&gt;Eve: eavesdropper (cannot modify messages)&lt;/li&gt;
&lt;li&gt;Mallory: malicious attacker (can modify messages)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="three-goals-of-cryptography"&gt;
 &lt;strong&gt;Three Goals of Cryptography&lt;/strong&gt;
 &lt;a class="anchor" href="#three-goals-of-cryptography"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Confidentiality:&lt;/strong&gt; adversary cannot read messages
&lt;ol&gt;
&lt;li&gt;Lock and unlock messages on local machine of sender and recipient, so all messages in the communication channel are encrypted&lt;/li&gt;
&lt;li&gt;If encrypted, the ciphertext should not give attackers any additional information about the plaintext, other than what the attacker already knew beforehand.&lt;/li&gt;
&lt;li&gt;Ideally, it should be impossible to distinguish between two encrypted messages of the same length.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IND-CPA:&lt;/strong&gt; Indistinguishability under chosen plaintext attack: If Eve sends a pair of plaintexts $M_0$ and $M_1$ to Alice to encrypt, Eve must only be able to guess which encrypted message was which plaintext with probability $0.5$. (This must also apply for two identical strings!)
&lt;ol&gt;
&lt;li&gt;Edge cases: does not hide length, and only applies to polynomial time attacks (since exponential time brute forcing would be unfeasible). For example, probability $0.5 + 0.5^{128}$ is acceptable.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrity:&lt;/strong&gt; adversary cannot change messages without being detected
&lt;ol&gt;
&lt;li&gt;Attach a tag to messages. If the tag was changed, then the message was modified&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Authenticity:&lt;/strong&gt; can prove that a message came from its intended sender&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="kerckhoff"&gt;
 &lt;strong&gt;Kerckhoff&amp;rsquo;s Principle&lt;/strong&gt;
 &lt;a class="anchor" href="#kerckhoff"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;Security through obscurity: always assume attackers know the system. i.e. cryptosystems should be secure even if attackers know exactly how it works:&lt;/p&gt;</description></item><item><title>Web Security</title><link>https://notes.bencuan.me/cs161/Web-Security/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Web-Security/</guid><description>&lt;h1 id="what-is-the-web"&gt;
 What is the Web?
 &lt;a class="anchor" href="#what-is-the-web"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;p&gt;The Web is a platform for deploying application and sharing information &lt;strong&gt;portably&lt;/strong&gt; and &lt;strong&gt;securely.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="http"&gt;
 HTTP
 &lt;a class="anchor" href="#http"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;HyperText Transfer Protocol is a common data communication protocol on the web.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Clients make a resource request to a specific URL (see below for an example).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;![[/cs161/img/Web-Security/Untitled.png]]&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An HTTP request has a &lt;strong&gt;method&lt;/strong&gt; (GET, POST..), a path, and some headers (like version, date, content type&amp;hellip;)&lt;/li&gt;
&lt;li&gt;A HTTP response primarily has a status code (200 OK) with some more headers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="htmlcssjs"&gt;
 HTML/CSS/JS
 &lt;a class="anchor" href="#htmlcssjs"&gt;#&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;HTML specifies the layout, CSS specifies the style, and JS specifies the behavior. All three work together to modify the DOM (Document Object Model) of a webpage, which is then sent to a painter that outputs an image on the screen.&lt;/p&gt;</description></item><item><title>Networking</title><link>https://notes.bencuan.me/cs161/Networking/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Networking/</guid><description>&lt;h1 id="what-is-the-internet"&gt;
 What is the Internet?
 &lt;a class="anchor" href="#what-is-the-internet"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;p&gt;A &lt;strong&gt;network&lt;/strong&gt; is a set of connected machines that can communicate with each other through some agreed &lt;strong&gt;protocol.&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;protocol&lt;/strong&gt; is a shared agreement on syntax (structure, format, order of messages) and semantics (what actions to take when a message is received)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Internet is a &lt;strong&gt;global network of computers&lt;/strong&gt; (also known as a network of networks). (For a full networking course&amp;rsquo;s treatment of every layer below — including how TCP, BGP, and DNS actually work — see [[cs168/internet organization and layers]]. This page focuses on the &lt;em&gt;security&lt;/em&gt; properties and attacks at each layer.)&lt;/p&gt;</description></item><item><title/><link>https://notes.bencuan.me/cs161/CS161-Index/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/CS161-Index/</guid><description>&lt;p&gt;[[Security Principles]] - How to evaluate threat models
[[x86]] - x86 assembly basics and calling convention.
[[Memory Safety Vulnerabilities]] - Buffer overflows and related vulnerabilities.
[[Heap Vulnerabilities]] - Vtables and heap overflow.
[[Memory Safety Mitigations]] - Canaries, NX pages, ASLR, PAC.
[[Cryptography]] - Hashing, authentication, encryption, MAC, signatures.
[[Web Security]] - Same-origin policy, CSRF, SQL injection, cookies, XSS, UI Attacks.
[[Networking]] - The OSI layer model and all related technologies.&lt;/p&gt;</description></item><item><title>Extra Topics</title><link>https://notes.bencuan.me/cs161/Extra-Topics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://notes.bencuan.me/cs161/Extra-Topics/</guid><description>&lt;h1 id="malware"&gt;
 Malware
 &lt;a class="anchor" href="#malware"&gt;#&lt;/a&gt;
&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Malware&lt;/strong&gt; is a catch-all term for malicious software, and can also be known as malcode or potentially unwanted application (PUA). Malware includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;File deleting software&lt;/li&gt;
&lt;li&gt;Spam email senders&lt;/li&gt;
&lt;li&gt;DoS attack platforms&lt;/li&gt;
&lt;li&gt;Rootkits that hide in the OS&lt;/li&gt;
&lt;li&gt;And so on&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="self-replicating-code"&gt;
 Self-replicating code
 &lt;a class="anchor" href="#self-replicating-code"&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;One thing that malware needs to do in order to become effective is spread quickly. One method to do so automatically is to write &lt;strong&gt;self-replicating code,&lt;/strong&gt; which outputs a copy of itself and propagates onwards. Self-replicating code is &lt;strong&gt;extremely dangerous&lt;/strong&gt; and should never be written intentionally.&lt;/p&gt;</description></item></channel></rss>