title: How can I properly mimic this encryption method to produce the proper value for the encryptedPwd field?
body: <h1><em>Background</em></h1>
  <p>I'm needing to pull reports from Amazon's Seller Central portal for multiple clients of ours on a weekly basis via a python script. I try to avoid webdrivers due to their inconsistent, error-prone nature across different OSes (from experience). Therefore, I figured it would be a fun little project (and maybe, ultimately, exercise in futility) to try reverse-engineering the login process for sellercentral.amazon.com. The process isn't difficult, save for two fields in the initial login form: <code>password</code> and <code>metadata1</code>. The <code>password</code> field is discussed in detail below. The <code>metadata1</code> field seems to employ a similar technique, while introducing the additional hurdle of changing every few seconds. When i monitored the <code>metadata1</code> value it appeared to be generated using a large json object of the various browser metrics. But, one thing at a time, so I'll focus on the password encryption in this post and worry about <code>metadata1</code> in a future post.</p>
  <h1><em>Getting to the point</em> </h1>
  <p>Upon form submission the value in the <code>password</code> field is encrypted using techniques discussed further down in this post. The resulting encrypted value then replaces the value in the the password field and renames the field <code>encryptedPwd</code></p>
  <h1><em>My Findings</em></h1>
  <p>So far, I've been reviewing the javascript files and stepping through the code sequences and have learned the following:</p>
  <ul>
  <li>They're utilizing a proprietary <a href="https://github.com/Anirudh-R-2506/phisherman/blob/fdc44f2ebcba046e063a2ec0be531c091a5db1ae/sites/amazon/signin/Amazon%20Sign-In-pwd_files/81JZFvi7%2BTL.js" rel="noreferrer">SiegeCrypto.js</a> script as the main cryptographic library (Siege being Amazon's Secure Ingress and Egress Team) &amp; <a href="https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto" rel="noreferrer">SubtleCrypto</a> as a part of the process for generating the encryptedPwd
    - SiegeCrypto is first found in <a href="https://github.com/Anirudh-R-2506/phisherman/blob/fdc44f2ebcba046e063a2ec0be531c091a5db1ae/sites/amazon/signin/Amazon%20Sign-In-pwd_files/AuthenticationPortalSigninNA.js" rel="noreferrer">AuthenticationPortalSigninNA.js</a>, declaring the basic initial encryption definition:</li>
  </ul>
  <pre class="lang-js prettyprint-override"><code>SiegeCrypto.addProfile(&quot;AuthenticationPortalSigninNA&quot;, {
    &quot;password&quot;: {dataType: &quot;AuthPortalSigninPasswordNA&quot;, requiresTail: false},
  });
  ...
  SiegeCrypto.addDataType({
      &quot;dataTypeId&quot;: &quot;AuthPortalSigninPasswordNA&quot;,
      &quot;jwkPublicKey&quot;: {&quot;kty&quot;:&quot;RSA&quot;,&quot;e&quot;:&quot;AQAB&quot;,&quot;n&quot;:&quot;gXXZV1VqZ6k_uQtyJNJy5q-qvKdqrXJNgKUO1aYc1UPBVqlhCP0GPxf-0GSo-LEtArgcbF8-j6_vSLSqztYxxF8og--rB8zAyZ8DXZaugX-UiJDQnoJL_HtXKuwIm9U7oEPoeD6H4ZDcfbsPj77xVn7UA2-a90N4aZqMC8EIfXIy1tqSbSPnxPOaiEmy8xGtG-L3RdCyc7TL0Swd_f0_DjRT6ip91IBlCmquoa-xJgZ9e44PVH4AwdyssiV4ZLEZ5yFcE0zcRb_62kx_TQptidbJ4nHocFVjmUW9YsrAWeKrBmOGZEjO4vbATYs1Yf4vgcH7Ix61EPR5sbDP4SlBWQ&quot;},
      &quot;providerId&quot;: &quot;si:md5&quot;,
      &quot;keyId&quot;: &quot;56d14edce8e2cb6c6842c59ddaee426e&quot;
  });
  </code></pre>
  <p>Stepping through the code I was able to find more specifics of the algorithm</p>
  <pre><code>*profile* (used by SiegeCrypto)
  - password: {dataType: &quot;AuthPortalSigninPasswordNA&quot;, requiresTail: false}
  *publicKeyProvider* (added as a DataType to SiegeCrypto)
  - keyId: 56d14edce8e2cb6c6842c59ddaee426e
  - providerId: si:md5
  *wrapKey*
  - wrappingAlgorithm
    - name: RSA-OAEP
    - hash: SHA-256
    - modulusLength: 2048
    - publicExponent: [1, 0, 1]
  *Additional Fields*
  name: aes_128_gcm_iv12_tag16
  encryption: AES-GCM
  ivLength: 12
  keyLength: 128
  tagLength: 128
  </code></pre>
  <p>Later in the process I was able to find the following, which I'm assuming is the parameters amidst being processed according to the encryptions specifications above, but i have no idea how to get to that point</p>
  <pre><code>cipherMessage: Uint8Array(413) [1, 128, 0, 20, 124, 132, 165, 153, 149, 96, 94, 4, 210, ...]
  messageHeader:
  - algorithmId: 20
  - contentType: 2
  - encryptedDataKeys: [{&quot;keyInfo&quot;: &quot;56d14edce8e2cb6c6842c59ddaee426e&quot;}]
  - encryptionContext: {}
  - frameLength: 12
  - headerIvLength: 12
  - messageId: Uint8Array(16) [124, 132, 165, 153, 149, 96, ...]
  - type: 128
  - version: 1
  </code></pre>
  <p>Looking over Amazon's AWS Encryption SDK (<a href="https://github.com/aws/aws-encryption-sdk-python/blob/master/src/aws_encryption_sdk/identifiers.py" rel="noreferrer">Python Repo</a>) I've found the following three pieces that seem to be what I need. However, I'm not sure where to go from here.</p>
  <pre class="lang-py prettyprint-override"><code># algorithm, mode, data_key_length, iv_length, auth_length, auth_key_length=0
  EncryptionSuite.AES_128_GCM_IV12_TAG16 = (algorithms.AES, modes.GCM, 16, 12, 16)
  
  # algorithm_id,  encryption, message_format_version
  AlgorithmSuite.AES_128_GCM_IV12_TAG16 = (0x0014, EncryptionSuite.AES_128_GCM_IV12_TAG16, 0x01)
  
  # encryption_type, algorithm, padding_type, padding_algorithm, padding_mgf
  WrappingAlgorithm.RSA_OAEP_SHA256_MGF1 = (EncryptionType.ASYMMETRIC, rsa, padding.OAEP, hashes.SHA256, padding.MGF1)
  </code></pre>
  <h1>The Question</h1>
  <p>Can someone provide a short python snippet utilizing the above encryption techniques along with an explanation, so I can see how the <code>encryptedPwd</code> field value is generated? Here's a dummy password to demonstrate with: Blamazon123</p>
  <p>Two example values of the above dummy password as <code>encryptedPwd</code> (I've added spaces where I saw consistences):</p>
  <pre><code>AYAAF  P/a2u8yLSNjLWzPRIi0Bac  AAAABAAZzaTptZDUAIDU2ZDE0ZWRjZThlMmNiNmM2ODQyYzU5ZGRhZWU0MjZlAQ  Brthm+db6k/Oo832X/5U+JtXcBrVnCetjOnvcypG5ZZ6xZr0rXDDMctQevThwGjGYqOOQTy6tFALgMHnjWC2bcBBtyKMhUflpCjGTRodjE7btdqrgExEr07k1ErejaQ1vAW8hQSedfsQR3gyWxJcKKlQ91B4CYO5UMMJzevQyln0SASh5MLW6xOHMnjwdHI8aKFw2ErcvIFg5OpqCDSIyPjifvxkSTue7gJ3fB0ACda04EA5wxmkRteCF753kVGYNBD0h9eOHCPcCm/Y7bWoJAelvqu/U/LxAPkl216deDko4oxjVqLeRy/IExbx6cdEDT7zu0U7HROhvstu8TZE1f  AgAAAAAMAAAADAAAAAAAAAAAAAAAA  O0Gt/txLoiiXlGQcb5dyFn/////  AAAAAQAAAAAAAAAAAAAAAQAAAAv  LEJ4zlnbivrzliBrcFGIsPBU3srfmTu91dw4=
  AYAAF  L1E3ydr57mIKpAQtOrAPsE  AAAABAAZzaTptZDUAIDU2ZDE0ZWRjZThlMmNiNmM2ODQyYzU5ZGRhZWU0MjZlAQ  AUJX+8tRKZESh1o09BLe6Qj13iuyP5Kb2IC/ipA1mRlWIQtIYApU8792+f5U2x8wv7rTVHcKM8wnFXP2I78PCbo4kXwV5Q6JE99bV4BP+5YnzB1YI6XUgrZ2ubm1wcSV3W1K3OhMogcXIbWjeEjKj2WmpVgSgCXKS6+Z6GxMnE+hArZlNIATYojL7IlLPR5kiGzN4pq86gLzGbfcG2at1MNQ5DdrJtktixLJPU1oFwCtT4AFfy6kiGfoepN+VE0AK0ysMyX3FY7QaI9qLtuA20zQX52NbLzG/qSENYohHzgvOOVzCIr4uwyJ3uXSA0kKXEJ4IbWmQ+k30cotoWRSJW  AgAAAAAMAAAADAAAAAAAAAAAAAAAA  NUNRLibdfG4P1ac0dL8Ka//////  AAAAAQAAAAAAAAAAAAAAAQAAAAv  4vyjW2MLIuuBm8D1c41v5ZwEQFk8k/p4GOss=
  </code></pre>
  <h1>Adding Additional Source Code with Analysis</h1>
  <p>The javascript file largely responsible for generating the <code>metadata1</code> field can be found <a href="https://gist.github.com/caffeinatedMike/b49bfe4cb179f90626dca3c726dcfd95" rel="noreferrer">here</a>. I've decrypted the functions that I saw were part of the <code>metadata1</code> generation process. They start on the following lines:</p>
  <ul>
  <li>1827 (relates to <code>encryptedPwd</code>) adds the encryption eventlistener to the form submit action</li>
  <li>332 the core generating function that dispatches the encrypting steps</li>
  <li>789 &amp; 810 create a crcTable that's used in generating</li>
  <li>1839 calculates a checksum (used in the <code>metadata1</code> process)</li>
  <li>2540 is where the <code>metadata1</code> values are returned (from line 332)</li>
  <li>2672 and 2704 are where I noticed the email being converted a hex value, that is prefixed to the <code>metadata1</code> contents before it's encrypted</li>
  </ul>