从业 666 年的 BILIBILI 网络安全工程师 KindMo 最近很困惑,公司有一个业务总是受到 SSRF 攻击。请帮他写一个程序,判断输入的字符串是否属于内网IP,用于防御该漏洞。
我们知道常见的内网IP有,127.0.0.1,192.168.0.1 等。
每次输入仅包含一个IP字符串,即一个测试样例
对于每个测试实例输出整数1或0,1代表True,即输入属于内网IP,0代表False,即输入不属于内网IP或不是IP字符串。
42.96.146.169
0
#include<bits/stdc++.h> using namespace std; int main() { vector<int> res; int num; char c; while(cin>>num>>c) res.push_back(num); cin>>num; res.push_back(num); if(res[0] == 10) cout << 1<< endl; else if(res[0]==172) { if(res[1] >=16 && res[1] <=32) cout << 1 << endl; else cout << 0 << endl; } else if(res[0] == 192) { if(res[1] == 168) cout << 1 << endl; else cout << 0 << endl; } else cout << 0 << endl; return 0; }
if input().split('.')[0] in ['127','192','10','172']: print(1) else: print(0)
if input()[:3] in ('127','192','10.','172'): print(1) else: print(0)
#include <iostream> #include <cstdio> #define INIT() ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); using namespace std; int main() { INIT(); string ip; cin >> ip; int n = ip.size(); int ip_num = 0; bool is_net = false; for(int i = 0; i < n; i++) { if(ip[i] == '.') { if(ip_num == 10 || ip_num == 127 || ip_num==17216 || ip_num ==192168 || ip_num==17231) { is_net = true; break; } } else { ip_num = ip_num * 10 + (ip[i] - '0'); } } if(is_net) cout << "1" << endl; else cout << "0" << endl; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] ss = s.split("\\."); int a = Integer.parseInt(ss[0]); int b = Integer.parseInt(ss[1]); if(a==10 || (a==172 && (b>=16 && b<32)) || (a==192 && b==168)) System.out.println(1); else System.out.println(0); } }
#include<iostream>
#include<string>
using namespace std;
int main(){
int a,b,c,d;
scanf("%d.%d.%d.%d",&a,&b,&c,&d); if((a==10||a==127)&&(b>=0&&b<=255)&&(c>=0&&c<=255)&&(d>=0&&d<=255))
cout<<1<<endl;
else if(a==172&&(b>=16&&b<=31)&&(c>=0&&c<=255)&&(d>=0&&d<=255))
cout<<1<<endl;
else if(a==192&&b==168&&(c>=0&&c<=255)&&(d>=0&&d<=255))
cout<<1<<endl;
else
cout<<0<<endl;
return 0;
}
def ipTest(the_ip): if the_ip == "127.0.0.1": return 1 elif the_ip.split('.')[0] == "10": return 1 elif the_ip.split('.')[0] == "192" and the_ip.split('.')[1] == "168": return 1 elif the_ip.split('.')[0] == "172" and (16 <= int(the_ip.split('.')[1]) <= 31): return 1 else: return 0 ip_addr = input() print(ipTest(ip_addr))
// 私有ip分为三类 // 1. 10.0.0.0 - 10.255.255.255 // 2. 172.16.0.0 - 172.31.25.255 // 3. 192.168.0.0 - 192.168.255.255 var arr=readline().split('.').map(Number) // 判断是否存在大于255的数 if(arr[2]>255&&arr[3]>255){ console.log(0) } // 如果不属于 10,172, 192则直接over if(arr[0]!=10&&arr[0]!=172&&arr[0]!=192){ console.log(0); }else if(arr[0]==172){ if(arr[1]!=16&&arr[1]!=31){ console.log(0) }else{ console.log(1) } }else if(arr[0]==10){ console.log(1) }else if(arr[0]==192&&arr[1]==168){ console.log(1) }else{ console.log(0) }
私有IP地址范围: A类:10.0.0.0-10.255.255.255 B类:172.16.0.0-172.31.255.255 C类:192.168.0.0-192.168.255.255 localhost:127.0.0.1
# -*- coding:utf-8 -*- def check_internal_ip(check_ip): check_ip_list = check_ip.split(".") if len(check_ip_list) == 4: a, b, c, d = check_ip_list a = int(a) b = int(b) c = int(c) d = int(d) if a > 255 or b > 255 or c > 255 or d > 255: return 0 elif a == 10 and b >= 0 and c >= 0 and d >= 0: return 1 elif a == 172 and (16 <= b <= 31) and c >= 0 and d >= 0: return 1 elif a == 192 and b == 168 and c >= 0 and d >= 0: return 1 else: return 0 else: return 0 check_ip = input() print(check_internal_ip(check_ip))
function (readline()){ var str=readline.split('.'); if(str[0]==10&&0<=str[1]<=255&&0<=str[2]<=255&&0<=str[3]<=255){ return 1 }else if(str[0]==172&&16<=str[1]<=31&&0<=str[2]<=255&&0<=str[3]<=255){ return 1 }else if(str[0]==192&&str[1]===168&&0<=str[2]<=255&&0<=str[3]<=255){ return 1 }else{ return 0 } }本地检测应该时没有问题的,但是不知道这个readline是怎么个输入法。。
#include <iostream> using namespace std; int main(){ unsigned a,b,c,d; char temp; cin >> a >> temp >> b >> temp >> c >> temp >> d; unsigned ip = (a<<24) | (b<<16) | (c<<8) | d; static unsigned mask[3] = {0x0a000000,0xac100000,0xc0a80000}; for(int i=0; i<3; i++){ if((ip&mask[i])==mask[i]){ cout << 1 << endl; return 0; } } cout << 0 << endl; return 0; }// A类地址:10.0.0.0--10.255.255.255
#include<iostream> #include<vector> #include<sstream> using namespace std; int main() { string str;cin >> str; stringstream stream(str); vector<string> res; string tem; while(getline(stream, tem, '.')) res.push_back(tem); if(res[0] == "10") cout << 1<< endl; else if(res[0]=="172") { if(res[1] >="16" && res[1] <="32") cout << 1 << endl; else cout << 0 << endl; } else if(res[0] == "192") { if(res[1] == "168") cout << 1 << endl; else cout << 0 << endl; } else cout << 0 << endl; return 0; }