ironpython2 icon indicating copy to clipboard operation
ironpython2 copied to clipboard

scapy import error

Open 15871722713 opened this issue 6 years ago • 6 comments

I use command 'pip install scapy' to install scapy , and it install sucessful (C:\Program Files\IronPython 2.7\Lib\site-packages\scapy). It's failed to import scapy in ironpython , but it's ok in python.

import re import telnetlib import threading import logger from time import sleep from random import randint from config_read import get_config_by_name from scapy.all import *

error information is . ipy test_mac_capacity_test.py Traceback (most recent call last): File "test_mac_capacity_test.py", line 13, in File "C:\Users\Administrator\Desktop\sw_test\test\sw_common_win.py", line 15, in File "C:\Program Files\IronPython 2.7\Lib\site-packages\scapy\all.py", line 12, in File "C:\Program Files\IronPython 2.7\Lib\site-packages\scapy\base_classes.py", line 25, in File "C:\Program Files\IronPython 2.7\Lib\site-packages\scapy\consts.py", line 33, in AttributeError: 'module' object has no attribute 'uname'

I use windows 10 and IronPython 2.7.9 (2.7.9.0) on .NET 4.0.30319.42000 (64-bit).

In "C:\Program Files\IronPython 2.7\Lib\site-packages\scapy\consts.py" WINDOWS = platform.startswith("win32") if WINDOWS: try: if float(platform_lib.release()) >= 8.1: LOOPBACK_NAME = "Microsoft KM-TEST Loopback Adapter" else: LOOPBACK_NAME = "Microsoft Loopback Adapter" except ValueError: LOOPBACK_NAME = "Microsoft Loopback Adapter" LOOPBACK_INTERFACE = None else: uname = os.uname() LOOPBACK_NAME = "lo" if LINUX else "lo0" LOOPBACK_INTERFACE = LOOPBACK_NAME

sys.platform = cli in ironpython but it is 'win32' in python.

I need to use scapy to send packets , so could you give me some suggestions。

15871722713 avatar Aug 29 '19 02:08 15871722713

You could try adding the following at the top of your script:

class DualPlatform(str):
    def __eq__(self, other):
        return str.__eq__(self, other) or str.__eq__("cli", other)
    def __ne__(self, other):
        return str.__ne__(self, other) and str.__ne__("cli", other)

import sys
sys.platform = DualPlatform("win32")

slozier avatar Aug 29 '19 06:08 slozier

class DualPlatform(str): def eq(self, other): return str.eq(self, other) or str.eq("cli", other)

def __ne__(self, other):
    return str.__ne__(self, other) and str.__ne__("cli", other)

import sys sys.platform = DualPlatform("win32")

from scapy.all import *

I add this at the top of my script , but there is other errors.

C:\Users\Administrator\Desktop\sw_test\test>ipy sw_common_win.py

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at InteropInvoker(IntPtr , Object , Object[] ) at CallSite.Target(Closure , CallSite , Object , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at Microsoft.Scripting.Interpreter.DynamicInstruction3.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3) at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2) at Microsoft.Scripting.Interpreter.FuncCallInstruction6.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3) at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) at IronPython.Compiler.Ast.CallExpression.Invoke0Instruction.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at IronPython.Compiler.Ast.CallExpression.Invoke0Instruction.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2) at Microsoft.Scripting.Interpreter.FuncCallInstruction6.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3) at IronPython.Runtime.Method.MethodBinding.SelfTarget(CallSite site, CodeContext context, Object target) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at Microsoft.Scripting.Interpreter.FuncCallInstruction5.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run3[T0,T1,T2,TRet](T0 arg0, T1 arg1, T2 arg2) at IronPython.Compiler.Ast.CallExpression.Invoke0Instruction.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options) at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path) at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str) at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func5 defaultLoader) at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path) at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path) at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current) at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level) at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level) at Microsoft.Scripting.Interpreter.FuncCallInstruction7.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run8[T0,T1,T2,T3,T4,T5,T6,T7,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level) at IronPython.Runtime.Operations.PythonOps.ImportStar(CodeContext context, String fullName, Int32 level) at Microsoft.Scripting.Interpreter.ActionCallInstruction3.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options) at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path) at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str) at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func5 defaultLoader) at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path) at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path) at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current) at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level) at IronPython.Modules.Builtin.import(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level) at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level) at IronPython.Runtime.Operations.PythonOps.ImportStar(CodeContext context, String fullName, Int32 level) at Microsoft.Scripting.Interpreter.ActionCallInstruction3.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options) at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path) at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str) at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func5 defaultLoader) at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path) at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path) at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current) at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level) at IronPython.Modules.Builtin.import(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level) at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level) at IronPython.Runtime.Operations.PythonOps.ImportStar(CodeContext context, String fullName, Int32 level) at Microsoft.Scripting.Interpreter.ActionCallInstruction`3.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) at IronPython.Hosting.PythonCommandLine.RunFileWorker(String fileName) at IronPython.Hosting.PythonCommandLine.RunFile(String fileName) at Microsoft.Scripting.Hosting.Shell.CommandLine.Run() at IronPython.Hosting.PythonCommandLine.Run() at Microsoft.Scripting.Hosting.Shell.CommandLine.Run(ScriptEngine engine, IConsole console, ConsoleOptions options) at Microsoft.Scripting.Hosting.Shell.ConsoleHost.RunCommandLine() at Microsoft.Scripting.Hosting.Shell.ConsoleHost.ExecuteInternal() at Microsoft.Scripting.Hosting.Shell.ConsoleHost.Run(String[] args) at PythonConsoleHost.Main(String[] args)

15871722713 avatar Aug 30 '19 03:08 15871722713

Looks like an issue with ctypes. I was able to get around the issue by modifying the scapy source but then I immediately hit another ctypes issue. This will likely require a bit of time debugging...

slozier avatar Aug 30 '19 21:08 slozier

I have a problem with ctypes too. When I use "import ctypes" in a python script launched with Ironpython I have an exception "No module named ctypes".

valentinbreiz avatar Sep 02 '19 09:09 valentinbreiz

@valentinbreiz Your problem is unrelated to this particular issue. It sounds more like you're missing the standard library?

slozier avatar Sep 06 '19 00:09 slozier

The crash appears to occur when passing a pointer that was created using ctypes.cast. Here's a repro for the ipy crash:

import ctypes

class POINT(ctypes.Structure):
    _fields_ = [('x', ctypes.c_long), ('y', ctypes.c_long)]

buf = ctypes.create_string_buffer(ctypes.sizeof(POINT))
ptr = ctypes.cast(buf, ctypes.POINTER(POINT))
ctypes.windll.user32.GetCursorPos(ptr)
print(ptr.contents.x, ptr.contents.y)

slozier avatar Sep 06 '19 00:09 slozier