des4maisons
des4maisons

Reputation: 1881

How to apply() the send function to a socket object?

EDIT: I am using the deprecated apply() for pickle exploit, for a security competition. apply seems to be the way you can call methods using a pickle instruction string. I would be happy to learn of a better way, however.

I'm trying to use apply() to call the send() method on a socket._socketobject, but I'm getting a strange error message.

In [130]: client
Out[130]: <socket._socketobject at 0x108d0bfa0>

In [131]: apply(socket._socketobject.send, (client, "stuff"))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-131-3f9b1f1de824> in <module>()
----> 1 apply(socket._socketobject.send, (client, "stuff"))

TypeError: 'member_descriptor' object is not callable

Interestingly, send() is of type function, and lets me call it:

In [128]: client.send
Out[128]: <function send>

In [129]: client.send("stuff")
Out[129]: 5

This is how I originally got my socket object named client:

In [107]: import signal

In [108]: sock = socket.socket()

In [109]: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

In [110]: sock.bind(('0.0.0.0', 1025))

In [111]: sock.listen(10)

In [112]: signal.signal(signal.SIGCHLD, signal.SIG_IGN)
Out[112]: 0
In [113]: client, addr = sock.accept()

Any help is appreciated!

Upvotes: 0

Views: 869

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798764

Interestingly, send() is of type function

Incorrect. send returns a function from its __get__() method, since it's a descriptor. You will need to interface with the descriptor protocol yourself if you want to get the actual function to call.

>>> dir(socket._socketobject.send)
['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__name__', '__new__', '__objclass__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> type(socket._socketobject.send)
<type 'member_descriptor'>

Upvotes: 2

Related Questions