IO :: Socket Introduction

xiaoxiao2021-03-06  89

[Perl Wen] IO :: Socket Introduction

Writer: Demonalex

Email: Demonalex_at_hackermail.com

Another constructs the Socket library, uses the object constructor. If you have seen WAWA boss's mobile network Exploit and ISNO big brother's WebDavx, you

It will find that these Exploits are doing this library, so if you want to write Exploits, you may want to see this article.

I used to write the traditional C language 'left downholic Socket library, which uses partial C library binary format, causing Perl to make it completely

Use it. The IO :: Socket library introduced in this article is the subclass of IO :: Handle, full object programming, everything will 'free' ...

Using the format and common method (parent class IO :: Handle and IO :: File general method is not described below):

============================================================================================================================================================================================================= ============================================================================================================================================================================================================= =======

-------------------------------------------------- ---------------------------

Import IO :: Socket Package:

Use IO :: Socket;

explain:

IO :: Socket And Two Subscience IO :: Socket :: Inet with IO :: Socket :: UNIX, us

Now it is of course IO :: Socket :: inet.

-------------------------------------------------- ---------------------------

NEW () method:

Socket Object Variable = IO :: Socket :: INET-> New (socket variable);

Example:

$ SOCK = IO :: Socket :: inet-> new ('192.168.1.2:23 ");

explain:

All Perl objects programming objective 'visualization' for a variable, here the Socket handle

Objects are no exception, the return value of this method is called the socket object variable. Use parameters here

Simple parameter mode, Socket address structure in double quotes or but quotation marks is' host IP or domain name: port

Number or service name ', or' host IP or domain name: Service Name (port number) '.

In addition to the simplest single parameter call, there are still many parameters that can be selectively called, under the NEW method.

A simple summary of these parameters:

*********************************************************** ********************

Parameter description value type

± zon zignanom ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± ± p p p 地址 远 地址 地址 地址 地址 地址 地址 地址 地址 地址 地址 地址 地址 地址 地址

Peerhost is the same as peeraddr

PeerPort remote port or service port or service

Localaddd host address [: port or service]

Localhost is the same as Localaddr

LocalPort local port port or service

Proto protocol name or protocol number used by PROTO

TYPE socket type SOCK_STREAM / SOCK_DGRAM ...

Listen listening queue length shaping

Reuse is used to avoid restart time gap boolean

Timeout timeout value

MultiHomed is used to connect multiple IP addresses Boolean

*********************************************************** ********************

Parameter peeraddr (remote host address) is basically the same as Peerhost (remote hostname), the caller

The same is true, and its value format except for the standard format, you can add port or service after adding ':', this

If you like, the value of the back parameter peerport (remote host port or service) is invalid.

Parameter peerport (remote host port or service), its format can be port, can also be

Service name, more can be 'combination', such as "telnet (23)"; when Peeraddr (remote host address) or

The value of this parameter is indicated in the value format of Peerhost, and when this parameter is called, the value of this parameter is called.

invalid.

Parameter Localaddr (local host address), localhost, localport (this

The relationship between the land or the service of the land or service is the three parameters introduced above Peeraddr (far away)

Cheng Host address), Peerhost, Peerport (remote host port or service) is quite.

There is also a situation, that is, if you only define localport (local host port or service), no

Have defined localaddr (local host address) or localhost (local host name), that IO :: Socket

The value of the address of the local machine is defeted as the INADDR_Any wildcard, that is, the local host is not defined.

The address value is defined as allowing all interfaces.

The value of Proto (protocol type) can be represented by two ways. One is a direct string representation party

Forms, such as:

Proto => "TCP"

Indicates that the protocol is TCP. The second way is to use the protocol number, EGP --- 8,

HMP --- 20, ICMP --- 1, RAW --- 255, RDP --- 27, RVD --- 66, TCP --- 6,

UDP --- 17, xns-idp --- 22, other --- 22, all --- 0; you can also use GetProtobyname

The function plus the value named the parameter call, such as:

Proto => getProtobyname ('TCP')

This form also indicates that the type of the protocol is TCP. It is more convenient to recommend using the first way.

Type (socket type) value is usually SOCK_STREAM (streamlined text), SOCK_DGRAM (data

Terrace text, SOCK_RAW, etc., don't say everyone know, TCP is used by flow sets.

Words, UDP uses data settles, constructing the IP package is the original socket.

If the above parameter proto (protocol type) is not defined by the value of Type (socket type),

IO :: Socket :: inet will guess their values ​​through the up and down 'Wen' part of the program, guessing

If you will default, 'TCP'.

The value of the parameter Listen (the length of the listener queue) is a shaping number. It represents the acceptable connection

Number of machines. If you want to construct the server, the Listen step is essential. Call Reuse (settings for SO_REUSEADDR before binding) can be exempted from the server to terminate to restart

The time you stay.

Timeout Calculates in seconds, two steps for connecting Connect and Accept,

The purpose of the call is to limit the suspend time of the connection when connecting the remote host.

The value of MULTIHOMED is a Boolean value when its value is true, if

The host to connect has multiple IP addresses, the NEW method of this machine call gethostByName ()

All IP addresses until you can successfully call.

From the list of upstairs, you can see IO :: Socket with traditional C library Socket API interface in call

What is wrong with it:

1) The control range is different. The interface provided by the C library can only control only domains, sockets that can only be controlled when generating a socket handle.

Word type, these parameters of the protocol. And the IO :: Socket interface creation statement (call new method) is almost

Can determine all parameters of this socket.

2) Call the 'protocol' defined portion of the used portion. IO :: Socket interface calls the parameters in the New method

The value of 'proto' can be directly defined as 'TCP', which is easier than the traditional C library's Socket definition.

3) IO :: Socket can directly define local host addresses, local ports and remote host addresses,

The remote port is in a socket, if this is the server, there is no need to call accept,

You can read and write this Socket directly in the I / O read and write section without defining a remote customer.

End Socket.

-------------------------------------------------- ---------------------------

ACCEPT () method:

Remote connection socket object variable = server socket target variable -> accept ();

Example:

$ transote_sock = $ sock-> acception ();

explain:

The call environment of this method is the same as the principle of the SOCKET library in traditional C, and the waiting to be monitored.

Listening process. None parameters, return the value of the socket object variable for the remote connection. Call this method is also one

Generate a socket process, but this socket is a socket that is a remote connection, it uses an object variable

The manner exists, and it has the same attribute and method as the local socket variable.

ACCEPT () method also provides another double return value for another dual return value in the IO :: Socket package:

(Remote connection socket target variable, remote host compression address variable) = server object variable -> accept ();

Example:

($ Remote_Sock, $ transote_addr) = $ sock-> acception ();

explain:

Basically the same way with a return value upstairs is basically the same, but there is a variable in the return value,

There are more variables in the return value ------ Remote host compressed address variable.

-------------------------------------------------- ---------------------------

Bind () method:

Return value variable = server-side socket target variable -> bind (local port number, local host network address);

Example:

$ Result = $ sock-> bind (80, '127.0.0.1');

explain:

Bind method is used to bind the address and port of the host on the server side. The two parameters it use are

Uncompassed value, the first one is the port, the second is the host's network adapter interface address (you can use the default)

Reserved word inaddr_any, this reserved word includes all network adapter interface addresses of the host, call

When it, it will exhaust all the network adapter interface addresses with exhaustive methods until they find it);

The value is the Boolean value for detecting whether this call is successful. -------------------------------------------------- ---------------------------

CONNECT () method:

Return value variable = socket object variable -> Connect (compressed address variable);

Example:

$ Result = $ sock-> connect ($ pack_addr);

explain:

Commonly used in TCP connections (can also be used for UDP, but not common), call will send a connection to the remote host

request. The parameter 'compressed address variable' is a socketdr_in form value, and the return value is a Boolean value. If

This method does not give parameter 'peeraddr' or 'peerhost' when the IO :: Socket :: inet object is.

'Peerport', otherwise the program logic error will appear.

The connection () method also has a dual-parameter call mode, which is simpler:

Return value variable = socket object variable -> Connect (remote port number, remote host address);

Example:

$ Result = $ sock-> connect ($ remote_port, $ transote_host);

explain:

The purpose of the call is quite equivalent to the call mode of single parameters upstairs. The first parameter is connected to the remote needs.

The host's port (equal to the parameter 'peerport') of the New method, the second parameter is the host that needs to be connected.

Address (equal to the parameter 'peeraddr' or 'peerhost' of the New method), the return value is the Boolean value.

-------------------------------------------------- ---------------------------

Listen () method:

Return value variable = socket object variable -> Listen (the maximum length value of the request queue);

Example:

$ Result = $ sock-> listen (20);

explain:

The TCP server is indispensable. Single parameters, parameters accept remote request queues for this server

The maximum length value, the return value is the Boolean value. Call this method is equivalent to establishing IO :: Socket :: inet

When the object is defined, the value of 'listen' is defined, so if the parameter 'listen' is defined in the New method

The logical error of 'program definition conflict' will appear in this method.

-------------------------------------------------- ---------------------------

SHUTDOWN () method:

Return value variable = socket object variable -> shutdown;

Example:

$ result = $ sock-> shutdown (2);

explain:

This method is a method of closing a socket object outside of Close. Single parameter, parameter value

For additional parameters definition, the external parameter list for this method:

*********************************************************** ********************

Parameter value description

± zon zignanosis ± zon zignanosis

0 Close the read operation of the object socket

1 Close the write operation of the object socket

2 Close all the operations of the object socket

*********************************************************** ********************

Its return value is the Boolean value.

-------------------------------------------------- ---------------------------

Send () method:

Successfully sent data value variable = socket object variable -> Send (send data, flag value, target address value);

Example:

$ succ_bytes = $ sock-> send ('hihi / n', 0, $ pack_host);

explain:

The Send method is special way to send data specifically for Socket, and the format and parameter format are also based.

This is the same as the SEND function in the Socket API of the C library. The first parameter is the data that needs to be sent; second

The parameter is a logo value, the words that are not added is default 0; the third parameter is usually only used for UDP connections, it is necessary to connect

Connected SockAddr_in format address value (Note: When the third parameter is necessary to write, the second party

The number must also be added); the return value is the size of the data value (in byte), which is successful.

-------------------------------------------------- ---------------------------

Recv () method:

Compressed remote address address = socket object variable -> RECV (receive data variable, receive data value length, flag value);

Example:

$ transote_pack_address = $ sock-> recv ($ MEM, 100, 0);

explain:

The RECV method is special way to receive data for Socket, and the call format and parameter format are also

The Socket API of the C library is basically the same. The first parameter is the variable value of the received data after receiving; the second

The parameter is the length value of the received data; the third parameter is a flag value, and the default is 0 or it is possible.

The value is not filled, the system is also 0).

-------------------------------------------------- ---------------------------

============================================================================================================================================================================================================= ============================================================================================================================================================================================================= =======

The common method of the IO :: Socket interface is introduced, but there is still a problem that you need to pay attention:

As a simple client, its steps only need to call the New method first, and then immediately allow basic I / O operation (using Print and getLine

Wait for the basic I / O method), finally simply call the Close method to end the session, the entire Socket session is completed.

Typical use example:

WAWA's DVBBS EXPLOIT: http://haoawa.8866.org/wawa/new/tech/dvbbs.pl

ISNO's WebDavx Exploit: http://www.xfocus.net/tools/200304/webdavx3.pl

What is better to use in the traditional socket interface of the C library? Io :: SOCKET interface? I can only answer you: "Radish green, each has

Love "...: P

转载请注明原文地址:https://www.9cbs.com/read-98306.html

New Post(0)