Perl Salt Generation on wp-config-sample.php

2019-09-01 08:52发布

I've done a lot of research to find a way to do this, but I'm coming up short. I'm looking for a way to replace all of the 'put your unique phrase here' with randomly generated salts.

Here is the section of the config I need to edit:

define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

This question (here) is close to what I want but expects the standard wp-config-sample.php to be edited. I don't know if it would be easier to delete that whole section and replace it, or just do some sort of find and replace on each line.


UPDATE:

Here is a code sample form my current script (and the attempt at incorporating the answer below..).

sub modify_configs {
    my $shell   = $_[0];
    my $dbname  = $_[1];
    my $dbuser  = $_[2];
    my $siteurl = $_[3];

    # modify wp-config.php
    print BOLD "Modifying configuration files...\n";
    chdir($local_dir);
    system('perl -pi -e "s/database_name_here/'.$dbname.'/g" wp-config-sample.php');
    system('perl -pi -e "s/username_here/'.$dbuser.'/g" wp-config-sample.php');
    system('perl -pi -e "s/password_here/'.$pass.'/g" wp-config-sample.php');
    my ($keysalts, $flipflop);
    `perl -ne 'BEGIN { $keysalts = qx(curl -sS https://api.wordpress.org/secret-key/1.1/salt) } if ( $flipflop = ( m/AUTH_KEY/ .. m/NONCE_SALT/ ) ) {if ( $flipflop =~ /E0$/ ) { printf qq|%s|, $keysalts; } next; } printf qq|%s|, $_;' wp-config-sample.php`;
    # rename('wp-config-sample.php', 'wp-config.php');
    ...
}

This fails with:

Use of uninitialized value $keysalts in concatenation (.) or string at ./gen.pl line 101, <MYFILE> line 3.
Use of uninitialized value $flipflop in concatenation (.) or string at ./gen.pl line 101, <MYFILE> line 3.
Use of uninitialized value $flipflop in concatenation (.) or string at ./gen.pl line 101, <MYFILE> line 3.
Use of uninitialized value $keysalts in concatenation (.) or string at ./gen.pl line 101, <MYFILE> line 3.
syntax error at -e line 1, near "{  ="
BEGIN not safe after errors--compilation aborted at -e line 1.

Any help here would be appreciated!

Thanks.

1条回答
趁早两清
2楼-- · 2019-09-01 09:25

Try:

perl -ne '
    BEGIN { 
        $keysalts = qx(curl -sS https://api.wordpress.org/secret-key/1.1/salt) 
    } 
    if ( $flipflop = ( m/AUTH_KEY/ .. m/NONCE_SALT/ ) ) {
        if ( $flipflop =~ /E0$/ ) {
            printf qq|%s|, $keysalts;
        }
        next;
    }
    printf qq|%s|, $_;
' wp-config-sample.php

It get salts using curl as an external command and then parses the input file line by line using a flip-flop to replace the section of defines with those with random salts.

It yields (part of output):

...
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'CQVDYWT|G!9/ZXd>#}/MB>n?`enZrpx#h,,4/=ev/@4|>H(0vkDDd~Pp!+R-#!4-');
define('SECURE_AUTH_KEY',  'peCpNn5f6c=T$%]@=Lx}C_aQhp;.%aTgrrAyQE 1476!<e%5Ys)0Zab OZtAM7?V');
define('LOGGED_IN_KEY',    '.}$YwXKsx|c(k(xZ.Tk{eGMt]#cOM8g,7Z?H{UKQzK1KN8rsIVZuddkW21E{(+q$');
define('NONCE_KEY',        'h]>4cN[U+O>xcQ;H#C+2ZvmOK4oC)TY[|5e(EsT,n+~+aOd+|~e/PN5%N$:7c90/');
define('AUTH_SALT',        '1U@V&Fp-v&epA,lzws>GrB]MuG.6[Wdnb:n;FY8Kqn~`1~4_} 3D)o6PzGBeIm-)');
define('SECURE_AUTH_SALT', '2!.I5D<JCo+4UIh>h~3PM?;.]$67;y-t3jp;PzKBNjd(!*6&>6gteX$@fGp_-Q;N');
define('LOGGED_IN_SALT',   '%>|}J%O]W><VWQ#x:BAfMPT:2O|a|VeQE0vK:<.dCW5WW9U3=-1fm]{eg3O~N*al');
define('NONCE_SALT',       '-G:lv+cuSpClI!.^W[)WfcFN.#HSv 7bA:W*<+A^%<ApQ7DmC?A[uTu+jk0~%:of');

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';
...
查看更多
登录 后发表回答