search
HomeBackend DevelopmentPHP TutorialPHP-SOCKETS preliminary programming_PHP tutorial

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
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)