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_rsa.py
Views: 11780
import sys1import math2import random3import binascii as ba4import os5from struct import unpack as u6from struct import pack7is2 = sys.version_info[0] < 38910def bt(b):11if is2:12return b13return ord(b)141516def b2i(b):17return int(ba.b2a_hex(b), 16)181920def i2b(i):21h = '%x' % i22if len(h) % 2 == 1:23h = '0' + h24if not is2:25h = h.encode('utf-8')26return ba.a2b_hex(h)272829def rs(a, o):30if a[o] == bt(pack('B', 0x81)):31return (u('B', a[o + 1])[0], 2 + o)32elif a[o] == bt(pack('B', 0x82)):33return (u('>H', a[o + 1:o + 3])[0], 3 + o)343536def ri(b, o):37i, o = rs(b, o)38return (b[o:o + i], o + i)394041def b2me(b):42if b[0] != bt(pack('B', 0x30)):43return (None, None)44_, o = rs(b, 1)45if b[o] != bt(pack('B', 2)):46return (None, None)47(m, o) = ri(b, o + 1)48if b[o] != bt(pack('B', 2)):49return (None, None)50e = b[o + 2:]51return (b2i(m), b2i(e))525354def der2me(d):55if d[0] != bt(pack('B', 0x30)):56return (None, None)57_, o = rs(d, 1)58while o < len(d):59if d[o] == bt(pack('B', 0x30)):60o += u('B', d[o + 1:o + 2])[0]61elif d[o] == bt(pack('B', 0x05)):62o += 263elif d[o] == bt(pack('B', 0x03)):64_, o = rs(d, o + 1)65return b2me(d[o + 1:])66else:67return (None, None)686970def rsa_enc(der, msg):71m, e = der2me(der)72h = pack('BB', 0, 2)73d = pack('B', 0)74l = 256 - len(h) - len(msg) - len(d)75p = os.urandom(512).replace(pack('B', 0), pack(''))76return i2b(pow(b2i(h + p[:l] + d + msg), e, m))777879