jos*_*sch 11 ctypes monkeypatching cpython pyopenssl python-3.x
我希望能够将证书传递给Python的ssl库,而无需临时文件.似乎Python的ssl模块不能这样做.
要解决此问题,我想从本机模块中检索SSL_CTX存储在ssl._ssl._SSLContext类中的基础结构_ssl.使用ctypes然后我可以SSL_CTX_*使用该上下文从libssl 手动调用相应的函数.这里显示了如何在C中执行此操作,我将通过ctypes执行相同的操作.
不幸的是,我陷入了我设法挂钩load_verify_locations函数的地步,ssl._ssl._SSLContext但似乎无法获得ssl._ssl._SSLContext结构实例的正确内存地址.load_verify_locations看到的所有函数都是父ssl.SSLContext对象.
我的问题是,如何从ssl.SSLContext对象的实例到本机基类的内存ssl._ssl._SSLContext?如果我愿意,我可以轻松访问其ctx成员.
到目前为止,这是我的代码.关于如何将本机Python模块进行monkeypatch的信用转到Lincoln Clarete的禁果项目
Py_ssize_t = hasattr(ctypes.pythonapi, 'Py_InitModule4_64') and ctypes.c_int64 or ctypes.c_int
class PyObject(ctypes.Structure):
pass
PyObject._fields_ = [
('ob_refcnt', Py_ssize_t),
('ob_type', ctypes.POINTER(PyObject)),
]
class SlotsProxy(PyObject):
_fields_ = [('dict', ctypes.POINTER(PyObject))]
class PySSLContext(ctypes.Structure):
pass
PySSLContext._fields_ = [
('ob_refcnt', Py_ssize_t),
('ob_type', ctypes.POINTER(PySSLContext)),
('ctx', ctypes.c_void_p),
]
name = ssl._ssl._SSLContext.__name__
target = ssl._ssl._SSLContext.__dict__
proxy_dict = SlotsProxy.from_address(id(target))
namespace = {}
ctypes.pythonapi.PyDict_SetItem(
ctypes.py_object(namespace),
ctypes.py_object(name),
proxy_dict.dict,
)
patchable = namespace[name]
old_value = patchable["load_verify_locations"]
libssl = ctypes.cdll.LoadLibrary("libssl.so.1.0.0")
libssl.SSL_CTX_set_verify.argtypes = (ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p)
libssl.SSL_CTX_get_verify_mode.argtypes = (ctypes.c_void_p,)
def load_verify_locations(self, cafile, capath, cadata):
print(self)
print(self.verify_mode)
addr = PySSLContext.from_address(id(self)).ctx
libssl.SSL_CTX_set_verify(addr, 1337, None)
print(libssl.SSL_CTX_get_verify_mode(addr))
print(self.verify_mode)
return old_value(self, cafile, capath, cadata)
patchable["load_verify_locations"] = load_verify_locations
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
Run Code Online (Sandbox Code Playgroud)
输出是:
<ssl.SSLContext object at 0x7f4b81304ba8>
2
1337
2
Run Code Online (Sandbox Code Playgroud)
这表明,无论我改变什么,都不是Python知道的ssl上下文,而是其他一些随机内存位置.
要试用上面的代码,您必须运行https服务器.使用以下方法生成自签名SSL证书:
$ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost' -nodes
Run Code Online (Sandbox Code Playgroud)
并使用以下代码启动服务器:
import http.server, http.server
import ssl
httpd = http.server.HTTPServer(('localhost', 4443), http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='cert.pem', keyfile='key.pem', server_side=True)
httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud)
然后将以下行添加到上面示例代码的末尾:
urllib.request.urlopen("https://localhost:4443", context=context)
Run Code Online (Sandbox Code Playgroud)
实际SSLContext答案即将到来,假设不再正确。
请参阅https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations
还有第三个论点,cadata
cadata 对象(如果存在)可以是一个或多个 PEM 编码证书的 ASCII 字符串,也可以是 DER 编码证书的类似字节的对象。
显然自 Python 3.4 以来就是这种情况
这个很简单,ssl.SSLContext继承自_ssl._SSLContextPython数据模型,意味着一个内存地址只有一个对象。
因此,ssl.SSLContext().load_verify_locations(...)实际上会调用:
ctx = \
ssl.SSLContext.__new__(<type ssl.SSLContext>, ...) # which calls
self = _ssl._SSLContext.__new__(<type ssl.SSLContext>, ...) # which calls
<type ssl.SSLContext>->tp_alloc() # which understands inheritance
self->ctx = SSL_CTX_new(...) # _ssl fields
self.set_ciphers(...) # ssl fields
return self
_ssl._SSLContext.load_verify_locations(ctx, ...)`.
Run Code Online (Sandbox Code Playgroud)
C 实现将得到一个看似错误类型的对象,但这没关系,因为所有预期的字段都在那里,因为它是由 generic 分配的,type->tp_alloc并且字段先由 填充_ssl._SSLContext,然后由 填充ssl.SSLContext。
这是一个演示(省略繁琐的细节):
# _parent.c
typedef struct {
PyObject_HEAD
} PyParent;
static PyObject* parent_new(PyTypeObject* type, PyObject* args,
PyObject* kwargs) {
PyParent* self = (PyParent*)type->tp_alloc(type, 0);
printf("Created parent %ld\n", (long)self);
return (PyObject*)self;
}
# child.py
class Child(_parent.Parent):
def foo(self):
print(id(self))
c1 = Child()
print("Created child:", id(c1))
# prints:
Created parent 139990593076080
Created child: 139990593076080
Run Code Online (Sandbox Code Playgroud)
typedef struct {
PyObject_HEAD
SSL_CTX *ctx;
<details skipped>
} PySSLContext;
Run Code Online (Sandbox Code Playgroud)
因此,ctx处于已知偏移处,即:
PyObject_HEAD
This is a macro which expands to the declarations of the fields of the PyObject type; it is used when declaring new types which represent objects without a varying length. The specific fields it expands to depend on the definition of Py_TRACE_REFS. By default, that macro is not defined, and PyObject_HEAD expands to:
Py_ssize_t ob_refcnt;
PyTypeObject *ob_type;
When Py_TRACE_REFS is defined, it expands to:
PyObject *_ob_next, *_ob_prev;
Py_ssize_t ob_refcnt;
PyTypeObject *ob_type;
Run Code Online (Sandbox Code Playgroud)
因此,在生产(非调试)构建中,并考虑自然对齐,PySSLContext变为:
struct {
void*;
void*;
SSL_CTX *ctx;
...
}
Run Code Online (Sandbox Code Playgroud)
所以:
_ctx = _ssl._SSLContext(2)
c_ctx = ctypes.cast(id(_ctx), ctypes.POINTER(ctypes.c_void_p))
c_ctx[:3]
[1, 140486908969728, 94916219331584]
# refcnt, type, C ctx
Run Code Online (Sandbox Code Playgroud)
import ssl
import socket
import ctypes
import pytest
def contact_github(cafile=""):
ctx = ssl.SSLContext()
ctx.verify_mode = ssl.VerifyMode.CERT_REQUIRED
# ctx.load_verify_locations(cafile, "empty", None) done via ctypes
ssl_ctx = ctypes.cast(id(ctx), ctypes.POINTER(ctypes.c_void_p))[2]
cssl = ctypes.CDLL("/usr/lib/x86_64-linux-gnu/libssl.so.1.1")
cssl.SSL_CTX_load_verify_locations.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
assert cssl.SSL_CTX_load_verify_locations(ssl_ctx, cafile.encode("utf-8"), b"empty")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("github.com", 443))
ss = ctx.wrap_socket(s)
ss.send(b"GET / HTTP/1.0\n\n")
print(ss.recv(1024))
def test_wrong_cert():
with pytest.raises(ssl.SSLError):
contact_github(cafile="bad-cert.pem")
def test_correct_cert():
contact_github(cafile="good-cert.pem")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
327 次 |
| 最近记录: |