首页 > 试题广场 >

写一段程序判断IP字符串是否属于内网IP

[编程题]写一段程序判断IP字符串是否属于内网IP
  • 热度指数:1981 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
从业 666 年的 BILIBILI 网络安全工程师 KindMo 最近很困惑,公司有一个业务总是受到 SSRF 攻击。请帮他写一个程序,判断输入的字符串是否属于内网IP,用于防御该漏洞。
我们知道常见的内网IP有,127.0.0.1,192.168.0.1 等。

输入描述:
每次输入仅包含一个IP字符串,即一个测试样例


输出描述:
对于每个测试实例输出整数1或0,1代表True,即输入属于内网IP,0代表False,即输入不属于内网IP或不是IP字符串。
示例1

输入

42.96.146.169

输出

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)



编辑于 2019-09-20 17:47:33 回复(2)
#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;
}

发表于 2019-08-14 22:24:27 回复(0)
思路:ip转换为整数,判断数字是否属于内网地址对应的数字:
10.0.0.0 - 10.255.255.255:  需要判断:10
127.0.0.0 - 127.255.255.255 需要判断: 127
172.16.0.0 - 172.31.255.255 需要判断: 12716 和12731
192.168.0.0 - 192.168.255.255 需要判断: 192168
#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;
}



编辑于 2020-04-17 14:47:59 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int a,b,c,d;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    if(a==10 || (a==172 && (b>=16 && b<32)) || (a==192 && b==168))
        cout<<1<<endl;
    else
        cout<<0<<endl;
    return 0;
}

发表于 2019-10-10 22:15:35 回复(0)
Java解法
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);
    }
}


发表于 2020-03-01 22:12:07 回复(0)
ip = input().split('.')
if ip[0] == '10':
    print(1)
elif ip[0] == '172':
    if ip[1] >= '16' and ip[1] <= '32':
        print(1)
    else:
        print(0)
elif ip[0] == '192' and ip[1] == '168':
    print(1)
else:
    print(0)

发表于 2019-07-22 08:07:04 回复(1)
#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;
}

C++代码
3种ip地址范围:
10.0.0.0 - 10.255.255.255
127.0.0.0 - 127.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255


发表于 2021-07-15 10:08:02 回复(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))

发表于 2020-08-01 14:50:12 回复(0)
// 私有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)
    }

发表于 2020-03-25 18:27:03 回复(0)
虽然测试用例可能会不充分,但是自己实际写代码还是尽量多判断一些情况。比如后两段>255和一些异常输入。
关键知识点:
私有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))



编辑于 2019-09-30 02:51:16 回复(0)
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是怎么个输入法。。

发表于 2019-08-20 09:40:34 回复(0)
#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
    // B类地址:172.16.0.0--172.31.255.255 
    // C类地址:192.168.0.0--192.168.255.255
发表于 2019-08-11 23:51:28 回复(0)
C++利用stringstream 和getline分割字符串
#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;
}


发表于 2019-08-06 21:37:46 回复(0)
#include <iostream>
#include <string>
using namespace std;

bool isIntranetIP(string ip)
{
    if(ip.find("10.") == 0)  //A类网址 
        return true;
    if(ip.compare("172.16.") > 0 && ip.compare("172.32.") < 0)  //B类网址 
        return true;
    if(ip.find("192.168.") == 0)  //C类网址 
        return true;
    return false;
}

int main()
{
    string IP;
    while(cin>>IP)
    {
        if(isIntranetIP(IP))
            cout<<1<<endl;
        else
            cout<<0<<endl;
    }
    return 0;
}
发表于 2019-07-15 16:21:29 回复(0)

热门推荐

通过挑战的用户

查看代码