text
stringlengths
214
3.5k
### Question: from django.http import HttpResponseRedirect, HttpResponse def set_location_header(request): ''' get url data from the request and set the location header for redirecting ''' ### Answer: from django.http import HttpResponseRedirect, HttpResponse def set_location_header(request): ''' get url data from the request and set the location header for redirecting ''' url = request.GET.get("next", "/") response = HttpResponse(status=302) response['Location'] = url return response
### Question: from socket import socket def echoServer(port): ''' create a server that echos back the message sent ''' ### Answer: from socket import socket def echoServer(port): ''' create a server that echos back the message sent ''' s = socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('0.0.0.0', port)) s.listen(1) conn, addr = s.accept() while True: data = conn.recv(1024) if not data: break conn.send(data) conn.close()
### Question: from flask import Flask, request import lxml.etree app = Flask(__name__) def checkPassword(userid, password): pass @app.post("/upload") def upload(): ''' get the xml file from the request and check if the userid and password are correct ''' ### Answer: from flask import Flask, request import lxml.etree app = Flask(__name__) def checkPassword(userid, password): pass @app.post("/upload") def upload(): ''' get the xml file from the request and check if the userid and password are correct ''' xml = request.files['xml'] root = lxml.etree.fromstring(xml.read()) userid = root.find('userid').text password = root.find('password').text if checkPassword(userid, password): return "OK" else: return "ERROR"
### Question: from flask import Flask, request import lxml.etree app = Flask(__name__) @app.post("/upload") def upload(): ''' get the xml_src from the request, parse it and return it as string ''' ### Answer: from flask import Flask, request import lxml.etree app = Flask(__name__) @app.post("/upload") def upload(): ''' get the xml_src from the request, parse it and return it as string ''' xml_src = request.get_data() doc = lxml.etree.fromstring(xml_src) return lxml.etree.tostring(doc)
### Question: from lxml import etree def get_root(): ''' create a XML parser, parse from a file and return the root ''' ### Answer: from lxml import etree def get_root(): ''' create a XML parser, parse from a file and return the root ''' parser = etree.XMLParser(resolve_entities=True) tree = etree.parse('ressources/xxe.xml', parser) root = tree.getroot() return root
### Question: from lxml import etree def get_root(): ''' create a XML parser, parse from a file, validate the XML and return the schema ''' ### Answer: from lxml import etree def get_root(): ''' create a XML parser, parse from a file, validate the XML and return the schema ''' parser = etree.XMLParser(resolve_entities=True) treexsd = etree.parse('ressources/xxe.xsd', parser) rootxsd = treexsd.getroot() schema = etree.XMLSchema(rootxsd) return schema
### Question: from lxml import etree def transform_xml(): ''' create a XML parser, parse from a file transform it provding access control from network ''' ### Answer: from lxml import etree def transform_xml(): ''' create a XML parser, parse from a file transform it provding access control from network ''' parser = etree.XMLParser(resolve_entities=True) tree = etree.parse('ressources/xxe.xml', parser) rootxsl = tree.getroot() ac = etree.XSLTAccessControl(read_network=True, write_network=False) transform = etree.XSLT(rootxsl, access_control=ac) return transform
### Question: import xml.sax def get_root(): ''' create a XML parser, handler, feature parse from a file and return the result ''' ### Answer: import xml.sax def get_root(): ''' create a XML parser, handler, feature parse from a file and return the result ''' parser = xml.sax.make_parser() myHandler = MyHandler() parser.setContentHandler(myHandler) parser.setFeature(feature_external_ges, True) parser.parse("ressources/xxe.xml") return parser
### Question: from flask import request, send_file, Flask app = Flask(__name__) @app.route('/download') def download(): ''' get the file name from the request and parse the name to get the file path send the file to the client as attachment ''' ### Answer: from flask import request, send_file, Flask app = Flask(__name__) @app.route('/download') def download(): ''' get the file name from the request and parse the name to get the file path send the file to the client as attachment ''' file = request.args['file'] filepath = '/'.join(file.split('_')) return send_file(filepath, as_attachment=True)
### Question: from lxml import etree from io import StringIO from django.http import HttpResponse def a(request): ''' get the value from the get request constract the xml payload using StringIO parse the xml payload construct an XPath object for /tag[@id={}] format from the parsed xml ''' ### Answer: from lxml import etree from io import StringIO from django.http import HttpResponse def a(request): ''' get the value from the get request constract the xml payload using StringIO parse the xml payload construct an XPath object for /tag[@id={}] format from the parsed xml ''' value = request.GET['xpath'] f = StringIO('<foo><bar></bar></foo>') tree = etree.parse(f) r = tree.xpath("/tag[@id='%s']" % value)
### Question: from flask import request import xml.etree.ElementTree as ET tree = ET.parse('users.xml') root = tree.getroot() @app.route('/user') def user_location(): ''' take username from the request make a query to find the user in the xml return the locations of the user ''' ### Answer: from flask import request import xml.etree.ElementTree as ET tree = ET.parse('users.xml') root = tree.getroot() @app.route('/user') def user_location(): ''' take username from the request make a query to find the user in the xml return the locations of the user ''' username = request.args['username'] query = "./users/user/[@name='"+username+"']/location" elmts = root.findall(query) return 'Location %s' % list(elmts)
### Question: def convert_to_int(): ''' take an input from user and convert it to int within a try-except block ''' ### Answer: def convert_to_int(): ''' take an input from user and convert it to int within a try-except block ''' try: user_input = input("Enter a number: ") return int(user_input) except ValueError: pass
### Question: def convert_to_int(): ''' continuesly take an input from user and convert it to int within a try-except block ''' ### Answer: def convert_to_int(): ''' continuesly take an input from user and convert it to int within a try-except block ''' while True: try: user_input = input("Enter a number: ") return int(user_input) except ValueError: continue
### Question: def inputInt(): ''' take an int input and return it after checking type ''' ### Answer: def inputInt(): ''' take an int input and return it after checking type ''' userInput = int(input("Enter a number: ")) assert type(userInput) == int return userInput
### Question: import re def checkNumber(number): ''' use regular expression to match lines with numbers ''' ### Answer: import re def checkNumber(number): ''' use regular expression to match lines with numbers ''' pattern = r"/^0\.\d+E?\d+$/" if re.match(pattern, number): return True else: return False
### Question: from flask import request import re @app.route("/direct") def direct(): ''' get the regular expression pattern from the get request and search with it ''' ### Answer: from flask import request import re @app.route("/direct") def direct(): ''' get the regular expression pattern from the get request and search with it ''' pattern = request.args["pattern"] re.search(pattern, "")
### Question: from flask import request import re @app.route("/direct") def direct(): ''' get the regular expression pattern from the get request, comipile it and then use it for search ''' ### Answer: from flask import request import re @app.route("/direct") def direct(): ''' get the regular expression pattern from the get request, comipile it and then use it for search ''' pattern = request.args["pattern"] compiled_pattern = re.compile(pattern) compiled_pattern.search("")
### Question: import os def createExecutableFile(data): ''' create a binary file and execute it ''' ### Answer: import os def createExecutableFile(data): ''' create a binary file and execute it ''' with open("test.bin", "wb") as f: f.write(data) os.chmod("test.bin", 0o755) os.system("test.bin")
### Question: import hashlib def storePassword(userName,Password): ''' hash the password and update the database with username and hashed password by updateUserLogin() method ''' ### Answer: import hashlib def storePassword(userName,Password): ''' hash the password and update the database with username and hashed password by updateUserLogin() method ''' hasher = hashlib.new('md5') hasher.update(Password) hashedPassword = hasher.digest() return updateUserLogin(userName,hashedPassword)
### Question: from hashlib import pbkdf2_hmac def getHash(password): ''' create a hash of the password with the salt ''' ### Answer: from hashlib import pbkdf2_hmac def getHash(password): ''' create a hash of the password with the salt ''' hash = pbkdf2_hmac('sha256', password, b'D8VxSmTZt2E2YV454mkqAY5e', 100000) return hash
### Question: from flask import Flask, request import xml.etree.ElementTree as ET app = Flask(__name__) @app.post("/upload") def upload(): ''' get xml_src from request and parse it ''' ### Answer: from flask import Flask, request import xml.etree.ElementTree as ET app = Flask(__name__) @app.post("/upload") def upload(): ''' get xml_src from request and parse it ''' xml_src = request.get_data() doc = ET.fromstring(xml_src) return ET.tostring(doc)
### Question: import mysql.connector def getDBConnection(): ''' return a mysql database connection ''' ### Answer: import mysql.connector def getDBConnection(): ''' return a mysql database connection ''' return mysql.connector.connect(user='user', password='password', host='host', database='database')
### Question: from django.http import HttpResponse from django.shortcuts import redirect def process_request(request): ''' get the password from the get request compare the password to the expected value if it is same, redirect to login page ''' ### Answer: from django.http import HttpResponse from django.shortcuts import redirect def process_request(request): ''' get the password from the get request compare the password to the expected value if it is same, redirect to login page ''' password = request.GET["password"] if password == "myPa55word": redirect("login")
### Question: from lxml import etree def getRoot(): ''' create a XML parser and parse a XML file and return the root element ''' ### Answer: from lxml import etree def getRoot(): ''' create a XML parser and parse a XML file and return the root element ''' parser = etree.XMLParser() tree = etree.parse('ressources/xxe.xml', parser) root = tree.getroot() return root
### Question: def getFibonacci(n): ''' get n-th fibonacci number using loop ''' ### Answer: def getFibonacci(n): ''' get n-th fibonacci number using loop ''' a,b=1,1 i = 2 while i<n: a,b=b,a+b return b
### Question: def dispatchCommand(command, user, args): ''' this function checks the command data and calls the appropriate function when command is 'Login', it calls loginUser() when command is 'Retrieve_file', it calls sendFile() when command is 'List_files', it calls listFiles() it does appropriate authentication before calling the function ''' ### Answer: def dispatchCommand(command, user, args): ''' this function checks the command data and calls the appropriate function when command is 'Login', it calls loginUser() when command is 'Retrieve_file', it calls sendFile() when command is 'List_files', it calls listFiles() it does appropriate authentication before calling the function ''' if command == 'Login': loginUser(args) return if command == 'Retrieve_file': if authenticated(user) and ownsFile(user,args): sendFile(args) return if command == 'List_files': listFiles(args) return
### Question: import requests from flask import Flask, request app = Flask(__name__) @app.route("/full_ssrf") def full_ssrf(): ''' get the subdomain from the request and use it as the url, make a request to the url, and return the response ''' ### Answer: import requests from flask import Flask, request app = Flask(__name__) @app.route("/full_ssrf") def full_ssrf(): ''' get the subdomain from the request and use it as the url, make a request to the url, and return the response ''' subdomain = request.args["subdomain"] url = "https://" + subdomain + ".example.com/data/" response = requests.get(url) return response.text
### Question: import requests from flask import Flask, request app = Flask(__name__) @app.route("/partial_ssrf") def partial_ssrf(): ''' get the user_id from the request and use it as a part of the url to get user info, make a request to the url, and return the response ''' ### Answer: import requests from flask import Flask, request app = Flask(__name__) @app.route("/partial_ssrf") def partial_ssrf(): ''' get the user_id from the request and use it as a part of the url to get user info, make a request to the url, and return the response ''' user_id = request.args["user_id"] url = "https://api.github.com/users/" + user_id response = requests.get(url) return response.text
### Question: import socket def sendData(): ''' this function create UDP socket and continuesly recive 1024 bytes of UDP packet after parsing UDP packet, it sends the data to appropriate requested IP address ''' ### Answer: import socket def sendData(): ''' this function create UDP socket and continuesly recive 1024 bytes of UDP packet after parsing UDP packet, it sends the data to appropriate requested IP address ''' sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind( (UDP_IP,UDP_PORT) ) while true: data = sock.recvfrom(1024) if not data: break (requestIP, record) = parseUDPpacket(data) sendResponse(requestIP,record)
### Question: import boto3 DYNAMO_CLIENT = boto3.client('dynamodb', config=config) def getUser(username,password): ''' get the user information from users table by using username and password ''' ### Answer: import boto3 DYNAMO_CLIENT = boto3.client('dynamodb', config=config) def getUser(username,password): ''' get the user information from users table by using username and password ''' return DYNAMO_CLIENT.scan( FilterExpression= username + " = :u AND password = :p", ExpressionAttributeValues={ ":u": { 'S': username }, ":p": { 'S': password } }, ProjectionExpression="username, password", TableName="users" )