private static MISP.ScriptList DecodeList(ReadOnlyDatagram datagram) { var r = new MISP.ScriptList(); uint count = 0; datagram.ReadUInt(out count, 8); byte[] temp = new byte[4]; for (int i = 0; i < count; ++i) { uint typeCode = 0; datagram.ReadUInt(out typeCode, 8); switch ((ScriptTypes)typeCode) { case ScriptTypes.List: r.Add(DecodeList(datagram)); break; case ScriptTypes.String: { String s; datagram.ReadString(out s); r.Add(s); } break; case ScriptTypes.Int32: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToInt32(temp, 0)); break; case ScriptTypes.UInt32: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToUInt32(temp, 0)); break; case ScriptTypes.Single: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToSingle(temp, 0)); break; case ScriptTypes.Bool: { uint b = 0; datagram.ReadUInt(out b, 8); if (b == 0) r.Add(null); else r.Add(true); } break; default: throw new MISP.ScriptError("Error decoding message", null); } } return r; }
internal static Object DeserializeObject(ReadOnlyDatagram datagram, SerializerState state, Database database) { uint rawTypeCode = 0; if (!datagram.ReadUInt(out rawTypeCode, 8)) throw new DeserializeError(); switch ((SerializedTypeCode)rawTypeCode) { case SerializedTypeCode.Null: return null; case SerializedTypeCode.Integer: { uint value = 0; if (!datagram.ReadUInt(out value, 32)) throw new DeserializeError(); return (int)value; } case SerializedTypeCode.String: { String value = ""; if (!datagram.ReadString(out value)) throw new DeserializeError(); return value; } case SerializedTypeCode.List: { uint length = 0; if (!datagram.ReadUInt(out length, 16)) throw new DeserializeError(); var result = new MISP.ScriptList(); for (int i = 0; i < length; ++i) result.Add(DeserializeObject(datagram, state, database)); return result; } case SerializedTypeCode.InternalObject: return state.ReadObject(datagram, SerializedTypeCode.InternalObject, database); case SerializedTypeCode.NamedObject: return state.ReadObject(datagram, SerializedTypeCode.NamedObject, database); default: throw new DeserializeError(); } }
private void SetupScript() { scriptEngine.AddGlobalVariable("clients", (s) => { return new MISP.ScriptList(ConnectedClients); }); scriptEngine.AddFunction("load", "name: Load an object from the database.", (context, arguments) => { var objectName = MISP.ScriptObject.AsString(arguments[0]); try { return database.LoadObject(objectName); } catch (Exception e) { //SendMessage(thisObject, "Failed to load object " + objectName + ": " + e.Message, true); return null; } }, "string name"); scriptEngine.AddFunction("reload", "name: Reloads an object from the database.", (context, arguments) => { var objectName = MISP.ScriptObject.AsString(arguments[0]); try { return database.ReLoadObject(objectName); } catch (Exception e) { //SendMessage(thisObject, "Failed to load object " + objectName + ": " + e.Message, true); return null; } }, "string name"); scriptEngine.AddFunction("create", "name: Create a new named object in the database.", (context, arguments) => { var objectName = MISP.ScriptObject.AsString(arguments[0]); try { return database.CreateObject(objectName); } catch (Exception e) { return null; } }, "string name"); scriptEngine.AddFunction("create-uniquely-named", "base-path: Directory to create new object in.", (context, arguments) => { var basePath = MISP.ScriptObject.AsString(arguments[0]); try { return database.CreateUniquelyNamedObject(basePath); } catch (Exception e) { return null; } }, "string base-path"); scriptEngine.AddFunction("save", "name: Save a named object.", (context, arguments) => { var objectName = MISP.ScriptObject.AsString(arguments[0]); try { database.SerializeObject(objectName); } catch (Exception e) { } return null; }, "string name"); scriptEngine.AddFunction("enumerate-database", "List all the database objects in a certain path.", (context, arguments) => { return database.EnumerateDirectory(arguments[0] as String); }, "string path"); scriptEngine.AddFunction("echo", "player<s> message: Send text to players.", (context, arguments) => { MISP.ScriptList to = null; if (arguments[0] is MISP.ScriptList) to = arguments[0] as MISP.ScriptList; else { to = new MISP.ScriptList(); to.Add(arguments[0]); } foreach (var obj in to) { if (obj is Client) (obj as Client).Send(MISP.ScriptObject.AsString(arguments[1])); else if (obj is MISP.ScriptObject) SendMessage(obj as MISP.ScriptObject, MISP.ScriptObject.AsString(arguments[1]), false); } return null; }, "to", "message"); scriptEngine.AddFunction("command", "player command: Send a command as if it came from player.", (context, arguments) => { Client client = null; foreach (var c in ConnectedClients) if (c.player == arguments[0]) client = c; if (client == null) client = new Client { player = arguments[0] as MISP.ScriptObject }; EnqueuAction(new Command( client, MISP.ScriptObject.AsString(arguments[1]))); return null; }, "object player", "string command"); scriptEngine.AddFunction("invoke", "seconds function arguments: Invoke a function in N seconds.", (context, arguments) => { var seconds = arguments[0] as int?; if (seconds == null || !seconds.HasValue) seconds = 0; EnqueuAction(new InvokeFunctionAction( MISP.Engine.ArgumentType<MISP.Function>(arguments[1]), MISP.Engine.ArgumentType<MISP.ScriptList>(arguments[2]), seconds.Value)); return null; }, "integer seconds", "function function", "list arguments"); scriptEngine.AddFunction("disconnect", "player : Disconnect a player.", (context, arguments) => { EnqueuAction(new DisconnectAction(MISP.Engine.ArgumentType<MISP.ScriptObject>(arguments[0]))); return null; }, "object player"); }