-1

Im automating nmap using python , i like to get some specific value from the results, which is a string

PORT    STATE SERVICE
443/tcp open  https
| ssl-cert: Subject: commonName=*.google.com/organizationName=Google LLC/stateOrProvinceName=California/countryName=US
| Subject Alternative Name: DNS:*.google.com, DNS:*.android.com, DNS:*.appengine.google.com, DNS:*.cloud.google.com, DNS:*.crowdsource.google.com, DNS:*.g.co, DNS:*.gcp.gvt2.com, DNS:*.gcpcdn.gvt1.com, DNS:*.ggpht.cn, DNS:*.gkecnapps.cn, DNS:*.google-analytics.com, DNS:*.google.ca, DNS:*.google.cl, DNS:*.google.co.in, DNS:*.google.co.jp, DNS:*.google.co.uk, DNS:*.google.com.ar, DNS:*.google.com.au, DNS:*.google.com.br, DNS:*.google.com.co, DNS:*.google.com.mx, DNS:*.google.com.tr, DNS:*.google.com.vn, DNS:*.google.de, DNS:*.google.es, DNS:*.google.fr, DNS:*.google.hu, DNS:*.google.it, DNS:*.google.nl, DNS:*.google.pl, DNS:*.google.pt, DNS:*.googleadapis.com, DNS:*.googleapis.cn, DNS:*.googlecnapps.cn, DNS:*.googlecommerce.com, DNS:*.googlevideo.com, DNS:*.gstatic.cn, DNS:*.gstatic.com, DNS:*.gstaticcnapps.cn, DNS:*.gvt1.com, DNS:*.gvt2.com, DNS:*.metric.gstatic.com, DNS:*.urchin.com, DNS:*.url.google.com, DNS:*.wear.gkecnapps.cn, DNS:*.youtube-nocookie.com, DNS:*.youtube.com, DNS:*.youtubeeducation.com, DNS:*.youtubekids.com, DNS:*.yt.be, DNS:*.ytimg.com, DNS:android.clients.google.com, DNS:android.com, DNS:developer.android.google.cn, DNS:developers.android.google.cn, DNS:g.co, DNS:ggpht.cn, DNS:gkecnapps.cn, DNS:goo.gl, DNS:google-analytics.com, DNS:google.com, DNS:googlecnapps.cn, DNS:googlecommerce.com, DNS:source.android.google.cn, DNS:urchin.com, DNS:www.goo.gl, DNS:youtu.be, DNS:youtube.com, DNS:youtubeeducation.com, DNS:youtubekids.com, DNS:yt.be
| Issuer: commonName=GTS CA 1O1/organizationName=Google Trust Services/countryName=US
| Public Key type: unknown
| Public Key bits: 256
| Signature Algorithm: sha256WithRSAEncryption
| Not valid before: 2020-03-03T09:45:25
| Not valid after:  2020-05-26T09:45:25
| MD5:   bda3 4bfa 9f3d 5091 14a2 4a0e 992b 183f
|_SHA-1: 12b0 59d4 f6fb cd67 5013 a49e 44cf 053f d773 a07f

my method return issuer,public key and public key bit and signature algorithm but with the rest of string output

def run_command():
    command ="nmap -p 443 --script ssl-cert google.com"
    output = subprocess.getoutput(command)
    Issuer= output.split("Issuer: ",1)[1]
    public_key = output.split("Public Key type: ",1)[1]
    public_key_bit= output.split("Public Key bits: ",1)[1]
    singature_algor = output.split("Signature Algorithm: ",1)[1]
    print(Issuer,public_key,public_key_bit,singature_algor)

the wanted results, just to print the value of each of these values bellow

Issuer: commonName=GTS CA 1O1/organizationName=Google Trust Services/countryName=US
Public Key type: unknown
Public Key bits: 256
Signature Algorithm: sha256WithRSAEncryption
4
  • What is the question? What have you tried? What result are you getting from the code you have? Commented Apr 9, 2020 at 8:18
  • i want to print the value of each of these : Issuer,Public Key type,Public Key bits and Signature Algorithm Commented Apr 9, 2020 at 8:22
  • 1
    why don't you use python3-nmap Commented Apr 9, 2020 at 8:38
  • I did try it but it doesn't support some nse scripts, check my other question here stackoverflow.com/questions/61098484/… Commented Apr 9, 2020 at 8:47

1 Answer 1

1

I manage to find a way

    def verify_certificate(self,to_check):
        command ="nmap -p 443 --script ssl-cert google.com"
        output = subprocess.getoutput(command)
        line = re.findall(to_check+":.*$", output, re.MULTILINE)
        for x in line:
            results = x.split(to_check+":",1)[1]
            print(results)

output

commonName=GTS CA 1O1/organizationName=Google Trust Services/countryName=US

and by changing to_check value, I get the other values as well without duplicate code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.