2022-03-21 16:51:00 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import sqliteParser from "sqlite-parser";
|
|
|
|
import { intersection, isPlainObject } from "lodash";
|
2024-01-30 15:31:06 +00:00
|
|
|
import { osqueryTablesAvailable } from "utilities/osquery_tables";
|
2022-03-21 16:51:00 +00:00
|
|
|
import {
|
2023-07-17 21:09:12 +00:00
|
|
|
OsqueryPlatform,
|
2022-03-21 23:06:25 +00:00
|
|
|
MACADMINS_EXTENSION_TABLES,
|
2022-03-21 16:51:00 +00:00
|
|
|
SUPPORTED_PLATFORMS,
|
2023-07-17 21:09:12 +00:00
|
|
|
SupportedPlatform,
|
2022-03-21 16:51:00 +00:00
|
|
|
} from "interfaces/platform";
|
|
|
|
|
|
|
|
type IAstNode = Record<string | number | symbol, unknown>;
|
|
|
|
|
|
|
|
// TODO: Research if there are any preexisting types for osquery schema
|
|
|
|
// TODO: Is it ever possible that osquery_tables.json would be missing name or platforms?
|
|
|
|
interface IOsqueryTable {
|
|
|
|
name: string;
|
2023-07-17 21:09:12 +00:00
|
|
|
platforms: OsqueryPlatform[];
|
2022-03-21 16:51:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 15:31:06 +00:00
|
|
|
type IPlatformDictionary = Record<string, OsqueryPlatform[]>;
|
2022-03-21 16:51:00 +00:00
|
|
|
|
2024-01-30 15:31:06 +00:00
|
|
|
const platformsByTableDictionary: IPlatformDictionary = (osqueryTablesAvailable as IOsqueryTable[]).reduce(
|
|
|
|
(dictionary: IPlatformDictionary, osqueryTable) => {
|
2022-03-21 16:51:00 +00:00
|
|
|
dictionary[osqueryTable.name] = osqueryTable.platforms;
|
|
|
|
return dictionary;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2022-03-21 23:06:25 +00:00
|
|
|
Object.entries(MACADMINS_EXTENSION_TABLES).forEach(([tableName, platforms]) => {
|
2022-03-21 16:51:00 +00:00
|
|
|
platformsByTableDictionary[tableName] = platforms;
|
|
|
|
});
|
|
|
|
|
|
|
|
// The isNode and visit functionality is informed by https://lihautan.com/manipulating-ast-with-javascript/#traversing-an-ast
|
|
|
|
const _isNode = (node: unknown): node is IAstNode => {
|
|
|
|
return !!node && isPlainObject(node);
|
|
|
|
};
|
|
|
|
|
|
|
|
const _visit = (
|
|
|
|
abstractSyntaxTree: IAstNode,
|
|
|
|
callback: (ast: IAstNode) => void
|
|
|
|
) => {
|
|
|
|
if (abstractSyntaxTree) {
|
|
|
|
callback(abstractSyntaxTree);
|
|
|
|
|
|
|
|
Object.keys(abstractSyntaxTree).forEach((key) => {
|
|
|
|
const childNode = abstractSyntaxTree[key];
|
|
|
|
if (Array.isArray(childNode)) {
|
|
|
|
childNode.forEach((grandchildNode) => _visit(grandchildNode, callback));
|
|
|
|
} else if (childNode && _isNode(childNode)) {
|
|
|
|
_visit(childNode, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-17 21:09:12 +00:00
|
|
|
const filterCompatiblePlatforms = (
|
|
|
|
sqlTables: string[]
|
|
|
|
): SupportedPlatform[] => {
|
2022-03-21 16:51:00 +00:00
|
|
|
if (!sqlTables.length) {
|
|
|
|
return [...SUPPORTED_PLATFORMS]; // if a query has no tables but is still syntatically valid sql, it is treated as compatible with all platforms
|
|
|
|
}
|
|
|
|
|
|
|
|
const compatiblePlatforms = intersection(
|
|
|
|
...sqlTables.map(
|
|
|
|
(tableName: string) => platformsByTableDictionary[tableName]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return SUPPORTED_PLATFORMS.filter((p) => compatiblePlatforms.includes(p));
|
|
|
|
};
|
|
|
|
|
2023-08-03 16:42:51 +00:00
|
|
|
export const parseSqlTables = (
|
2022-03-21 16:51:00 +00:00
|
|
|
sqlString: string,
|
|
|
|
includeCteTables = false
|
|
|
|
): string[] => {
|
|
|
|
let results: string[] = [];
|
|
|
|
|
|
|
|
// Tables defined via common table expression will be excluded from results by default
|
|
|
|
const cteTables: string[] = [];
|
|
|
|
|
|
|
|
const _callback = (node: IAstNode) => {
|
2024-01-03 20:40:33 +00:00
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(node.variant === "common" || node.variant === "recursive") &&
|
|
|
|
node.format === "table" &&
|
|
|
|
node.type === "expression"
|
|
|
|
) {
|
|
|
|
const targetName = node.target && (node.target as IAstNode).name;
|
|
|
|
targetName &&
|
|
|
|
typeof targetName === "string" &&
|
2022-03-21 16:51:00 +00:00
|
|
|
cteTables.push(targetName);
|
2024-01-03 20:40:33 +00:00
|
|
|
return;
|
2022-03-21 16:51:00 +00:00
|
|
|
}
|
2024-01-03 20:40:33 +00:00
|
|
|
|
|
|
|
node.variant === "table" &&
|
|
|
|
// ignore table-valued functions (see, e.g., https://www.sqlite.org/json1.html#jeach)
|
|
|
|
node.type !== "function" &&
|
|
|
|
node.name &&
|
|
|
|
typeof node.name === "string" &&
|
|
|
|
results.push(node.name);
|
2022-03-21 16:51:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const sqlTree = sqliteParser(sqlString);
|
|
|
|
_visit(sqlTree, _callback);
|
|
|
|
|
|
|
|
if (cteTables.length && !includeCteTables) {
|
|
|
|
results = results.filter((r: string) => !cteTables.includes(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
} catch (err) {
|
2022-04-11 20:18:31 +00:00
|
|
|
// console.log(`sqlite-parser error: ${err}\n\n${sqlString}`);
|
2022-03-21 16:51:00 +00:00
|
|
|
|
2022-04-11 20:18:31 +00:00
|
|
|
throw err;
|
2022-03-21 16:51:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-03 16:42:51 +00:00
|
|
|
export const checkTable = (
|
|
|
|
sqlString = "",
|
|
|
|
includeCteTables = false
|
|
|
|
): { tables: string[] | null; error: Error | null } => {
|
|
|
|
let sqlTables: string[] | undefined;
|
|
|
|
try {
|
|
|
|
sqlTables = parseSqlTables(sqlString, includeCteTables);
|
|
|
|
} catch (err) {
|
|
|
|
return { tables: null, error: new Error(`${err}`) };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlTables === undefined) {
|
|
|
|
return {
|
|
|
|
tables: null,
|
|
|
|
error: new Error(
|
|
|
|
"Unexpected error checking table names: sqlTables are undefined"
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { tables: sqlTables, error: null };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkPlatformCompatibility = (
|
2022-03-21 16:51:00 +00:00
|
|
|
sqlString: string,
|
|
|
|
includeCteTables = false
|
2023-07-17 21:09:12 +00:00
|
|
|
): { platforms: SupportedPlatform[] | null; error: Error | null } => {
|
2022-03-21 16:51:00 +00:00
|
|
|
let sqlTables: string[] | undefined;
|
|
|
|
try {
|
2024-01-03 20:40:33 +00:00
|
|
|
// get tables from str
|
2022-03-21 16:51:00 +00:00
|
|
|
sqlTables = parseSqlTables(sqlString, includeCteTables);
|
|
|
|
} catch (err) {
|
2022-04-11 20:18:31 +00:00
|
|
|
return { platforms: null, error: new Error(`${err}`) };
|
2022-03-21 16:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlTables === undefined) {
|
2022-04-11 20:18:31 +00:00
|
|
|
return {
|
|
|
|
platforms: null,
|
|
|
|
error: new Error(
|
|
|
|
"Unexpected error checking platform compatibility: sqlTables are undefined"
|
|
|
|
),
|
|
|
|
};
|
2022-03-21 16:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 20:18:31 +00:00
|
|
|
try {
|
2024-01-03 20:40:33 +00:00
|
|
|
// use tables to get platforms
|
2022-04-11 20:18:31 +00:00
|
|
|
const platforms = filterCompatiblePlatforms(sqlTables);
|
|
|
|
return { platforms, error: null };
|
|
|
|
} catch (err) {
|
|
|
|
return { platforms: null, error: new Error(`${err}`) };
|
|
|
|
}
|
2022-03-21 16:51:00 +00:00
|
|
|
};
|
|
|
|
|
2023-08-03 16:42:51 +00:00
|
|
|
export const sqlKeyWords = [
|
|
|
|
"select",
|
|
|
|
"insert",
|
|
|
|
"update",
|
|
|
|
"delete",
|
|
|
|
"from",
|
|
|
|
"where",
|
|
|
|
"and",
|
|
|
|
"or",
|
|
|
|
"group",
|
|
|
|
"by",
|
|
|
|
"order",
|
|
|
|
"limit",
|
|
|
|
"offset",
|
|
|
|
"having",
|
|
|
|
"as",
|
|
|
|
"case",
|
|
|
|
"when",
|
|
|
|
"else",
|
|
|
|
"end",
|
|
|
|
"type",
|
|
|
|
"left",
|
|
|
|
"right",
|
|
|
|
"join",
|
|
|
|
"on",
|
|
|
|
"outer",
|
|
|
|
"desc",
|
|
|
|
"asc",
|
|
|
|
"union",
|
|
|
|
"create",
|
|
|
|
"table",
|
|
|
|
"primary",
|
|
|
|
"key",
|
|
|
|
"if",
|
|
|
|
"foreign",
|
|
|
|
"not",
|
|
|
|
"references",
|
|
|
|
"default",
|
|
|
|
"null",
|
|
|
|
"inner",
|
|
|
|
"cross",
|
|
|
|
"natural",
|
|
|
|
"database",
|
|
|
|
"drop",
|
|
|
|
"grant",
|
|
|
|
];
|
|
|
|
|
|
|
|
// Note: `last` was removed from the list of built-in functions because it collides with the
|
|
|
|
// `last` table available in osquery
|
|
|
|
export const sqlBuiltinFunctions = [
|
|
|
|
"avg",
|
|
|
|
"count",
|
|
|
|
"first",
|
|
|
|
"max",
|
|
|
|
"min",
|
|
|
|
"sum",
|
|
|
|
"ucase",
|
|
|
|
"lcase",
|
|
|
|
"mid",
|
|
|
|
"len",
|
|
|
|
"round",
|
|
|
|
"rank",
|
|
|
|
"now",
|
|
|
|
"format",
|
|
|
|
"coalesce",
|
|
|
|
"ifnull",
|
|
|
|
"isnull",
|
|
|
|
"nvl",
|
|
|
|
];
|
|
|
|
|
|
|
|
export const sqlDataTypes = [
|
|
|
|
"int",
|
|
|
|
"numeric",
|
|
|
|
"decimal",
|
|
|
|
"date",
|
|
|
|
"varchar",
|
|
|
|
"char",
|
|
|
|
"bigint",
|
|
|
|
"float",
|
|
|
|
"double",
|
|
|
|
"bit",
|
|
|
|
"binary",
|
|
|
|
"text",
|
|
|
|
"set",
|
|
|
|
"timestamp",
|
|
|
|
"money",
|
|
|
|
"real",
|
|
|
|
"number",
|
|
|
|
"integer",
|
|
|
|
];
|
|
|
|
|
|
|
|
export default {
|
|
|
|
checkPlatformCompatibility,
|
|
|
|
checkTable,
|
|
|
|
sqlKeyWords,
|
|
|
|
sqlBuiltinFunctions,
|
|
|
|
sqlDataTypes,
|
|
|
|
};
|