How to secure phpMyAdmin

后端 未结 9 573
清歌不尽
清歌不尽 2020-12-02 04:40

I have noticed that there are strange requests to my website trying to find phpmyadmin, like

/phpmyadmin/
/pma/

etc.

Now I have ins

相关标签:
9条回答
  • 2020-12-02 04:48

    If you are running a linux server:

    • Using SSH you can forbid the user/password loging and only accept a public key in the authorized_keys file
    • Use putty to connect to your server and open a remote terminal
    • Forward X11 and brings localhost firefox/iceweasel to your desktop (in windows you need Xming software installed)
    • Now you secured your phpMyAdmin throught ssh

    This system is quite secure/handy for homeservers -usually with all ports blocked by default-. You only have to forward the SSH port (don't use number 22).

    If you like Microsoft Terminal Server you can even set a SSH Tunneling to your computer and connect securely to your web server throught it.

    With ssh tunneling you even can forward the 3306 port of your remote server to a local port and connect using local phpMyAdmin or MySQL Workbench.

    I understand that this option is an overkill, but is as secure as the access of your private key.

    0 讨论(0)
  • 2020-12-02 04:48

    The simplest approach would be to edit the webserver, most likely an Apache2 installation, configuration and give phpmyadmin a different name.

    A second approach would be to limit the IP addresses from where phpmyadmin may be accessed (e.g. only local lan or localhost).

    0 讨论(0)
  • 2020-12-02 04:51

    In newer versions of phpMyAdmin access permissions for user-names + ip-addresses can be set up inside the phpMyAdmin's config.inc.php file. This is a much better and more robust method of restricting access (over hard-coding URLs and IP addresses into Apache's httpd.conf).

    Here is a full example of how to switch to white-listing all users (no one outside this list will be allowed access), and also how to restrict user root to the local system and network only.

    $cfg['Servers'][$i]['AllowDeny']['order'] = 'deny,allow';
    $cfg['Servers'][$i]['AllowDeny']['rules'] = array(
        'deny % from all', // deny everyone by default, then -
    
        'allow % from 127.0.0.1', // allow all local users
        'allow % from ::1',
    
        //'allow % from SERVER_ADDRESS', // allow all from server IP
    
        // allow user:root access from these locations (local network)
        'allow root from localhost',
        'allow root from 127.0.0.1',
        'allow root from 10.0.0.0/8',
        'allow root from 172.16.0.0/12',
        'allow root from 192.168.0.0/16',
    
        'allow root from ::1',
    
        // add more usernames and their IP (or IP ranges) here -    
        );
    

    Source: How to Install and Secure phpMyAdmin on localhost for Windows

    This gives you much more fine-grained access restrictions than Apache's URL permissions or an .htaccess file can provide, at the MySQL user name level.

    Make sure that the user you are login in with, has its MySQL Host: field set to 127.0.0.1 or ::1, as phpMyAdmin and MySQL are on the same system.

    0 讨论(0)
  • 2020-12-02 05:00

    Most likely, somewhere on your webserver will be an Alias directive like this;

    Alias /phpmyadmin "c:/wamp/apps/phpmyadmin3.1.3.1/"
    

    In my wampserver / localhost setup, it was in c:/wamp/alias/phpmyadmin.conf.

    Just change the alias directive and you should be good to go.

    0 讨论(0)
  • 2020-12-02 05:01

    One of my concerns with phpMyAdmin was that by default, all MySQL users can access the db. If DB's root password is compromised, someone can wreck havoc on the db. I wanted to find a way to avoid that by restricting which MySQL user can login to phpMyAdmin.

    I have found using AllowDeny configuration in PhpMyAdmin to be very useful. http://wiki.phpmyadmin.net/pma/Config#AllowDeny_.28rules.29

    AllowDeny lets you configure access to phpMyAdmin in a similar way to Apache. If you set the 'order' to explicit, it will only grant access to users defined in 'rules' section. In the rules, section you restrict MySql users who can access use the phpMyAdmin.

    $cfg['Servers'][$i]['AllowDeny']['order'] = 'explicit'
    $cfg['Servers'][$i]['AllowDeny']['rules'] = array('pma-user from all')
    

    Now you have limited access to the user named pma-user in MySQL, you can grant limited privilege to that user.

    grant select on db_name.some_table to 'pma-user'@'app-server'
    
    0 讨论(0)
  • 2020-12-02 05:01

    The best way to secure phpMyAdmin is the combination of all these 4:

    1. Change phpMyAdmin URL
    2. Restrict access to localhost only.
    3. Connect through SSH and tunnel connection to a local port on your computer
    4. Setup SSL to already encrypted SSH connection. (x2 security)
    

    Here is how to do these all with: Ubuntu 16.4 + Apache 2 Setup Windows computer + PuTTY to connect and tunnel the SSH connection to a local port:

    # Secure Web Serving of phpMyAdmin (change URL of phpMyAdmin):
    
        sudo nano /etc/apache2/conf-available/phpmyadmin.conf
                /etc/phpmyadmin/apache.conf
            Change: phpmyadmin URL by this line:
                Alias /newphpmyadminname /usr/share/phpmyadmin
            Add: AllowOverride All
                <Directory /usr/share/phpmyadmin>
                    Options FollowSymLinks
                    DirectoryIndex index.php
                    AllowOverride Limit
                    ...
            sudo systemctl restart apache2
            sudo nano /usr/share/phpmyadmin/.htaccess
                deny from all
                allow from 127.0.0.1
    
            alias phpmyadmin="sudo nano /usr/share/phpmyadmin/.htaccess"
            alias myip="echo ${SSH_CONNECTION%% *}"
    
    # Secure Web Access to phpMyAdmin:
    
            Make sure pma.yourdomain.com is added to Let's Encrypt SSL configuration:
                https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-16-04
    
            PuTTY => Source Port (local): <local_free_port> - Destination: 127.0.0.1:443 (OR localhost:443) - Local, Auto - Add
    
            C:\Windows\System32\drivers\etc
                Notepad - Run As Administrator - open: hosts
                    127.0.0.1 pma.yourdomain.com
    
            https://pma.yourdomain.com:<local_free_port>/newphpmyadminname/ (HTTPS OK, SSL VPN OK)
            https://localhost:<local_free_port>/newphpmyadminname/ (HTTPS ERROR, SSL VPN OK)
    
            # Check to make sure you are on SSH Tunnel
                1. Windows - CMD:
                    ping pma.yourdomain.com
                    ping www.yourdomain.com
    
                    # See PuTTY ports:
                    netstat -ano |find /i "listening"
    
                2. Test live:
                    https://pma.yourdomain.com:<local_free_port>/newphpmyadminname/
    

    If you are able to do these all successfully,

    you now have your own url path for phpmyadmin,
    you denied all access to phpmyadmin except localhost,
    you connected to your server with SSH,
    you tunneled that connection to a port locally,
    you connected to phpmyadmin as if you are on your server,
    you have additional SSL conenction (HTTPS) to phpmyadmin in case something leaks or breaks.
    
    0 讨论(0)
提交回复
热议问题