Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/data/meterpreter/python/met_aes.py
Views: 11779
import copy1import struct2import sys345def chunks(lst, n):6for i in range(0, len(lst), n):7yield lst[i:i + n]8910def _cw(word):11return (word[0] << 24) | (word[1] << 16) | (word[2] << 8) | word[3]121314def _s2b(text):15return list(ord(c)for c in text)161718def _b2s(binary):19return "".join(chr(b)for b in binary)202122if sys.version_info[0] >= 3:23xrange = range2425def _s2b(text):26if isinstance(text, bytes):27return text28return [ord(c)for c in text]2930def _b2s(binary):31return bytes(binary)32else:33def bytes(s, e): return s343536def _gmul(a, b):37r = 038while b:39if b & 1:40r ^= a41a <<= 142if a > 255:43a ^= 0x11B44b >>= 145return r464748def _mix(n, vec):49return sum(_gmul(n, v) << (24 - 8 * shift) for shift, v in enumerate(vec))505152def _ror32(n):53return (n & 255) << 24 | n >> 8545556def _rcon():57return [_gmul(1, 1 << n) for n in range(30)]585960def _Si(S):61return [S.index(n) for n in range(len(S))]626364def _mixl(S, vec):65return [_mix(s, vec) for s in S]666768def _rorl(T):69return [_ror32(t) for t in T]707172empty = struct.pack('')737475class AESCBC(object):76nrs = {16: 10, 24: 12, 32: 14}77rcon = _rcon()78S = [7999, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171,80118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156,81164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241,82113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226,83235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179,8441, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57,8574, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127,8680, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218,8733, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167,88126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238,89184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211,90172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108,9186, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198,92232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246,9314, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217,94142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191,95230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 2296]97Si = _Si(S)98T1 = _mixl(S, (2, 1, 1, 3))99T2 = _rorl(T1)100T3 = _rorl(T2)101T4 = _rorl(T3)102T5 = _mixl(Si, (14, 9, 13, 11))103T6 = _rorl(T5)104T7 = _rorl(T6)105T8 = _rorl(T7)106U1 = _mixl(range(256), (14, 9, 13, 11))107U2 = _rorl(U1)108U3 = _rorl(U2)109U4 = _rorl(U3)110111def __init__(self, key):112if len(key)not in (16, 24, 32):113raise ValueError('Invalid key size')114rds = self.nrs[len(key)]115self._Ke = [[0] * 4 for i in xrange(rds + 1)]116self._Kd = [[0] * 4 for i in xrange(rds + 1)]117rnd_kc = (rds + 1) * 4118KC = len(key) // 4119tk = [struct.unpack('>i', key[i:i + 4])[0]120for i in xrange(0, len(key), 4)]121rconpointer = 0122t = KC123for i in xrange(0, KC):124self._Ke[i // 4][i % 4] = tk[i]125self._Kd[rds - (i // 4)][i % 4] = tk[i]126while t < rnd_kc:127tt = tk[KC - 1]128tk[0] ^= ((self.S[(tt >> 16) & 255] << 24) ^ (self.S[(tt >> 8) & 255] << 16) ^ (129self.S[tt & 255] << 8) ^ self.S[(tt >> 24) & 255] ^ (self.rcon[rconpointer] << 24))130rconpointer += 1131if KC != 8:132for i in xrange(1, KC):133tk[i] ^= tk[i - 1]134else:135for i in xrange(1, KC // 2):136tk[i] ^= tk[i - 1]137tt = tk[KC // 2 - 1]138tk[KC // 2] ^= (self.S[tt & 255] ^ (self.S[(tt >> 8) & 255] << 8) ^139(self.S[(tt >> 16) & 255] << 16) ^ (self.S[(tt >> 24) & 255] << 24))140for i in xrange(KC // 2 + 1, KC):141tk[i] ^= tk[i - 1]142j = 0143while j < KC and t < rnd_kc:144self._Ke[t // 4][t % 4] = tk[j]145self._Kd[rds - (t // 4)][t % 4] = tk[j]146j += 1147t += 1148for r in xrange(1, rds):149for j in xrange(0, 4):150tt = self._Kd[r][j]151self._Kd[r][j] = (self.U1[(tt >> 24) & 255] ^ self.U2[(152tt >> 16) & 255] ^ self.U3[(tt >> 8) & 255] ^ self.U4[tt & 255])153154def _encdec(self, data, K, s, S, L1, L2, L3, L4):155if len(data) != 16:156raise ValueError('wrong block length')157rds = len(K) - 1158(s1, s2, s3) = s159a = [0, 0, 0, 0]160t = [(_cw(data[4 * i:4 * i + 4]) ^ K[0][i])for i in xrange(0, 4)]161for r in xrange(1, rds):162for i in xrange(0, 4):163a[i] = L1[(t[i] >> 24) & 255]164a[i] ^= L2[(t[(i + s1) % 4] >> 16) & 255]165a[i] ^= L3[(t[(i + s2) % 4] >> 8) & 255]166a[i] ^= L4[t[(i + s3) % 4] & 255] ^ K[r][i]167t = copy.copy(a)168rst = []169for i in xrange(0, 4):170tt = K[rds][i]171rst.append((S[(t[i] >> 24) & 255] ^ (tt >> 24)) & 255)172rst.append((S[(t[(i + s1) % 4] >> 16) & 255] ^ (tt >> 16)) & 255)173rst.append((S[(t[(i + s2) % 4] >> 8) & 255] ^ (tt >> 8)) & 255)174rst.append((S[t[(i + s3) % 4] & 255] ^ tt) & 255)175return rst176177def enc_in(self, pt):178return self._encdec(179pt, self._Ke, [1801, 2, 3], self.S, self.T1, self.T2, self.T3, self.T4)181182def dec_in(self, ct):183return self._encdec(184ct, self._Kd, [1853, 2, 1], self.Si, self.T5, self.T6, self.T7, self.T8)186187def pad(self, pt):188c = 16 - (len(pt) % 16)189return pt + bytes(chr(c) * c, 'utf-8')190191def unpad(self, pt):192c = pt[-1]193if not isinstance(c, int):194c = ord(c)195return pt[:-c]196197def encrypt(self, iv, pt):198if len(iv) != 16:199raise ValueError('initialization vector must be 16 bytes')200else:201self._lcb = _s2b(iv)202pt = self.pad(pt)203return empty.join([self.enc_b(b)for b in chunks(pt, 16)])204205def enc_b(self, pt):206if len(pt) != 16:207raise ValueError('plaintext block must be 16 bytes')208pt = _s2b(pt)209pcb = [(p ^ l)for (p, l) in zip(pt, self._lcb)]210self._lcb = self.enc_in(pcb)211return _b2s(self._lcb)212213def decrypt(self, iv, ct):214if len(iv) != 16:215raise ValueError('initialization vector must be 16 bytes')216else:217self._lcb = _s2b(iv)218if len(ct) % 16 != 0:219raise ValueError('ciphertext must be a multiple of 16')220return self.unpad(empty.join([self.dec_b(b)for b in chunks(ct, 16)]))221222def dec_b(self, ct):223if len(ct) != 16:224raise ValueError('ciphertext block must be 16 bytes')225cb = _s2b(ct)226pt = [(p ^ l)for (p, l) in zip(self.dec_in(cb), self._lcb)]227self._lcb = cb228return _b2s(pt)229230231