Skip to content

Latest commit

 

History

History
188 lines (156 loc) · 5.82 KB

readme.md

File metadata and controls

188 lines (156 loc) · 5.82 KB

SPYTHON 2.7.15

一、修改Python源码(仅针对Python-2.7.15

下载Python-2.7.15解释器的源码并对其进行如下的修改以运行加密的Python源码:

推荐使用aliyun镜像下载python源码

1. 添加源文件

复制文件path/to/spython/src/Python/decrypt_source_file.cpath/to/spython/spython-2.7.15/Python-2.7.15/Python
复制文件path/to/spython/src/Include/decrypt_source_file.hpath/to/spython/spython-2.7.15/Python-2.7.15/Include

2. 添加解密函数到Python源码中

diff --git a/Modules/main.c b/Modules/main.c
index a6edf82..3e69ca2 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -4,6 +4,7 @@
 #include "osdefs.h"
 #include "code.h" /* For CO_FUTURE_DIVISION */
 #include "import.h"
+#include "decrypt_source_file.h"
 
 #ifdef __VMS
 #include <unixlib.h>
@@ -607,7 +606,7 @@ Py_Main(int argc, char **argv)
         }
 
         if (sts==-1 && filename!=NULL) {
-            if ((fp = fopen(filename, "r")) == NULL) {
+            if ((fp = d_open(filename, "r")) == NULL) {
                 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
                     argv[0], filename, errno, strerror(errno));

diff --git a/Python/import.c b/Python/import.c
index 1d74faf..3a31846 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -14,6 +14,7 @@
 #include "eval.h"
 #include "osdefs.h"
 #include "importdl.h"
+#include "decrypt_source_file.h"
 
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
@@ -1575,7 +1575,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
             filemode = fdp->mode;
             if (filemode[0] == 'U')
                 filemode = "r" PY_STDIOTEXTMODE;
-            fp = fopen(buf, filemode);
+            fp = d_open(buf, filemode);
             if (fp != NULL) {
                 if (case_ok(buf, len, namelen, name))
                     break;

3. 禁止生成.pyc文件

由于开发者可以通过.pyc反编译出python源代码,所以需禁止生成.pyc文件

--- a/Modules/main.c
+++ b/Modules/main.c
@@ -64,7 +64,6 @@ static char *usage_1 = "\
 Options and arguments (and corresponding environment variables):\n\
 -b     : issue warnings about comparing bytearray with unicode\n\
          (-bb: issue errors)\n\
--B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
 -c cmd : program passed in as string (terminates option list)\n\
 -d     : debug output from parser; also PYTHONDEBUG=x\n\
 -E     : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
@@ -375,9 +374,9 @@ Py_Main(int argc, char **argv)
             Py_OptimizeFlag++;
             break;
 
-        case 'B':
-            Py_DontWriteBytecodeFlag++;
-            break;
+        // case 'B':
+        //     Py_DontWriteBytecodeFlag++;
+        //     break;
 
         case 's':
             Py_NoUserSiteDirectory++;
@@ -443,7 +442,7 @@ Py_Main(int argc, char **argv)
 
         }
     }
-
+    Py_DontWriteBytecodeFlag++;
     if (help)
         return usage(0, argv[0]);
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 44fe13d..c7a62ba 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -191,8 +191,8 @@ Py_InitializeEx(int install_sigs)
         Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
     if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
         Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
-    if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
-        Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
+    // if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
+    //     Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
     /* The variable is only tested for existence here; _PyRandom_Init will
        check its value further. */
     if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')

4. 禁止访问字节码

diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index a66aa69..33bc4eb 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -202,7 +202,7 @@ static PyMemberDef code_memberlist[] = {
     {"co_nlocals",      T_INT,          OFF(co_nlocals),        READONLY},
     {"co_stacksize",T_INT,              OFF(co_stacksize),      READONLY},
     {"co_flags",        T_INT,          OFF(co_flags),          READONLY},
-    {"co_code",         T_OBJECT,       OFF(co_code),           READONLY},
+    // {"co_code",         T_OBJECT,       OFF(co_code),           READONLY},
     {"co_consts",       T_OBJECT,       OFF(co_consts),         READONLY},
     {"co_names",        T_OBJECT,       OFF(co_names),          READONLY},
     {"co_varnames",     T_OBJECT,       OFF(co_varnames),       READONLY},

5. 修改Makefile.pre.in文件

Makefile需要链接libssl库,生成decrypt_source_file.o

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 9297e7f..4e11afe 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -185,7 +185,7 @@ INSTSONAME=	@INSTSONAME@
 LIBS=		@LIBS@
 LIBM=		@LIBM@
 LIBC=		@LIBC@
-SYSLIBS=	$(LIBM) $(LIBC)
+SYSLIBS=	$(LIBM) $(LIBC)  -lcrypto -lssl
 SHLIBS=		@SHLIBS@
 
 THREADOBJ=	@THREADOBJ@
@@ -294,6 +294,7 @@ PGENOBJS=	$(POBJS) $(PGOBJS)
 
 ##########################################################################
 PYTHON_OBJS=	\
+		Python/decrypt_source_file.o \
 		Python/_warnings.o \
 		Python/Python-ast.o \
 		Python/asdl.o \
@@ -752,6 +753,7 @@ Python/formatter_string.o: $(srcdir)/Python/formatter_string.c \
 # Header files
 
 PYTHON_HEADERS= \
+		Include/decrypt_source_file.h \
 		Include/Python-ast.h \
 		Include/Python.h \
 		Include/abstract.h \

6. 编译安装Python-2.7.15

cd path/to/spython/spython-2.7.15/
tar zxvf Python-2.7.15.tgz
cd Python-2.7.15
./configure --prefix=$PREFIX
make
sudo make install

7. 编译安装spython-enc

cd path/to/spython
make
sudo make install