Recently, decompilation looks at some android applications, especially those that involve underlying functions, such as game acceleration, memory modification, and host-mounted scripts. It is found that all communication mechanisms in these applications use sockets, it has basically become a standard for such applications.
Because such applications have the following things in common:
1. You need the root permission of the android phone. After all, you need to modify some underlying things. Sometimes you cannot modify the root permission.
2. Have your own so, and compile the important or difficult jobs into an executable elf file, and then let the apk Application Start the elf File. There are also several options for the location of the elf File.
Scenario 1: directly store the file in the lib directory under the application package and grant the executable permission to the executable file.
Case 2: Put it in the assets folder, copy it to the files folder when the program starts, grant the executable permission, and then execute
Case 3: it is also placed under the assets folder, but the security is a little higher. Generally, a simple XOR or shift encryption is implemented, and the following extension is modified to reduce the password when copying the file. Then, sometimes the real elf file will be deleted after the execution.
3. socket is used for communication between executable so files and java applications. After all, executable so files are the main force, while java applications need to send instructions to so, so also needs to provide some useful data to the java application for the upper layer to display to the user. There are also the following situations about socket communication methods:
Scenario 1: the executable so serves as the server. When the java application sends a command to it, it actively links to the executable so to send data. Sometimes, so also returns some data, in this case, so directly performs the task, such as slowing down the game.
Scenario 2: Execute the so file as the client and use a persistent link. In this case, the java application is used as the server, A little clever here is that the socket port number is not fixed, but the port number automatically allocated by the system when the java application creates the socket. However, you must use a java application to pass the port number to the so file when the so file can be started.
The principle is basically so much. The following is an example. The programming technology involved in this example is mainly for java applications to create their own socket clients and servers, and then communicate with each other, at the same time, an so executable file is started to send data to the executable so through the data stream between processes. The executable so file prints the received data through the log.
Enter the demo,
Click Start socket server
Logs are printed at this time.
11-04 15:56:28. 341: V/cheatecore-server (5774): server port = 44034
Such data,
Since serverSocket = new ServerSocket (m_port );
M_port is 0, so the port number is temporarily allocated by the system.
Then the socketserver starts to wait for a client socket to connect.
M_socket = serverSocket. accept ();
The accept function blocks the thread. This time
Use the adb shell to enter the android phone and use netstat-an | busybox grep 44034.
650) this. width = 650; "src ="/e/u/themes/default/images/spacer.gif "style =" border: 1px solid # ddd; "alt =" spacer.gif "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/121605Ba-0.jpg "title =" qq 31104162412.jpg "alt =" 162506902.jpg"/>
Obviously, this port is in the listen status.
Then click Set client socket port number and start client socket
11-04 16:03:11. 067: V/cheatecore (5774): Get the server port number: 44034
11-04 16:03:15. 575: V/cheatecore-client (5774): client socket begin
11-04 16:03:15. 575: V/cheatecore-server (5774): a client socket come here !!!
A client socket comehere is displayed at this time !!!
This log is executed again.
Netstat-an | busybox grep 44034
The result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12160549A-1.png "title =" 33433.png" alt = "162556762.png"/>
A link has been established between the randomly assigned client port number and the server.
Then click get data stream
Reader = new BufferedReader (inputreader );
Serveroutwriter = new PrintWriter (newBufferedWriter (new OutputStreamWriter (sk. getOutputStream (), true );
It mainly obtains the input stream and output stream of the socket, that is, the tcp link between the client and the server has been established, and the rest is to receive and send data through the stream.
Click
Start the server socket to receive data thread
String sReader = reader. readLine ();
This statement is waiting for the socket data of the client to come over. readLine is a blocking function. If you are familiar with the C language, this is actually the recv function in the socket, blocking the call.
Because tcp is full-duplex and the current status is persistent, the server can send data to the client, the client can also send data to the server, and the next button
Server socket sends data
11-04 16:11:19. 810: V/cheatecore-client (5774): the client receives data from the server: server data
11-04 16:11:19. 810: V/cheatecore-server (5774): the server receives client data: clientdata
When the log appears at this time, the client will return data to the server after receiving the server data. This completes an interaction, but the socket is not broken at this time, because it is a persistent link, it can be sent multiple times
End socket
Disable the socket, and then use netstat-an | busybox grep 44034.
This command is used to query and found that:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1216053500-2.png "title =" 55666565.png" alt = "162636442.png"/>
The socket created just now has been released, and the port will be released after the wait query times out.
Test sending data to the started Process
11-04 16:15:11. 278: E/cheatecore (6163): input target main function !!!!
11-04 16:15:11. 278: E/cheatecore (6163): scanf input content is 44034
The main process is to start
/Data/com. example. sockettest/files/target
This program
Testprocee implements runtime.getruntime(cmd.exe c (cmd );
PrintWriterpoutwriter =NewPrintWriter (NewBufferedWriter (NewOutputStreamWriter (testprocee. getOutputStream ())),True);
Poutwriter. println (test );
Then, the data is sent to complete the data interaction between processes. In fact, this is also a way of communication between processes, and the use of socket to complete the communication between processes.
For example, see the following