Miscellaneous

hard rock casino

HackToday

09 August 2020

Points: 332

Problem :lock:

We got a netcat service running some kind of betting game

We’re also given the source code

View py source
#!/usr/bin/python
import random, signal, sys

class Unbuffered(object):
  def __init__(self, stream):
    self.stream = stream
  def write(self, data):
    self.stream.write(data)
    self.stream.flush()
  def writelines(self, datas):
    self.stream.writelines(datas)
    self.stream.flush()
  def __getattr__(self, attr):
    return getattr(self.stream, attr)

sys.stdout = Unbuffered(sys.stdout)

def handler(signum, frame):
  print '\nmaaf casino sudah mau tutup, silakan coba lagi...'
  exit()

class Player:
  def __init__(self, nama):
    self.nama = nama
    self.saldo = 1000
  def taruhan(self):
    try:
      bet = int(raw_input('\nhalo %s, ayo pasang taruhan: ' % (self.nama)).strip())
      if self.saldo >= bet:
        if bet > 0:
          if random.random() >= 0.44: # 56% winning chance?
            self.saldo += bet
            print 'kamu menang! saldo kamu %d' % (self.saldo)
          else:
            self.saldo -= bet
            print 'kamu kalah, saldo kamu %d' % (self.saldo)
        else:
          print '%s, dilarang bermain curang!!1!1' % (self.nama)
      else:
        print 'maaf %s, saldo kamu tidak cukup' % (self.nama)
      if self.saldo == 0:
        print '\nkamu bangkrut, bye %s' % (self.nama)
        exit()
      elif self.saldo >= 100000:
        print open('flag.txt').read().strip()
        exit()
    except:
      exit()

n = raw_input('nama kamu: ').strip()
p = Player(n)
signal.signal(signal.SIGALRM, handler)
signal.alarm(10)
while True:
  p.taruhan()

Download server.py

Solution :key:

From the source we know

After a bit of thinking we came up with a strategy

  1. we’ll bet all our money
    • our money will be doubled everytime we win
  2. if we lose, we lose all our money, but just re-enter and get another 1k
  3. repeat until we win

Now we need to check if this strategy is viable or not

By betting all our money everytime, everytime we win our money will be doubled, so in order to get from 1k to 100k by doubling, we need X where 2 ** X >= 100k, calculator action, we get 2 ** 7 = 128. So we need to win at least 7x in a row to get above 100k, with a winning chance of about 56%, we can count the chance of winning 7x in a row with 0.56 ** 7 which is 0.01727094849536001 or about 1%. So it is very possible to win with this strategy! All we need is to run it around 100x and hope we get lucky.

So we made this little python script so we can do the betting in under 10 secs time limit, and also keep a little attempt number to “track our luck”.

import socket
import time
import string
import sys

host = "chall.codepwnda.id"
port = 14021
percobaan = 1

def netcat(h, p):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((h,p))

    time.sleep(0.1)
    res = s.recv(10240)
    print(res)

    s.send("percobaan ke "+str(percobaan))
    s.send("\n")

    duit = 1000
    while 1:
        print("[ ] bet "+str(duit))
        s.send(str(duit))
        s.send("\n")
        time.sleep(0.3)
        res = s.recv(1024)
        print(res)
        if "bangkrut" in res:
            break
        elif "hacktoday" in res:
            exit()
        pos = res.find("saldo kamu")
        try:
            duit = [int(i) for i in res.split() if i.isdigit()][0]
        except:
            break

while 1:
    netcat(host,port)
    percobaan += 1

Download solve.py

Leave it running for a bit, and we get the flag at attempt no 171, pretty bad-ish luck but oh well!

Flag :checkered_flag:

hacktoday{when_this_house_is_rocking_dont_bother_knocking__come_on_in}