Página 1 de 1

Extraer datos de Shodan

NotaPublicado: 2016-08-05 19:04 @836
por BigBear
Hola. Tengo el siguiente código:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!usr/bin/perl
  2.  
  3. use LWP::UserAgent;
  4. use JSON;
  5. use Data::Dumper;
  6.  
  7. my $nave = LWP::UserAgent->new( ssl_opts => { verify_hostname => 1 } );
  8. $nave->agent(
  9.     "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"
  10. );
  11. $nave->timeout(5);
  12.  
  13. my $code = toma("http://localhost/twitter/timeline.php");
  14.  
  15. my $resultado = JSON->new->decode($code);
  16.  
  17. print "Created at : " . $resultado->{"created_at"} . "\n";
  18.  
  19. sub toma {
  20.     return $nave->get( $_[0] )->content;
  21. }
  22.  
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4


Mi página local me devuelve este código shodan:

Sintáxis: [ Descargar ] [ Ocultar ]
Using javascript Syntax Highlighting
  1. [
  2.   {
  3.     "coordinates": null,
  4.     "favorited": false,
  5.     "truncated": false,
  6.     "created_at": "Wed Aug 29 17:12:58 +0000 2012",
  7.     "id_str": "240859602684612608",
  8.     "entities": {
  9.       "urls": [
  10.         {
  11.           "expanded_url": "https://dev.twitter.com/blog/twitter-certified-products",
  12.           "url": "https://t.co/MjJ8xAnT",
  13.           "indices": [
  14.             52,
  15.             73
  16.           ],
  17.           "display_url": "dev.twitter.com/blog/twitter-c\u2026"
  18.         }
  19.       ],
  20.       "hashtags": [
  21.  
  22.       ],
  23.       "user_mentions": [
  24.  
  25.       ]
  26.     },
  27.     "in_reply_to_user_id_str": null,
  28.     "contributors": null,
  29.     "text": "Introducing the Twitter Certified Products Program: https://t.co/MjJ8xAnT",
  30.     "retweet_count": 121,
  31.     "in_reply_to_status_id_str": null,
  32.     "id": 240859602684612608,
  33.     "geo": null,
  34.     "retweeted": false,
  35.     "possibly_sensitive": false,
  36.     "in_reply_to_user_id": null,
  37.     "place": null,
  38.     "user": {
  39.       "profile_sidebar_fill_color": "DDEEF6",
  40.       "profile_sidebar_border_color": "C0DEED",
  41.       "profile_background_tile": false,
  42.       "name": "Twitter API",
  43.       "profile_image_url": "http://a0.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
  44.       "created_at": "Wed May 23 06:01:13 +0000 2007",
  45.       "location": "San Francisco, CA",
  46.       "follow_request_sent": false,
  47.       "profile_link_color": "0084B4",
  48.       "is_translator": false,
  49.       "id_str": "6253282",
  50.       "entities": {
  51.         "url": {
  52.           "urls": [
  53.             {
  54.               "expanded_url": null,
  55.               "url": "http://dev.twitter.com",
  56.               "indices": [
  57.                 0,
  58.                 22
  59.               ]
  60.             }
  61.           ]
  62.         },
  63.         "description": {
  64.           "urls": [
  65.  
  66.           ]
  67.         }
  68.       },
  69.       "default_profile": true,
  70.       "contributors_enabled": true,
  71.       "favourites_count": 24,
  72.       "url": "http://dev.twitter.com",
  73.       "profile_image_url_https": "https://si0.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
  74.       "utc_offset": -28800,
  75.       "id": 6253282,
  76.       "profile_use_background_image": true,
  77.       "listed_count": 10775,
  78.       "profile_text_color": "333333",
  79.       "lang": "en",
  80.       "followers_count": 1212864,
  81.       "protected": false,
  82.       "notifications": null,
  83.       "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme1/bg.png",
  84.       "profile_background_color": "C0DEED",
  85.       "verified": true,
  86.       "geo_enabled": true,
  87.       "time_zone": "Pacific Time (US & Canada)",
  88.       "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
  89.       "default_profile_image": false,
  90.       "profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png",
  91.       "statuses_count": 3333,
  92.       "friends_count": 31,
  93.       "following": null,
  94.       "show_all_inline_media": false,
  95.       "screen_name": "twitterapi"
  96.     },
  97.     "in_reply_to_screen_name": null,
  98.     "source": "<a href="//sites.google.com/site/yorufukurou/%5C%22" rel="\"nofollow\"">YoruFukurou</a>",
  99.     "in_reply_to_status_id": null
  100.   },
  101.   {
  102.     "coordinates": null,
  103.     "favorited": false,
  104.     "truncated": false,
  105.     "created_at": "Sat Aug 25 17:26:51 +0000 2012",
  106.     "id_str": "239413543487819778",
  107.     "entities": {
  108.       "urls": [
  109.         {
  110.           "expanded_url": "https://dev.twitter.com/issues/485",
  111.           "url": "https://t.co/p5bOzH0k",
  112.           "indices": [
  113.             97,
  114.             118
  115.           ],
  116.           "display_url": "dev.twitter.com/issues/485"
  117.         }
  118.       ],
  119.       "hashtags": [
  120.  
  121.       ],
  122.       "user_mentions": [
  123.  
  124.       ]
  125.     },
  126.     "in_reply_to_user_id_str": null,
  127.     "contributors": null,
  128.     "text": "We are working to resolve issues with application management & logging in to the dev portal: https://t.co/p5bOzH0k ^TS",
  129.     "retweet_count": 105,
  130.     "in_reply_to_status_id_str": null,
  131.     "id": 239413543487819778,
  132.     "geo": null,
  133.     "retweeted": false,
  134.     "possibly_sensitive": false,
  135.     "in_reply_to_user_id": null,
  136.     "place": null,
  137.     "user": {
  138.       "profile_sidebar_fill_color": "DDEEF6",
  139.       "profile_sidebar_border_color": "C0DEED",
  140.       "profile_background_tile": false,
  141.       "name": "Twitter API",
  142.       "profile_image_url": "http://a0.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
  143.       "created_at": "Wed May 23 06:01:13 +0000 2007",
  144.       "location": "San Francisco, CA",
  145.       "follow_request_sent": false,
  146.       "profile_link_color": "0084B4",
  147.       "is_translator": false,
  148.       "id_str": "6253282",
  149.       "entities": {
  150.         "url": {
  151.           "urls": [
  152.             {
  153.               "expanded_url": null,
  154.               "url": "http://dev.twitter.com",
  155.               "indices": [
  156.                 0,
  157.                 22
  158.               ]
  159.             }
  160.           ]
  161.         },
  162.         "description": {
  163.           "urls": [
  164.  
  165.           ]
  166.         }
  167.       },
  168.       "default_profile": true,
  169.       "contributors_enabled": true,
  170.       "favourites_count": 24,
  171.       "url": "http://dev.twitter.com",
  172.       "profile_image_url_https": "https://si0.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
  173.       "utc_offset": -28800,
  174.       "id": 6253282,
  175.       "profile_use_background_image": true,
  176.       "listed_count": 10775,
  177.       "profile_text_color": "333333",
  178.       "lang": "en",
  179.       "followers_count": 1212864,
  180.       "protected": false,
  181.       "notifications": null,
  182.       "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme1/bg.png",
  183.       "profile_background_color": "C0DEED",
  184.       "verified": true,
  185.       "geo_enabled": true,
  186.       "time_zone": "Pacific Time (US & Canada)",
  187.       "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
  188.       "default_profile_image": false,
  189.       "profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png",
  190.       "statuses_count": 3333,
  191.       "friends_count": 31,
  192.       "following": null,
  193.       "show_all_inline_media": false,
  194.       "screen_name": "twitterapi"
  195.     },
  196.     "in_reply_to_screen_name": null,
  197.     "source": "<a href="//sites.google.com/site/yorufukurou/%5C%22" rel="\"nofollow\"">YoruFukurou</a>",
  198.     "in_reply_to_status_id": null
  199.   }
  200. ]
Coloreado en 0.006 segundos, usando GeSHi 1.0.8.4

Solo son datos de tuits. Necesito extraer "coordinates" y el "retweet_count" de cada tuit pero el tema es que no sé cómo listar esos dos únicos tuits, están divididos de forma rara como tres arrays por cada tuit.

No puedo extraer ningún dato, ni siquiera el más básico "created_at" porque me dice que no existe: Not a HASH reference at

¿ Cómo puedo leer estos datos ?

Re: Extraer datos de Shodan

NotaPublicado: 2016-08-05 23:03 @002
por explorer
Los corchetes inicial y final indican un nivel más de profundidad. Cierto: la estructura consiste en un array que contiene dos elementos. Debes indicarlo a la hora de acceder a la información.

Por ejemplo, con un código como el siguiente:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. use v5.24;
  3. use autodie;
  4. use JSON;
  5.  
  6. my $json;
  7. {
  8.     local $/;
  9.     open my $fh, '<', 'shodan.json';
  10.     my $json_text = <$fh>;
  11.     close $fh;
  12.     $json = decode_json( $json_text );
  13. }
  14.  
  15. #use Data::Dumper;
  16. #say Dumper $json;
  17.  
  18. say $json->[1]{'user'}{'created_at'};   # Wed May 23 06:01:13 +0000 2007
  19.  
  20. say $json->[0]{'entities'}{'urls'}[0]{'indices'}[1]; # 73
  21. say $json->[1]{'entities'}{'urls'}[0]{'indices'}[1]; # 118
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


Pero... hay un problema: el json mostrado no está bien formado.

En las líneas "source", el valor no tiene bien escapadas las comillas. Por ejemplo, lo que has mostrado es así:
Sintáxis: [ Descargar ] [ Ocultar ]
Using javascript Syntax Highlighting
  1. "source": "<a href="//sites.google.com/site/yorufukurou/%5C%22" rel="\"nofollow\"">YoruFukurou</a>",
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4
cuando debería ser esto:
Sintáxis: [ Descargar ] [ Ocultar ]
Using javascript Syntax Highlighting
  1. "source": "<a href=\"//sites.google.com/site/yorufukurou/%5C%22\" rel=\"nofollow\">YoruFukurou</a>",
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Re: Extraer datos de Shodan

NotaPublicado: 2016-08-06 09:52 @452
por BigBear
Hola, explorer, gracias por la ayuda, tengo este código:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. my $resultado = JSON->new->decode($code);
  2.  
  3. my @encontrados = @{ $resultado->{"matches"} };
  4. foreach my $encontrado (@encontrados) {
  5.         print "Created at : ".$encontrado->[0]{"user"}{"created_at"}."\n";
  6. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


El tema es cómo puedo listar estos dos tuits en un array para poder leerlo como un array cómodamente, no sé qué nombre ponerle que no sea 'matches' porque no encuentro otro nombre que englobe estos dos tuits.

¿ Se puedo hacer esto en este caso ?

Re: Extraer datos de Shodan

NotaPublicado: 2016-08-08 07:49 @367
por explorer
No entiendo la pregunta. 'matches' no está en ninguna parte en el JSON de ejemplo.

Si lo que quieres es convertir una referencia a un array, en un array, entonces lo que tienes que hacer es des-referenciarlo :)

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. my $resultado = JSON->new->decode($code);
  2. my @resultado = @$resultado;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4