Create SSH user that can only update iptables

I have closed all the ports on my server. I only allow access to port 80 and 443 for Cloudflare ip-addresses. I’ve added the ip-ranges of Cloudflare to iptables in Ubuntu 22.04 to achieve this.

Now I would like to make an automation that checks every night at 00:05 if the ip-ranges of Cloudflare have been changed. If so I would like to login with SSH delete all the ip-ranges in iptables and import the new ip-ranges of Cloudflare.
Therefore I’ve made the PHP script below, that is executed every night with a cronjob.

For this action I would like to create an SSH user named “iptables” which rights are limited to the following 3 actions. So the SSH user shouldn’t have any other rights out of security reasons. SSH is already restricted by ip-address.

  • sudo iptables -F
  • iptables -A INPUT -p tcp -m multiport –dports http,https -j DROP
  • iptables -I INPUT -p tcp -m multiport –dports http,https -s $i -j ACCEPT; done

If this is not possible I would like to limit the rights of the user to only iptables.
How can I achieve this and is this possible?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$url = 'https://www.cloudflare.com/ips-v4';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);
$date_modified = curl_getinfo($curl);
curl_close($curl);
$date_modified = date ("d-m-Y", $date_modified['filetime']);
$date_yesterday = date('d-m-Y',strtotime("-1 days"));
//IF MODIFIED DATE IS EQUAL TO YESTERDAYS DATE UPDATE IP4 IPTABLES
if ($date_modified == $date_yesterday ){
// LOGIN WITH SSH
$connection = ssh2_connect($ssh_host, 22);
// LOGIN SUCCESFULL
if(ssh2_auth_password($connection, $ssh_user, $ssh_password)){
// DELETE ALL IP4 IPTABLES RULES WITH FLUSH
$stream = ssh2_exec($connection, 'sudo iptables -F');
// ACCEPT THE CURRENT IP4 RANGE FROM CLOUDFLARE FOR PORT 80 AND 443
$stream = ssh2_exec($connection, 'for i in `curl https://www.cloudflare.com/ips-v4`; do iptables -I INPUT -p tcp -m multiport --dports http,https -s $i -j ACCEPT; done');
// DENY ALL OTHER IP4 ADDRESSES
$stream = ssh2_exec($connection, 'iptables -A INPUT -p tcp -m multiport --dports http,https -j DROP');
// LOGOUT FROM SSH
ssh2_exec($connection, 'exit');
ssh2_disconnect();
// FLUSH $connection
$connection = null;
unset($connection);
}
// LOGIN FAILED
else{
$conn_error=1;
}
} ```
</code>
<code>$url = 'https://www.cloudflare.com/ips-v4'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FILETIME, true); $result = curl_exec($curl); $date_modified = curl_getinfo($curl); curl_close($curl); $date_modified = date ("d-m-Y", $date_modified['filetime']); $date_yesterday = date('d-m-Y',strtotime("-1 days")); //IF MODIFIED DATE IS EQUAL TO YESTERDAYS DATE UPDATE IP4 IPTABLES if ($date_modified == $date_yesterday ){ // LOGIN WITH SSH $connection = ssh2_connect($ssh_host, 22); // LOGIN SUCCESFULL if(ssh2_auth_password($connection, $ssh_user, $ssh_password)){ // DELETE ALL IP4 IPTABLES RULES WITH FLUSH $stream = ssh2_exec($connection, 'sudo iptables -F'); // ACCEPT THE CURRENT IP4 RANGE FROM CLOUDFLARE FOR PORT 80 AND 443 $stream = ssh2_exec($connection, 'for i in `curl https://www.cloudflare.com/ips-v4`; do iptables -I INPUT -p tcp -m multiport --dports http,https -s $i -j ACCEPT; done'); // DENY ALL OTHER IP4 ADDRESSES $stream = ssh2_exec($connection, 'iptables -A INPUT -p tcp -m multiport --dports http,https -j DROP'); // LOGOUT FROM SSH ssh2_exec($connection, 'exit'); ssh2_disconnect(); // FLUSH $connection $connection = null; unset($connection); } // LOGIN FAILED else{ $conn_error=1; } } ``` </code>
$url = 'https://www.cloudflare.com/ips-v4';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);
$date_modified = curl_getinfo($curl);
curl_close($curl);

$date_modified = date ("d-m-Y", $date_modified['filetime']);
$date_yesterday = date('d-m-Y',strtotime("-1 days"));

//IF MODIFIED DATE IS EQUAL TO YESTERDAYS DATE UPDATE IP4 IPTABLES
if ($date_modified == $date_yesterday ){
    
// LOGIN WITH SSH   
$connection = ssh2_connect($ssh_host, 22);

// LOGIN SUCCESFULL
if(ssh2_auth_password($connection, $ssh_user, $ssh_password)){
    
// DELETE ALL IP4 IPTABLES RULES WITH FLUSH
$stream = ssh2_exec($connection, 'sudo iptables -F');
    
// ACCEPT THE CURRENT IP4 RANGE FROM CLOUDFLARE FOR PORT 80 AND 443 
$stream = ssh2_exec($connection, 'for i in `curl https://www.cloudflare.com/ips-v4`; do iptables -I INPUT -p tcp -m multiport --dports http,https -s $i -j ACCEPT; done');
    
// DENY ALL OTHER IP4 ADDRESSES 
$stream = ssh2_exec($connection, 'iptables -A INPUT -p tcp -m multiport --dports http,https -j DROP');
    
// LOGOUT FROM SSH
ssh2_exec($connection, 'exit');
ssh2_disconnect();

// FLUSH $connection
$connection = null;
unset($connection);
    
}

// LOGIN FAILED
else{
    $conn_error=1;
}

} ```

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật