Home  >  Article  >  Backend Development  >  PHP-SOCKETS preliminary programming_PHP tutorial

PHP-SOCKETS preliminary programming_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:19:31772browse

Author: darkness[bst]

msn:cqxy[at]21cn.net

I have learned PHP for 2 months and gained a lot. But unlike others, I prefer socket.php There are too few articles on sockets. So I decided to write a series of php-socket reading notes. From the most basic to socket_raw.
Examples + experiences. The examples will include port forwarding (breaking through firewalls), dynamic network types exp, port scanning, php backdoor, outsourcing exp framework. Due to study reasons, I can only write one article per week. Volume 1 is given now. I hope everyone can invest in php shell programming.


Foreword:

php is one of the most popular scripting languages ​​in the world. It has been widely used in web programming. What I want to say is that php is not only excellent in web, but also excellent in shell. It's just that people are more accustomed to using perl to write shell scripts. I would like to state here that I am not a PHP expert and have only been exposed to PHP for a few weeks. This is just a reading note. Please point out any errors. You can also send me an email and discuss php together.

Pre-requisite knowledge:

What attracts me most about PHP is the sockets extension. In fact, I can simply use VB winsock, and I can write a commonly used winsock program using VB. But I still chose php. Because it is cross-platform.

php does not support advanced sockets by default, and only supports several functions such as "encapsulated" fsockopen. As an extension of php, socket needs to be set up to support it. In windows you need to set up php. ini, in php. ini, find the line "windows extensions" and remove the semicolon in front of "extension=php_sockets.dll". that’s ok. Under *nix, you need to add the -enable-sockets command when compiling. When not using the dl() function, your php must be in the same directory as php_sockets.dll. Ok, the php socket configuration is completed.

The following is the problem of running.

It is very simple to run the php script in the terminal. Under windows, c:phpphp.exe ╟q test.php, under *nix, the php file must be declared in advance to be executed by php, just like perl. Like #!/usr/local/bin/php ╟q ., and then ./test.php. Parameter q means not to output php header information.

Input parameter problem:

Some people say how to input parameters in php shell. On the web, you can enter parameters like this http://xxx.com/aa.php?Parameter 1=xxxx&Parameter 2=ssssss. It doesn't matter that php is the same as perl and has similar parameter functions. Let’s look at the official description

“argv”

The parameters passed to the script. When the script is run in command-line mode, the argv variable is passed to the program as C-style command-line arguments. When the get method is called, this variable contains the requested data.

"argc"

Contains the number of command line arguments passed to the program (if running in command line mode).



Haha, to put it simply. Let me give you an example



The following is the code:

[ctrl+a select all]


I think you should understand, here argc[0] refers to the program itself . You can also do it like this.

print(%s,$argv[1]);
 

I spent an hour at the Internet cafe at noon to write this short paragraph


The previous paragraph talks about running in command line mode. For more information, please refer to
http://www.php.net/manual/zh/features.commandline.php


1.fopen application
fopen It can also be called an encapsulated socket function. Not only used for file reading and writing, but also for sockets. fopen is equivalent to the inet control/class of other high-level languages. Compared with fsockopen, it has more advanced operations on URLs.

How to use fopen
$s = fopen ($url, mode);
The mode attribute of fopen:
mode Description
r Open in read-only mode, point the file pointer to File header.
r+ opens in read-write mode and points the file pointer to the file header.
w opens in writing mode, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try to create it.
w+ opens in read-write mode, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try to create it.
a opens in writing mode and points the file pointer to the end of the file. If the file does not exist, try to create it.
a+ opens in read-write mode and points the file pointer to the end of the file. If the file does not exist, try to create it.
x is created and opened for writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns false and generates an e_warning level error message. If the file does not exist, try to create it. This is equivalent to specifying the o_excl|o_creat flag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later, and can only be used for local files.
x+ creates and opens it for reading and writing, pointing the file pointer to the file header.If the file already exists, the fopen() call fails and returns false and generates an e_warning level error message. If the file does not exist, try to create it. This is equivalent to specifying the o_excl|o_creat flag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later, and can only be used for local files.


is used for local file operations and can also be used for inet. Isn’t it very cool?
Suppose you want to test whether the iis directory of a website has write permission.
You can write like this
$s = fopen("http://www.bugkidz.org","x+") or die("No write permission exists ”)
If it exists, you can continue to construct the following statement. Use fwrite to write files remotely.
But general websites have read-only permissions
$s =fopen("http://www.bugkidz.org/index.php?id=1","r" );
In this way, the content of http://www.bugkidz.org/index.php?id=1 is read, but it must be processed to obtain the complete file content
This way
while (!feof($s)) {
echo fgets($s, 1024);
}
I think fopen is the most convenient for sql injection.
function phpinet($url)
{
fopen($url,"r") or die("Error opening url");


while (!feof($ s)) {
$cahe = fgets($s, 1024);
}

retrun $cahe;
fclose($s)
}


This function is equivalent to inet.openurl in VB
Usage of fsockopen family functions
fsockopen is also an encapsulated socket function. It is somewhat similar to the winsock control in VB. Regrettably, it supports active socket connection and does not support bind, listen, etc. If you need to implement these functions, you must use advanced socket programming in PHP. Even so, the fsockopen function can meet most needs.
Use fsockopen like this
resource fsockopen ( string target, int port [, int errno [, string errstr [, float timeout]]])

Example:
$sock = fsockopen(" 192.168.0.1",80,$errno,$errstr,30);
The first two are the address and port, the middle two are error-related variables, and the last is the timeout setting.
Usually $sock = fsockopen("192.168.0.1",80); This is enough.
$sock = fsockopen("192.168.0.1",80); This is a typical TCP connection. The UDP connection is like this
$sock = fsockopen("udp://192.168.0.1",53);
It is also possible to use this to write a TFTP client.

Fsockopen application examples:

Example 1, simple HTTP session.

Code


The following is the code:

[ctrl+a select all]

The process is generally like this
Create the fsockopen resource, define the sending content, and use the fwrite function or fputs function Write the definition content and output the obtained content line by line until the end of the file is reached. Use the fgets function or fread. Use fclose to close the created fsockopen resource.
ANGEL has written a PHP port scanning tool and posted it
http://www.4ngel.net/article/20.htm

Select fsockopen to write The simple EXP sending framework is definitely a good idea. becozitssoeasy.
Look at my PHP upload vulnerability exploit.

Code


The following is the code:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532676.htmlTechArticleAuthor: darkness[bst] msn:cqxy[at]21cn.net I have been learning PHP for 2 months and have gained a lot. .But unlike others, I prefer socket.php. There are too few articles on sockets. So I decided to write a series...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn