Skip to content

Instantly share code, notes, and snippets.

@gtfierro
Last active January 4, 2016 13:19
Show Gist options
  • Save gtfierro/8627218 to your computer and use it in GitHub Desktop.
Save gtfierro/8627218 to your computer and use it in GitHub Desktop.
stripe-ctf level 3
#!/bin/sh
set -e
# Add or modify any build steps you need here
cd "$(dirname "$0")"
pip install --user pandas flask requests grequests
#!/bin/bash
python slave.py 9091 &
slave1=$!
python slave.py 9092 &
slave2=$!
python slave.py 9093 &
slave3=$!
python master.py
kill $slave1 $slave2 $slave3
from flask import Flask
from flask import jsonify, request
from collections import defaultdict
import itertools
import requests
import grequests
import json
import pandas as pd
import os
import random
servers = ['http://localhost:9091','http://localhost:9092','http://localhost:9093']
servercounter = 0
app = Flask(__name__)
fileindex = []
lines = [] # this will contain tuples of (filename:linenum, linecontents)
filenames = set()
boolstring = lambda x: "true" if x else "false"
indexcomplete = False
def indexfile(filename):
contents = open(filename,'rb').read()
for linenum, line in enumerate(contents.split('\n')):
lines.append(('{0}:{1}'.format(filename[len(initialpath)+1:],linenum+1), line))
def indexpath(pathToIndex):
if os.path.isdir(pathToIndex):
pathcontents = os.listdir(pathToIndex)
for content in pathcontents:
indexpath(os.path.join(pathToIndex, content))
else:
if pathToIndex not in filenames:
filenames.add(pathToIndex)
global initialpath
global servercounter
servercounter += 1
x= servers[servercounter%3]+'/index?path={0}&initial={1}'.format(pathToIndex, initialpath)
requests.get(x)
indexfile(pathToIndex)
@app.route('/healthcheck')
def healthcheck():
return jsonify(success=bool(True))
@app.route('/index')
def doindexing():
global initialpath
initialpath = request.args.get('path')
indexpath(request.args.get('path'))
global fileindex
fileindex = pd.DataFrame.from_records(lines)
# once we're done indexing, set the global flag to true for /isIndexed (below)
global indexcomplete
indexcomplete = True
return '' # returns 200
@app.route('/isIndexed')
def isIndexed():
return jsonify(success=boolstring(indexcomplete))
@app.route('/')
def root():
query = request.args.get('q')
if not query:
return jsonify(success=False, results=[])
results = []
global fileindex
slaveresults = []
rs = (grequests.get(server+'/?q={0}'.format(query)) for server in servers)
res = grequests.map(rs)
for slave in res:
slaveresults.extend(json.loads(slave.content)['results'])
return jsonify(success=boolstring(len(results)), results=slaveresults)
if __name__ == "__main__":
app.run(debug=True,port=9090)
from flask import Flask
from flask import jsonify, request
from collections import defaultdict
import pandas as pd
import os
import sys
app = Flask(__name__)
fileindex = []
lines = [] # this will contain tuples of (filename:linenum, linecontents)
filenames = set()
boolstring = lambda x: "true" if x else "false"
indexcomplete = False
def indexfile(filename):
contents = open(filename,'rb').read()
for linenum, line in enumerate(contents.split('\n')):
lines.append(('{0}:{1}'.format(filename[len(initialpath)+1:],linenum+1), line))
def indexpath(pathToIndex):
if os.path.isdir(pathToIndex):
pathcontents = os.listdir(pathToIndex)
for content in pathcontents:
indexpath(os.path.join(pathToIndex, content))
else:
if pathToIndex not in filenames:
filenames.add(pathToIndex)
indexfile(pathToIndex)
@app.route('/healthcheck')
def healthcheck():
return jsonify(success=bool(True))
@app.route('/index')
def doindexing():
global initialpath
initialpath = request.args.get('initial')
path = request.args.get('path')
indexpath(path)
global fileindex
fileindex = pd.DataFrame.from_records(lines)
# once we're done indexing, set the global flag to true for /isIndexed (below)
global indexcomplete
indexcomplete = True
return '' # returns 200
@app.route('/isIndexed')
def isIndexed():
return jsonify(success=boolstring(indexcomplete))
@app.route('/')
def root():
query = request.args.get('q')
if not query:
return jsonify(success=False, results=[])
results = []
global fileindex
res = fileindex[fileindex[1].str.contains(query)]
results = [x[1][0] for x in res.iterrows()]
return jsonify(success=boolstring(len(results)), results=results)
if __name__ == "__main__":
port = sys.argv[1]
app.run(debug=True,port=int(port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment