def main(args): """ Do a cut-and-paste attack. Effectively what this will do is align and pad the input so that the email is on a block all on its own, specifying "admin" as an email. Then, it will take the ecb output block and put that where the role block should go in the encrypted version. When this is decrypted, "admin" (padded to an entire block) will show up as the role. """ len_before_email = len('email=') attack_email = (16 - len_before_email) * '\0' + pad('admin', 16, '\0') len_before_role = len_before_email + len(attack_email) + len('&uid=10&role=') extra_pad_length = 16 - len_before_role % 16 attack_email += '\0' * extra_pad_length attack_enc = list(encrypted_profile_for(attack_email)) admin_block = attack_enc[16:32] email = '*****@*****.**' len_before_role = len_before_email + len(email) + len('&uid=10&role=') extra_pad_length = 16 - len_before_role % 16 actual_enc = list(encrypted_profile_for(email + '\0' * extra_pad_length)) actual_enc[len_before_role+extra_pad_length:len_before_role+extra_pad_length+16] = admin_block print( parse_cookie( decrypted_profile_for( ''.join(actual_enc) ) ) )
def main(args): if len(args) < 1: print("Please include a filename to use!") return random.seed(time.time()) with open(args[0]) as file: enc, mode = cbc_or_ecb(pad(file.read(), 16) * 2) print(detect_mode(enc), mode)
def testPad(self): cases = { ("YELLOW SUBMARINE", 16, '\x04'): "YELLOW SUBMARINE", ("YELLOW SUBMARINE", 20, '\x04'): "YELLOW SUBMARINE\x04\x04\x04\x04", ("YELLOW SUBMARINE", 20, 'a'): "YELLOW SUBMARINEaaaa", ("YELLOW SUBMARINE", 5, 'a'): "YELLOW SUBMARINEaaaa", } for i, o in cases.iteritems(): self.assertEqual(padding.pad(*i), o)
def cbc_or_ecb(plaintext): use_cbc = random.randint(0, 2) == 1 aes = AES.new( generate_key(), AES.MODE_CBC if use_cbc else AES.MODE_ECB, Random.get_random_bytes(16)) text = pad( Random.get_random_bytes(6 + random.randint(0, 5)) + plaintext + Random.get_random_bytes(6 + random.randint(0, 5)), 16) return (aes.encrypt(text), 'cbc' if use_cbc else 'ecb')
def main(args): # create the cipher using the key and the ECB mode # ECB mode uses the same key to encrypt every block of plain data cipher = AES.new(key, AES.MODE_CBC, iv) # Read the plain data from a file and pad it so it is a # multiple of 'block_size' in length with the padding at the end # to fill the remaining space plain_text = read_plain(args.input) padded_plain_text = pad(plain_text, block_size) # encrypt the padded text with the prev. created chipher and write it to a file. cipher_text = iv + cipher.encrypt(padded_plain_text) write_encrypted(cipher_text, args.output)
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # =================================================================== hex_iv = 'aabbccddeeff00998877665544332211' iv = int(hex_iv, 16) # convert hex to 16-byte iv bytes_iv = bytes.fromhex(hex_iv) with open(os.path.join(sys.path[0], "plaintext"), "r") as f: plaintext = f.read().encode('utf-8') padded = pad(plaintext, 16) hex_cipher = '46beb3b832973495f79b860884245e431d73c2d3f7e3a7632dce894ed14ff62b' bytes_cipher = bytes.fromhex(hex_cipher) with open(os.path.join(sys.path[0], "words.txt"), "r") as key_file: for line in key_file: potential_key = line.strip() if (len(potential_key) > 16): continue if (len(potential_key) < 16): hash_number = 16 - len(potential_key) hashes = "#" * hash_number potential_key += hashes bytes_pkey = potential_key.encode('utf-8') cipher = AES.new(bytes_pkey, AES.MODE_CBC, bytes_iv)
def encrypt(self, text): """Encrypt text.""" text = pad(text.encode(), AES.block_size) iv = Random.new().read(AES.block_size) cipher = AES.new(self.key, self.mode, iv) return iv + cipher.encrypt(text)
def main(args): if len(args) < 2: print("Usage: ./challenge_1.py <text> <bytes>") return print(pad(args[0], int(args[1])))
def computeRHS_NOSGS_WEAVE_2D(main,grid,myFFT,utilities): main.Q2U() scale = np.sqrt( (3./2.)**3*np.sqrt(grid.N1*grid.N2*grid.N3) ) ureal = np.zeros( (int(3./2.*grid.N1),int(3./2.*grid.N2),int(3./2.*grid.N3)) ) vreal = np.zeros( (int(3./2.*grid.N1),int(3./2.*grid.N2),int(3./2.*grid.N3)) ) main.uhat = unpad(pad(main.uhat,1),1) main.vhat = unpad(pad(main.vhat,1),1) ureal = myFFT.ifftT_obj(pad(main.uhat,1))*scale vreal = myFFT.ifftT_obj(pad(main.vhat,1))*scale uuhat = unpad( myFFT.fft_obj(ureal*ureal),1) vvhat = unpad( myFFT.fft_obj(vreal*vreal),1) uvhat = unpad( myFFT.fft_obj(ureal*vreal),1) ### Setup pointers for weave k1 = grid.k1 k2 = grid.k2 ksqr_i = grid.ksqr_i ksqr = grid.ksqr Q = main.Q uhat = main.uhat vhat = main.vhat phat = np.zeros((grid.N1,grid.N2,grid.N3/2+1),dtype='complex') nu = main.nu N1 = grid.N1 N2 = grid.N2 N3 = grid.N3 Zj = 1j nthreads = myFFT.nthreads code=""" int i,j,k; omp_set_num_threads(nthreads); #pragma omp parallel for private(i,j,k) shared(uhat,vhat,phat,k1,k2,ksqr,ksqr_i,uuhat,vvhat,uvhat,Q,nu) for(i=0;i<N1;i++){ for (j=0;j<N2;j++){ for (k=0;k<N3/2+1;k++){ phat(i,j,k) = ksqr_i(i,j,k)*( -k1(i,j,k)*k1(i,j,k)*uuhat(i,j,k) - k2(i,j,k)*k2(i,j,k)*vvhat(i,j,k) - 2.*k1(i,j,k)*k2(i,j,k)*uvhat(i,j,k) ); Q(3*i,3*j,3*k) = -Zj*k1(i,j,k)*uuhat(i,j,k) - Zj*k2(i,j,k)*uvhat(i,j,k) - Zj*k1(i,j,k)*phat(i,j,k) - nu*ksqr(i,j,k)*uhat(i,j,k); Q(3*i+1,3*j+1,3*k+1) = -Zj*k1(i,j,k)*uvhat(i,j,k) - Zj*k2(i,j,k)*vvhat(i,j,k) - Zj*k2(i,j,k)*phat(i,j,k) - nu*ksqr(i,j,k)*vhat(i,j,k); Q(3*i+2,3*j+2,3*k+2) = 0; } } } """ weave.inline(code,['N1','N2','N3','uhat','vhat','phat','k1','k2','ksqr','ksqr_i','uuhat','vvhat','uvhat','Q','nu','Zj','nthreads'],\ type_converters=converters.blitz,compiler='gcc',extra_compile_args=\ ['-march=native -O3 -fopenmp'], support_code = \ r""" #include <iostream> #include <complex> #include <cmath> #include <omp.h> """, libraries=['gomp'] )
def encrypted_profile_for(email): return aes.encrypt(pad(profile_for(email), 16))
def ecnryption(self, text): data = bytes(text, 'utf-8') ciphertext = secrets[1].encrypt(pad(data, AES.block_size)) ct = b64encode(ciphertext).decode('utf-8') return str(ct)
def encrypt_ecb(plaintext, aes): text = plaintext + 'Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK'.decode( 'base64') return aes.encrypt(pad(text, 16))