Home Backend Development PHP Tutorial PHP-SOCKETS preliminary programming_PHP tutorial

PHP-SOCKETS preliminary programming_PHP tutorial

Jul 13, 2016 pm 05:19 PM
msn author study programming

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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Remove duplicate values ​​from PHP array using regular expressions Remove duplicate values ​​from PHP array using regular expressions Apr 26, 2024 pm 04:33 PM

How to remove duplicate values ​​from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Build browser-based applications with Golang Build browser-based applications with Golang Apr 08, 2024 am 09:24 AM

Build browser-based applications with Golang Golang combines with JavaScript to build dynamic front-end experiences. Install Golang: Visit https://golang.org/doc/install. Set up a Golang project: Create a file called main.go. Using GorillaWebToolkit: Add GorillaWebToolkit code to handle HTTP requests. Create HTML template: Create index.html in the templates subdirectory, which is the main template.

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles