Skip to content

Instantly share code, notes, and snippets.

@mgramin
Last active March 31, 2024 11:40
Show Gist options
  • Save mgramin/0dd3872813047a27886d387a188781cd to your computer and use it in GitHub Desktop.
Save mgramin/0dd3872813047a27886d387a188781cd to your computer and use it in GitHub Desktop.
Simple REST service for osquery
from flask import Flask, request
from flask_restful import Api, Resource
import osquery
class User(Resource):
@staticmethod
def get():
instance = osquery.SpawnInstance()
instance.open()
query = request.args.get('query', default='', type=str)
query = instance.client.query(query)
return query.response, 200
@staticmethod
def post():
instance = osquery.SpawnInstance()
instance.open()
query = request.data
query = instance.client.query(query)
return query.response, 200
app = Flask(__name__)
api = Api(app)
api.add_resource(User, "/exec")
app.run(host='0.0.0.0', debug=True, threaded=True, port=8082)
@RamrajSekar
Copy link

RamrajSekar commented May 23, 2019

@mgramin, How do I curl this API?I'm ending up with below error,

[root@r00etyn0c osq]# curl -k http://127.0.0.1:5000/exec?query=select version from os_version
[]
curl: (6) Could not resolve host: version; Name or service not known
curl: (6) Could not resolve host: from; Name or service not known
curl: (6) Could not resolve host: os_version; Name or service not known

However with below code I'm able to run the query but need it to be dynamic any help on this would be helpful,

`from flask import Flask, request
from flask_restful import Api, Resource
import osquery

class User(Resource):
@staticmethod
def get():
instance = osquery.SpawnInstance()
instance.open()
query = "select name,pid from processes"
#query = request.args.get('query', default='', type=str)
query = instance.client.query(query)
return query.response, 200

#instance = osquery.SpawnInstance()
#instance.open()

app = Flask(name)
api = Api(app)
api.add_resource(User, "/exec")
app.run(debug=True)
`

@mgramin
Copy link
Author

mgramin commented Jun 4, 2019

@RamrajSekar
Hi, its very simple:

curl -d 'select version from os_version' -H "Content-Type: application/json" -X POST http://127.0.0.1:8082/exec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment