Ticket #2797: enumlookup.agi.2.patch

File enumlookup.agi.2.patch, 7.5 kB (added by evilbunny, 4 years ago)

Due to debian OpenSSL bug the RSA key pair needed to be regenerated

  • enumlookup.agi

    old new  
    77// Originally written for use with FreePBX. 
    88 
    99// Based on e164.org's enum.php script, available on http://www.e164.org/enum.phps 
     10// Extended version based on http://www.e164.org/enum2.phps 
    1011 
    1112/* --------WARNING--------- 
    1213 *  
     
    2526// Go through the ENUM providers and look for the number. 
    2627 
    2728$dialstr = ""; 
     29// Set $allow_plain_text to false to disable unencrypted DNS ENUM lookups. 
     30$allow_plain_text = true; 
    2831 
    2932foreach ($enums as $enum) { 
    3033  // Are we using php5 and can use get_dns_record? 
    31     if (function_exists("dns_get_record")) { 
     34  if (function_exists("dns_get_record")) { 
     35    if ($enum == 'e164.org' && function_exists("socket_create") && 
     36      function_exists("mcrypt_module_open") && 
     37      function_exists("openssl_public_encrypt")) { 
     38      $arr = get_encrypted($lookup, $enum); 
     39    } 
     40 
     41    if ((!isset($arr) || $arr === false) && $allow_plain_text != false) { 
    3242      $arr = get_php5($lookup, $enum); 
     43    } 
    3344  // else, do we have Pear Net DNS? 
    3445  // Disabled, as I couldn't easily get it working on my machine 
    3546  //} elseif ((@include 'Net/DNS.php') =='OK') { 
    3647  //  $arr = get_pear($arpa); 
    37   } else
     48  } elseif($allow_plain_text != false)
    3849    @exec('dig -h > /dev/null 2>&1 ', $res, $var); 
    3950    if ($var != 127) {  
    4051      $arr = get_dig($lookup, $enum); 
     
    120131  } 
    121132} 
    122133 
     134function byte_values($value, $start = false, $num = false) { 
     135  $stop = strlen($value); 
     136  if($num === false) 
     137    $num = $stop - $start; 
     138 
     139  $ret = 0; 
     140  for($x = $start; $num > ($x - $start); $x++) 
     141    $result |= (ord($value{$x}) << 8 * (($num - ($x - $start))-1)); 
     142 
     143  return $result; 
     144} 
     145 
     146function get_encrypted($lookup, $enum) { 
     147  global $AGI; 
     148 
     149  $nl = "\n"; 
     150  $pubkey = '-----BEGIN PUBLIC KEY-----'.$nl. 
     151    'MIICAjANBgkqhkiG9w0BAQEFAAOCAe8AMIIB6gKCAeEAorGbkc81xGWePkinYXbLM'.$nl. 
     152    'f/rDwepU/etd2ycfwsToxwnt6iHf7hO9ycjOmYpYG4uMG0PmYISaBiwWd55qpUUgI'.$nl. 
     153    'nhwVFMGX8KOe6c3nreQ9iKQWYu47A2QVk81qX3k5UvjY+Ab63Mg63GtA/DQ7ScI+S'.$nl. 
     154    'MJuPCkVlXaUy82xeR7zs1eGYI20/mcNoB/nM5OcSpkSJ0dPXFSTmC9DKLd0emUZX9'.$nl. 
     155    'PdTfz7iKzjWYSg5DOlJwnggeErzDeTHVPoHOa+jT5JkVHzX5wSR2oyN65/s5rswCf'.$nl. 
     156    'gjqi8lyO+zCSZiu9ZdtvFeLHcmDAkKh610iTdH8WeHt4HSp61yppQ/wnVqrguDms1'.$nl. 
     157    'ER5lNt70pB+qIZStyB4DXjlXK4PkEAWTwJRV8E0Ix7hbcJ9IuwVxUUbTqyCWRFVed'.$nl. 
     158    'dSVoMN9yI6eHKoGzFXRojncBRK0Rtq9F1yEKnP7lz1QB3XUdZ28ipW1Flg2zsWsKL'.$nl. 
     159    'PbKoyn5A0Q8TJRiXDpEkBhwwWaoaxDR1xy8UDOIWt/9Rf0sgjfjmN649gXqAnaysr'.$nl. 
     160    'WcpPO130W4u5SOyRZnQhGk2Jw+4hEub9c1/Fcf3la1BaMlMIzIJqfUwoTMRlakBFE'.$nl. 
     161    'faMmgFGT57Kfng+c7gb5ecipKVrMRXbjNdAgMBAAE='.$nl. 
     162    '-----END PUBLIC KEY-----'.$nl; 
     163 
     164  $AGI->verbose("Encrypted DNS look up $lookup on $enum",3); 
     165  $arpa = $enum; 
     166  for ($i = 0; $i < strlen($lookup); $i++) 
     167    $arpa = $lookup[$i].".".$arpa; 
     168 
     169  dns_get_record('e164.org', DNS_NS, $NS, $additonal); 
     170  foreach($NS as $key => $host) 
     171    $servers[] = $host['target']; 
     172 
     173  if(($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === false || 
     174    socket_bind($socket, '0.0.0.0', 0) == false) 
     175    return false; 
     176 
     177  $rnd = fopen('/dev/urandom', 'r'); 
     178  $urnd = fgets($rnd, 16); 
     179  fclose($rnd); 
     180  $key = substr(hash('sha1', $urnd, 1), 0, 16); 
     181  $td = mcrypt_module_open('rijndael-128', '', 'cbc', ''); 
     182  $key = substr($key, 0, mcrypt_enc_get_key_size($td)); 
     183 
     184  $bits = explode('.', $arpa); 
     185  $tmp = ''; 
     186  foreach($bits as $bit) 
     187    $tmp .= chr(strlen($bit)).$bit; 
     188  $hostname = $tmp; 
     189 
     190  foreach($servers as $server) 
     191  { 
     192    $IPs = dns_get_record($server, DNS_A, $NS2, $add2); 
     193    foreach($IPs as $IP) 
     194    { 
     195      $server_address = $IP['ip']; 
     196      $qryID = rand(0,65535); 
     197      $qry = chr($qryID >> 8).chr($qryID % 256).chr(1).chr(0).chr(0). 
     198        chr(1).chr(0).chr(0).chr(0).chr(0).chr(0).chr(1).$hostname. 
     199        chr(0).chr(0).chr(35).chr(0).chr(1).chr(0).chr(0).chr(41). 
     200        chr(16).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0); 
     201 
     202      openssl_public_encrypt($key.$qry, $encrypted, $pubkey); 
     203      $strlen = strlen($encrypted) + 3; 
     204      $encrypted = chr($strlen >> 8).chr($strlen % 256).chr(255).$encrypted; 
     205 
     206      $len = socket_sendto($socket, $encrypted, strlen($encrypted), 0, $server_address, 53); 
     207 
     208      $dnsreply = $read = ''; 
     209      $strlen = 0; 
     210      do 
     211      { 
     212        $streams = array($socket); 
     213        if(@socket_select($streams, $write = NULL, $except = NULL, 2) <= 0) 
     214          break; 
     215 
     216        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 0, 'usec' => 1)); 
     217        while(($flag = @socket_recv($socket, $buf, 4096, 0)) > 0) 
     218        { 
     219          $read .= $buf; 
     220          if($strlen == 0) 
     221            $strlen = (ord($buf{0}) << 8) + ord($buf{1}); 
     222 
     223          if($strlen > 0 && strlen($read) >= $strlen) 
     224          { 
     225            $cipher_text = substr($read, 3); 
     226            $iv_size = mcrypt_enc_get_iv_size($td); 
     227            $iv = substr($cipher_text, 0, $iv_size); 
     228            $cipher_text = substr($cipher_text, $iv_size); 
     229 
     230            $plain = false; 
     231            if(mcrypt_generic_init($td, $key, $iv) != -1) 
     232              $plain = mdecrypt_generic($td, $cipher_text); 
     233 
     234            mcrypt_generic_deinit($td); 
     235            mcrypt_module_close($td); 
     236 
     237            if(($dnsreply = @gzinflate($plain)) == false) 
     238              $dnsreply = $plain; 
     239            $replyID = byte_values($dnsreply, 0, 2); 
     240 
     241            if($replyID == $qryID) 
     242              break(4); 
     243            unset($dnsreply); 
     244          } 
     245        } 
     246      } while(1); 
     247    } 
     248  } 
     249 
     250  if(!isset($dnsreply{0})) 
     251    return false; 
     252 
     253  $querys = byte_values($dnsreply, 4, 2); 
     254  $ans = byte_values($dnsreply, 6, 2); 
     255 
     256  $queries = $answers = array(); 
     257  $offset = 12; 
     258  for($i = 0; $i < $querys; $i++) 
     259  { 
     260    while(($nextlen = byte_values($dnsreply, $offset++, 1)) > 0) 
     261    { 
     262      if(isset($queries[$i]['hostname'])) 
     263        $queries[$i]['hostname'] .= '.'; 
     264      $queries[$i]['hostname'] .= substr($dnsreply, $offset, $nextlen); 
     265      $offset += $nextlen; 
     266    } 
     267 
     268    $queries[$i]['type'] = byte_values($dnsreply, $offset, 2); 
     269    $offset += 2; 
     270    $queries[$i]['class'] = byte_values($dnsreply, $offset, 2); 
     271    $offset += 2; 
     272  } 
     273 
     274  for($i = 0; $i < $ans; $i++) 
     275  { 
     276    if(byte_values($dnsreply, $offset, 2) == 49164) 
     277    { 
     278      $answers[$i]['hostname'] = $queries[0]['hostname']; 
     279      $offset += 2; 
     280    } else { 
     281      while(($nextlen = byte_values($dnsreply, $offset++, 1)) > 0) 
     282      { 
     283        if(isset($answers[$i]['hostname'])) 
     284          $answers[$i]['hostname'] .= '.'; 
     285        $answers[$i]['hostname'] .= substr($dnsreply, $offset, $nextlen); 
     286        $offset += $nextlen; 
     287      } 
     288    } 
     289 
     290    $answers[$i]['type'] = byte_values($dnsreply, $offset, 2); 
     291    $offset += 2; 
     292    $answers[$i]['class'] = byte_values($dnsreply, $offset, 2); 
     293    $offset += 2; 
     294    $answers[$i]['ttl'] = byte_values($dnsreply, $offset, 4); 
     295    $offset += 4; 
     296    $datalength = byte_values($dnsreply, $offset, 2); 
     297    $offset += 2; 
     298    $off2 = 0; 
     299    $record = substr($dnsreply, $offset, $datalength); 
     300    $offset += $datalength; 
     301    $answers[$i]['prio']  = byte_values($record, $off2, 2); 
     302    $off2 += 2; 
     303    $answers[$i]['order'] = byte_values($record, $off2, 2); 
     304    $off2 += 2; 
     305    $flagslength = byte_values($record, $off2++, 1); 
     306    $answers[$i]['flag'] = substr($record, $off2, $flagslength); 
     307    $off2 += $flagslength; 
     308    $srvlength = byte_values($record, $off2++, 1); 
     309    $tech = $answers[$i]['tech'] = substr($record, $off2, $srvlength); 
     310    $off2 += $srvlength; 
     311    $regexlen = byte_values($record, $off2++, 1); 
     312    $bit = explode('!', substr($record, $off2, $regexlen)); 
     313    $URI = ereg_replace($bit[1], $bit[2], '+'.$lookup); 
     314    if($URI[3] == ':') 
     315      $URI[3] = '/'; 
     316    if($URI[4] == ':') 
     317      $URI[4] = '/'; 
     318    $answers[$i]['URI'] = $URI; 
     319  } 
     320 
     321  if (count($answers) > 0) { 
     322    return $answers; 
     323  } else { 
     324    return null; 
     325  } 
     326} 
     327 
    123328 
    124329// helper functions 
    125330function get_var( $agi, $value) 
     
    133338  } 
    134339  else return ''; 
    135340} 
    136  
    137 ?>