Beispiel #1
0
 private void SetupRegexFunctions()
 {
     AddFunction("rmatch", "True if regex matches parameter",
                 (context, arguments) =>
     {
         var regex  = AutoBind.StringArgument(arguments[0]);
         var value  = AutoBind.StringArgument(arguments[1]);
         var result = System.Text.RegularExpressions.Regex.Match(value, regex);
         return(result);
     }, Arguments.Arg("regex"), Arguments.Arg("value"));
 }
Beispiel #2
0
        private void SetupVariableFunctions()
        {
            AddFunction("var",
                        "name value : Assign value to a variable named [name].", (context, arguments) =>
            {
                if (specialVariables.ContainsKey(ScriptObject.AsString(arguments[0])))
                {
                    context.RaiseNewError("Can't assign to protected variable name.", context.currentNode);
                }
                else
                {
                    try
                    {
                        context.Scope.ChangeVariable(ScriptObject.AsString(arguments[0]), arguments[1]);
                    }
                    catch (Exception e) { context.RaiseNewError(e.Message, context.currentNode); }
                }
                return(arguments[1]);
            },
                        Arguments.Mutator(Arguments.Lazy("name"), "(@identifier value)"),
                        Arguments.Arg("value"));

            AddFunction("let",
                        "^( ^(\"name\" value ?cleanup-code) ^(...) ) code : Create temporary variables, run code. Optional clean-up code for each variable.",
                        (context, arguments) =>
            {
                var variables = ArgumentType <ScriptObject>(arguments[0]);
                if (!String.IsNullOrEmpty(variables.gsp("@prefix"))) /* Raise warning */ } {
                    var code      = ArgumentType <ScriptObject>(arguments[1]);
                    var cleanUp   = new List <LetVariable>();
                    Object result = null;

                    foreach (var item in variables._children)
                    {
                        var sitem = item as ScriptObject;
                        if (sitem._children.Count <= 1 || sitem._children.Count > 3)
                        {
                            context.RaiseNewError("Bad variable definition in let", context.currentNode);
                            goto RUN_CLEANUP;
                        }

                        var nameObject = sitem._child(0) as ScriptObject;
                        var varName    = "";

                        if (nameObject.gsp("@type") == "token")
                        {
                            varName = nameObject.gsp("@token");
                        }
                        else
                        {
                            varName = AutoBind.StringArgument(Evaluate(context, nameObject, true));
                        }
                        if (context.evaluationState != EvaluationState.Normal)
                        {
                            goto RUN_CLEANUP;
                        }

                        var varValue = Evaluate(context, sitem._child(1), false);
                        if (context.evaluationState != EvaluationState.Normal)
                        {
                            goto RUN_CLEANUP;
                        }

                        context.Scope.PushVariable(varName, varValue);
                        cleanUp.Add(new LetVariable
                    {
                        name        = varName,
                        cleanupCode = sitem._children.Count == 3 ? sitem._child(2) : null
                    });
                    }

                    result = Evaluate(context, code, true);

                    RUN_CLEANUP:
                    var pre_state = context.evaluationState;
                    foreach (var item in cleanUp)
                    {
                        if (item.cleanupCode != null)
                        {
                            context.evaluationState = EvaluationState.Normal;
                            Evaluate(context, item.cleanupCode, true, true);
                        }
                        context.Scope.PopVariable(item.name);
                    }
                    context.evaluationState = pre_state;
                    return(result);
},
Beispiel #3
0
        private void SetupFileFunctions()
        {
            var file_functions = new GenericScriptObject();

            file_functions.SetProperty("open", Function.MakeSystemFunction("open",
                                                                           Arguments.Args("file-name", "mode"), "Opens a file",
                                                                           (context, arguments) =>
            {
                var mode = AutoBind.StringArgument(arguments[1]).ToUpperInvariant();
                if (mode == "READ")
                {
                    return(System.IO.File.OpenText(AutoBind.StringArgument(arguments[0])));
                }
                else if (mode == "WRITE")
                {
                    return(System.IO.File.CreateText(AutoBind.StringArgument(arguments[0])));
                }
                else if (mode == "APPEND")
                {
                    return(System.IO.File.AppendText(AutoBind.StringArgument(arguments[0])));
                }
                else
                {
                    context.RaiseNewError("Invalid mode specifier", context.currentNode);
                }
                return(null);
            }));

            file_functions.SetProperty("close", Function.MakeSystemFunction("close",
                                                                            Arguments.Args("file"), "Close a file.",
                                                                            (context, arguments) =>
            {
                if (arguments[0] is System.IO.StreamReader)
                {
                    (arguments[0] as System.IO.StreamReader).Close();
                }
                else if (arguments[0] is System.IO.StreamWriter)
                {
                    (arguments[0] as System.IO.StreamWriter).Close();
                }
                else
                {
                    context.RaiseNewError("Argument is not a file.", context.currentNode);
                }
                return(null);
            }));

            file_functions.SetProperty("read-line", Function.MakeSystemFunction("read-line",
                                                                                Arguments.Args("file"), "Read a line from a file.",
                                                                                (context, arguments) =>
            {
                var file = arguments[0] as System.IO.StreamReader;
                if (file == null)
                {
                    context.RaiseNewError("Argument is not a read file.", context.currentNode);
                    return(null);
                }
                return(file.ReadLine());
            }));

            file_functions.SetProperty("read-all", Function.MakeSystemFunction("read-all",
                                                                               Arguments.Args("file"), "Read all of a file.",
                                                                               (context, arguments) =>
            {
                var file = arguments[0] as System.IO.StreamReader;
                if (file == null)
                {
                    context.RaiseNewError("Argument is not a read file.", context.currentNode);
                    return(null);
                }
                return(file.ReadToEnd());
            }));

            file_functions.SetProperty("write", Function.MakeSystemFunction("write",
                                                                            Arguments.Args("file", "text"), "Write to a file.",
                                                                            (context, arguments) =>
            {
                var file = arguments[0] as System.IO.StreamWriter;
                if (file == null)
                {
                    context.RaiseNewError("Argument is not a write file.", context.currentNode);
                    return(null);
                }
                file.Write(ScriptObject.AsString(arguments[1]));
                return(null);
            }));

            file_functions.SetProperty("more", Function.MakeSystemFunction("more",
                                                                           Arguments.Args("file"), "Is there more to read in this file?",
                                                                           (context, arguments) =>
            {
                var file = arguments[0] as System.IO.StreamReader;
                if (file == null)
                {
                    context.RaiseNewError("Argument is not a read file.", context.currentNode);
                    return(null);
                }
                if (file.EndOfStream)
                {
                    return(null);
                }
                return(true);
            }));

            AddGlobalVariable("file", c => file_functions);
        }
Beispiel #4
0
        public void SetupStandardConsoleFunctions(Engine mispEngine)
        {
            mispEngine.AddFunction("print", "Print something.",
                                   (context, arguments) =>
            {
                noEcho = true;
                foreach (var item in arguments[0] as ScriptList)
                {
                    Write(UnescapeString(MISP.Console.PrettyPrint2(item, 0)));
                }
                return(null);
            }, Arguments.Optional(Arguments.Repeat("value")));

            mispEngine.AddFunction("printf", "Print a string with formatting.",
                                   (context, arguments) =>
            {
                noEcho = true;
                var s  = String.Format(AutoBind.StringArgument(arguments[0]),
                                       AutoBind.ListArgument(arguments[1]).ToArray());
                Write(UnescapeString(s));
                return(null);
            },
                                   Arguments.Arg("format-string"),
                                   Arguments.Optional(Arguments.Repeat("value")));


            mispEngine.AddFunction("emitfl", "Emit a list of functions",
                                   (context, arguments) =>
            {
                noEcho = true;
                if (arguments[0] == null)
                {
                    foreach (var item in mispEngine.functions)
                    {
                        EmitFunctionListItem(item.Value);
                    }
                }
                else if (arguments[0] is String)
                {
                    var regex = new System.Text.RegularExpressions.Regex(arguments[0] as String);
                    foreach (var item in mispEngine.functions)
                    {
                        if (regex.IsMatch(item.Value.gsp("@name")))
                        {
                            EmitFunctionListItem(item.Value);
                        }
                    }
                }
                else
                {
                    foreach (var item in AutoBind.ListArgument(arguments[0]))
                    {
                        if (item is ScriptObject)
                        {
                            EmitFunctionListItem(item as ScriptObject);
                        }
                    }
                }
                return(null);
            }, Arguments.Optional("list"));

            mispEngine.AddFunction("module", "Load a module from disc",
                                   (context, arguments) =>
            {
                return(NetModule.LoadModule(mispEngine, AutoBind.StringArgument(arguments[0]),
                                            AutoBind.StringArgument(arguments[1])));
            }, Arguments.Arg("file"), Arguments.Arg("module"));
        }
Beispiel #5
0
        private void SetupStringFunctions()
        {
            AddFunction("split", "Split a string into pieces", (context, arguments) =>
            {
                var pieces = AutoBind.StringArgument(arguments[0]).Split(
                    new String[] { AutoBind.StringArgument(arguments[1]) },
                    Int32.MaxValue, StringSplitOptions.RemoveEmptyEntries);
                var r = new ScriptList(pieces);
                return(r);
            }, Arguments.Arg("string"), Arguments.Arg("split-chars"));

            AddFunction("strlen", "string : Returns length of string.",
                        (context, arguments) =>
            {
                return(ScriptObject.AsString(arguments[0]).Length);
            }, Arguments.Arg("string"));

            AddFunction("strind",
                        "string n : Returns nth element in string.",
                        (context, arguments) =>
            {
                var index = arguments[1] as int?;
                if (index == null || !index.HasValue)
                {
                    return(null);
                }
                if (index.Value < 0)
                {
                    return(null);
                }
                var str = ScriptObject.AsString(arguments[0]);
                if (index.Value >= str.Length)
                {
                    return(null);
                }
                return(str[index.Value]);
            },
                        Arguments.Arg("string"),
                        Arguments.Arg("n"));

            AddFunction("substr", "Returns a portion of the input string.",
                        (context, arguments) =>
            {
                var str   = ScriptObject.AsString(arguments[0]);
                var start = AutoBind.IntArgument(arguments[1]);
                if (arguments[2] == null)
                {
                    return(str.Substring(start));
                }
                else
                {
                    return(str.Substring(start, AutoBind.IntArgument(arguments[2])));
                }
            },
                        Arguments.Arg("string"),
                        Arguments.Arg("start"),
                        Arguments.Optional("length"));

            AddFunction("strcat", "Concatenate many strings into one.",
                        (context, arguments) =>
            {
                var r = new StringBuilder();
                foreach (var obj in AutoBind.ListArgument(arguments[0]))
                {
                    r.Append(ScriptObject.AsString(obj));
                }
                return(r.ToString());
            },
                        Arguments.Repeat("item"));

            AddFunction("itoa", "Change a number to the string representation.",
                        (context, arguments) =>
            {
                return(arguments[0].ToString());
            },
                        Arguments.Arg("i"));

            AddFunction("atoi", "",
                        (context, arguments) =>
            {
                return(Convert.ToInt32(arguments[0]));
            },
                        Arguments.Arg("i"));

            AddFunction("unescape", "", (context, arguments) =>
            {
                return(Console.UnescapeString(ScriptObject.AsString(arguments[0])));
            },
                        Arguments.Arg("string"));

            AddFunction("format", "Format a string.",
                        (context, arguments) =>
            {
                return(String.Format(AutoBind.StringArgument(arguments[0]),
                                     AutoBind.ListArgument(arguments[1]).ToArray()));
            },
                        Arguments.Arg("format-string"),
                        Arguments.Optional(Arguments.Repeat("value")));
        }