bambi6-service-postit

Simple Note-Taking A/D Service for BambiCTF6 in 2021
git clone https://git.sinitax.com/sinitax/bambi6-service-postit
Log | Files | Refs | README | LICENSE | sfeed.txt

util.py (2906B)


      1#!/usr/bin/env python3
      2
      3import math
      4import secrets
      5import string
      6import time
      7from base64 import b64encode
      8from logging import LoggerAdapter
      9from os import path
     10from typing import Any
     11
     12leetconv = {
     13    "O": "0",
     14    "l": "1",
     15    "I": "1",
     16    "Z": "2",
     17    "E": "3",
     18    "A": "4",
     19    "S": "5",
     20    "G": "6",
     21    "T": "7",
     22}
     23
     24srcdir = path.dirname(path.abspath(__file__))
     25wordlist = open(f"{srcdir}/media/wordlist").read().replace(" ", "").split("\n")
     26names = [line for line in wordlist if line != ""]
     27rickroll = open(f"{srcdir}/media/rickroll.b64").read().replace("\n", "")
     28messages = [
     29    "Remember: invite Paul to lan party",
     30    "Shopping list: tomatoes and potatoes",
     31    "This is not the flag hehe",
     32    "🤓🤓🤓 Try Harder 🤓🤓🤓",
     33    "Crypto makes me go 🥴🤢🤮",
     34    "RSA, more like (R)eal (S)Bad (A)Crypto",
     35    "🤡 The flag is in another castle! 🤡",
     36    "🧠 solving crypto challenges with a calculator 🧠",
     37]
     38gplmsg = "You should have received a copy of the GNU \
     39General Public License along with this file; if not, \
     40write to the Free Software Foundation, Inc., 51 Franklin St, \
     41Fifth Floor, Boston, MA 02110-1301 USA"
     42
     43
     44def randint(low: int, high: int) -> int:
     45    return low + secrets.randbelow(high - low + 1)
     46
     47
     48def randbool() -> bool:
     49    return randint(0, 1) == 1
     50
     51
     52def randstr(n: int) -> str:
     53    alphabet = string.ascii_letters + string.digits
     54    chars = [secrets.choice(alphabet) for i in range(n)]
     55    return "".join(chars)
     56
     57
     58def bytes2int(s: bytes) -> int:
     59    return int.from_bytes(s, byteorder="big")
     60
     61
     62def int2bytes(i: int) -> bytes:
     63    assert i >= 0
     64    blen = 1 if i == 0 else math.ceil(math.log2(i) / 8)
     65    return int.to_bytes(i, byteorder="big", length=blen)
     66
     67
     68def spongebobcase(name: str) -> str:
     69    return "".join([(c.upper() if randbool() else c) for c in name])
     70
     71
     72def leetify(name: str) -> str:
     73    if randbool():
     74        name = spongebobcase(name)
     75    return "".join([(leetconv[c] if c in leetconv else c) for c in name])
     76
     77
     78def gen_username() -> bytes:
     79    msg = ""
     80    while len(msg) < randint(10, 30):
     81        part = secrets.choice(names)
     82        if randbool():
     83            part = leetify(part)
     84        msg += part
     85    username = msg + randstr(30)
     86    return username.encode()
     87
     88
     89def gen_noise() -> bytes:
     90    selection = randint(0, len(messages) + 2)
     91    if selection == 0:
     92        msg = randstr(randint(30, 60))
     93    elif selection == 1:
     94        msg = rickroll
     95    elif selection == 2:
     96        msg = gplmsg
     97    else:
     98        msg = secrets.choice(messages)
     99        if randbool():
    100            msg = b64encode(msg.encode()).decode()
    101    return msg.encode()
    102
    103
    104async def timed(promise: Any, logger: LoggerAdapter, ctx: str) -> Any:
    105    logger.debug("START: {}".format(ctx))
    106    start = time.time()
    107    result = await promise
    108    end = time.time()
    109    logger.debug("DONE:  {} (took {:.3f} seconds)".format(ctx, end - start))
    110    return result