Skip to content

Commit b3cad9b

Browse files
committed
PHP and Javascript solutions to moderate challenge #137 - Seek for an intruder (ip dotted or plain, octat, hex, binary, or decimal regexp)
1 parent b4806c3 commit b3cad9b

File tree

3 files changed

+346
-0
lines changed

3 files changed

+346
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// https://www.codeeval.com/open_challenges/137/
2+
3+
function hexdec(hex_string) {
4+
hex_string = (hex_string + '').replace(/[^a-f0-9]/gi, '');
5+
return parseInt(hex_string, 16);
6+
}
7+
function bindec(binary_string) {
8+
binary_string = (binary_string + '').replace(/[^01]/gi, '');
9+
return parseInt(binary_string, 2);
10+
}
11+
function octdec(oct_string) {
12+
oct_string = (oct_string + '').replace(/[^0-7]/gi, '');
13+
return parseInt(oct_string, 8);
14+
}
15+
function decbin(number) {
16+
if (number < 0) {
17+
number = 0xFFFFFFFF + number + 1;
18+
}
19+
return parseInt(number, 10).toString(2);
20+
}
21+
22+
var ips = {};
23+
var patterns = {
24+
'dotted_hexa_pattern': /(0x[1-f][0-f]|0x0[1-f]|0x[1-f])\.(0x[0-f]{2}|0x0)\.(0x[0-f]{2}|0x0)\.(0xf[0-e]|0x[1-e][0-f]|0x0[0-f]|0x0)/,
25+
'dotted_binary_pattern': /([0-1]{7}1|[0-1]{6}1[0-1]|[0-1]{5}1[0-1]{2}|[0-1]{4}1[0-1]{3}|[0-1]{3}1[0-1]{4}|[0-1]{2}1[0-1]{5}|[0-1]1[0-1]{6}|1[0-1]{7})\.([0-1]{8})\.([0-1]{8})\.([0-1]{7}0|[0-1]{6}0[0-1]|[0-1]{5}0[0-1]{2}|[0-1]{4}0[0-1]{3}|[0-1]{3}0[0-1]{4}|[0-1]{2}0[0-1]{5}|[0-1]0[0-1]{6}|0[0-1]{7})/,
26+
'dotted_octal_pattern': /(0[1-3][0-7]{2}|00[1-7][0-7]|000[1-7])\.(0[1-3][0-7]{2}|00[0-7]{2})\.(0[1-3][0-7]{2}|00[0-7]{2})\.(037[0-6]|03[0-6][0-7]|0[1-2][0-7]|00[0-7]{2})/,
27+
'dotted_decimal_pattern': /(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])/,
28+
'hexa_pattern': /0x([1-f][0-f]|0[1-f]|[1-f])([0-f]{2}|0)([0-f]{2}|0)(f[0-e]|[1-e][0-f]|0[0-f]|0)/,
29+
'binary_pattern': /([0-1]{7}1|[0-1]{6}1[0-1]|[0-1]{5}1[0-1]{2}|[0-1]{4}1[0-1]{3}|[0-1]{3}1[0-1]{4}|[0-1]{2}1[0-1]{5}|[0-1]1[0-1]{6}|1[0-1]{7})([0-1]{8})([0-1]{8})([0-1]{7}0|[0-1]{6}0[0-1]|[0-1]{5}0[0-1]{2}|[0-1]{4}0[0-1]{3}|[0-1]{3}0[0-1]{4}|[0-1]{2}0[0-1]{5}|[0-1]0[0-1]{6}|0[0-1]{7})/,
30+
'octal_pattern': /(3777777777[0-6]|377777777[0-6][0-7]|37777777[0-6][0-7]{2}|3777777[0-6][0-7]{3}|377777[0-6][0-7]{4}|37777[0-6][0-7]{5}|3777[0-6][0-7]{6}|377[0-6][0-7]{7}|37[0-6][0-7]{8}|3[0-6][0-7]{9}|[1-2][0-7]{10}|[1-7][0-7]{8})/,
31+
'decimal_pattern': /(429496729[0-4]|42949672[0-8][0-9]|4294967[0-2][0-9]{2}|429496[0-7][0-9]{3}|42949[0-6][0-9]{4}|4294[0-8][0-9]{5}|429[0-3][0-9]{6}|42[0-8][0-9]{7}|4[0-1][0-9]{8}|[1-9][0-9]{8}|1677721[6-9]|167772[2-9][0-9]|16777[3-9][0-9]{2}|1677[8-9][0-9]{3}|167[8-9][0-9]{4}|16[8-9][0-9]{5}|1[7-9][0-9]{6}|[2-9][0-9]{7})/
32+
};
33+
34+
var fs = require("fs");
35+
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
36+
line = line.trim();
37+
if( line !== '' ){
38+
for( var name in patterns ){
39+
var pattern = patterns[name];
40+
var matches = null;
41+
if( null !== ( matches = line.match(pattern) ) ){
42+
// console.log(name, matches);
43+
var ip = false;
44+
var bin = false;
45+
var bin_len = 0;
46+
switch( name ){
47+
case 'dotted_hexa_pattern' :
48+
ip = hexdec(matches[1]) + '.' + hexdec(matches[2]) + '.' + hexdec(matches[3]) + '.' + hexdec(matches[4]);
49+
break;
50+
case 'dotted_binary_pattern' :
51+
ip = bindec(matches[1])+'.'+bindec(matches[2])+'.'+bindec(matches[3])+'.'+bindec(matches[4]);
52+
break;
53+
case 'dotted_octal_pattern' :
54+
ip = octdec(matches[1])+'.'+octdec(matches[2])+'.'+octdec(matches[3])+'.'+octdec(matches[4]);
55+
break;
56+
case 'dotted_decimal_pattern' :
57+
ip = matches[1]+'.'+matches[2]+'.'+matches[3]+'.'+matches[4];
58+
break;
59+
case 'hexa_pattern' :
60+
ip = hexdec(matches[1])+'.'+hexdec(matches[2])+'.'+hexdec(matches[3])+'.'+hexdec(matches[4]);
61+
break;
62+
case 'binary_pattern' :
63+
ip = bindec(matches[1])+'.'+bindec(matches[2])+'.'+bindec(matches[3])+'.'+bindec(matches[4]);
64+
break;
65+
case 'octal_pattern' :
66+
bin = decbin(octdec(matches[1]));
67+
bin_len = bin.length;
68+
if( bin_len < 32 ) {
69+
bin = new Array( 32 - bin_len +1 ).join('0') + bin;
70+
}
71+
ip = bindec(bin.substr(0,8))+'.'+bindec(bin.substr(8,8))+'.'+bindec(bin.substr(16,8))+'.'+bindec(bin.substr(24,8));
72+
break;
73+
case 'decimal_pattern' :
74+
bin = decbin(matches[1]);
75+
bin_len = bin.length;
76+
if( bin_len < 32 ) {
77+
bin = new Array( 32 - bin_len +1 ).join('0') + bin;
78+
}
79+
ip = bindec(bin.substr(0,8))+'.'+bindec(bin.substr(8,8))+'.'+bindec(bin.substr(16,8))+'.'+bindec(bin.substr(24,8));
80+
break;
81+
}
82+
if( ip ){
83+
if( 'undefined' === typeof(ips[ip]) ){
84+
ips[ip] = 0;
85+
}
86+
ips[ip]++;
87+
}
88+
}
89+
}
90+
}
91+
});
92+
93+
var new_ips = [];
94+
for(var ip in ips){
95+
new_ips.push({ip: ip, v: ips[ip]});
96+
}
97+
98+
new_ips.sort( function(a,b){
99+
if( a.v == b.v ){
100+
return b.ip - a.ip;
101+
}
102+
else{
103+
return b.v - a.v;
104+
}
105+
});
106+
107+
var max = new_ips[0].v;
108+
109+
var result = '';
110+
for(var i=0, cnt=new_ips.length; i < cnt ; i++){
111+
var ip = new_ips[i];
112+
if( ip.v == max ){
113+
if( result !== '' ) result += ' ';
114+
result += ip.ip;
115+
}
116+
else{
117+
break;
118+
}
119+
}
120+
121+
console.log(result);
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
header('Content-Type: text/plain; charset=utf-8');
3+
4+
// https://www.codeeval.com/open_challenges/137/
5+
6+
if( isset($_GET['f_i_l_e']) && $_GET['f_i_l_e'] ) $argv[1] = $_GET['f_i_l_e'];
7+
8+
if( isset($argv[1]) && $argv[1] ){
9+
$filename = $argv[1];
10+
if( file_exists($filename) ){
11+
if( is_readable($filename) ){
12+
$fp = fopen($filename, 'r');
13+
if( $fp ){
14+
/*
15+
Dotted decimal 192.0.2.235 with no leading zero.
16+
Decimal 3221226219 The 32-bit number expressed in decimal.
17+
18+
Dotted hexadecimal 0xc0.0x0.0x02.0xeb Each octet is individually converted to hexadecimal form.
19+
Hexadecimal 0xC0 00 02 EB Concatenation of the octets from the dotted hexadecimal.
20+
21+
Dotted octal 0300.0000.0002.0353 Each octet is individually converted into octal.
22+
Octal 030000001353
23+
24+
Dotted binary 11000000.00000000.00000010.11101011 Each octet is individually converted into binary.
25+
Binary 11000000 00000000 00000010 11101011
26+
27+
28+
29+
// 1.0.0.0 => 255.255.255.254
30+
// 1-255 => (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])
31+
// 0-255 => (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
32+
// 0-254 => (25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
33+
*/
34+
35+
$dotted_decimal_pattern = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])';
36+
$dotted_decimal_pattern .= '\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])';
37+
$dotted_decimal_pattern .= '\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])';
38+
$dotted_decimal_pattern .= '\.(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])';
39+
$dotted_decimal_pattern = '/'.$dotted_decimal_pattern.'/';
40+
// echo $dotted_decimal_pattern."\n";
41+
42+
// 16777216 => 4 294 967 294
43+
$decimal_pattern = '/(';
44+
$decimal_pattern .= '429496729[0-4]';
45+
$decimal_pattern .= '|42949672[0-8][0-9]';
46+
$decimal_pattern .= '|4294967[0-2][0-9]{2}';
47+
$decimal_pattern .= '|429496[0-7][0-9]{3}';
48+
$decimal_pattern .= '|42949[0-6][0-9]{4}';
49+
$decimal_pattern .= '|4294[0-8][0-9]{5}';
50+
$decimal_pattern .= '|429[0-3][0-9]{6}';
51+
$decimal_pattern .= '|42[0-8][0-9]{7}';
52+
$decimal_pattern .= '|4[0-1][0-9]{8}';
53+
54+
$decimal_pattern .= '|[1-9][0-9]{8}';
55+
56+
$decimal_pattern .= '|1677721[6-9]';
57+
$decimal_pattern .= '|167772[2-9][0-9]';
58+
$decimal_pattern .= '|16777[3-9][0-9]{2}';
59+
$decimal_pattern .= '|1677[8-9][0-9]{3}';
60+
$decimal_pattern .= '|167[8-9][0-9]{4}';
61+
$decimal_pattern .= '|16[8-9][0-9]{5}';
62+
$decimal_pattern .= '|1[7-9][0-9]{6}';
63+
$decimal_pattern .= '|[2-9][0-9]{7}';
64+
$decimal_pattern .= ')/';
65+
// echo $decimal_pattern ."\n";
66+
67+
// 0xc0.0x0.0x02.0xeb
68+
// 0x01.0x0.0x0.0x0 => 0xff.0xff.0xff.0xfe
69+
$dotted_hexa_pattern = '(0x[1-f][0-f]|0x0[1-f]|0x[1-f])'; // 0x01=>0xff
70+
$dotted_hexa_pattern .= '\.(0x[0-f]{2}|0x0)'; // 0x0=>0xff
71+
$dotted_hexa_pattern .= '\.(0x[0-f]{2}|0x0)'; // 0x0=>0xff
72+
$dotted_hexa_pattern .= '\.(0xf[0-e]|0x[1-e][0-f]|0x0[0-f]|0x0)'; // 0x0=>0xfe
73+
$dotted_hexa_pattern = '/'.$dotted_hexa_pattern.'/';
74+
// echo $dotted_hexa_pattern."\n";
75+
76+
$hexa_pattern = str_replace('\.','',$dotted_hexa_pattern);
77+
$hexa_pattern = str_replace('/','',$hexa_pattern);
78+
$hexa_pattern = '/0x' . str_replace('0x','',$hexa_pattern).'/';
79+
// echo $hexa_pattern."\n";
80+
81+
// 0001.0000.0000.000 => 0377.0377.0377.0376
82+
$dotted_octal_pattern = '(0[1-3][0-7]{2}|00[1-7][0-7]|000[1-7])'; // 0001=>0377
83+
$dotted_octal_pattern .= '\.(0[1-3][0-7]{2}|00[0-7]{2})'; // 0000=>0377
84+
$dotted_octal_pattern .= '\.(0[1-3][0-7]{2}|00[0-7]{2})'; // 0000=>0377
85+
$dotted_octal_pattern .= '\.(037[0-6]|03[0-6][0-7]|0[1-2][0-7]|00[0-7]{2})'; // 0000=>0376
86+
$dotted_octal_pattern = '/'.$dotted_octal_pattern.'/';
87+
// echo $dotted_octal_pattern."\n";
88+
89+
// 1 00 00 00 00 => 3 77 77 77 77 76
90+
91+
$octal_pattern = '/(';
92+
$octal_pattern .= '3777777777[0-6]';
93+
$octal_pattern .= '|377777777[0-6][0-7]';
94+
$octal_pattern .= '|37777777[0-6][0-7]{2}';
95+
$octal_pattern .= '|3777777[0-6][0-7]{3}';
96+
$octal_pattern .= '|377777[0-6][0-7]{4}';
97+
$octal_pattern .= '|37777[0-6][0-7]{5}';
98+
$octal_pattern .= '|3777[0-6][0-7]{6}';
99+
$octal_pattern .= '|377[0-6][0-7]{7}';
100+
$octal_pattern .= '|37[0-6][0-7]{8}';
101+
$octal_pattern .= '|3[0-6][0-7]{9}';
102+
$octal_pattern .= '|[1-2][0-7]{10}';
103+
$octal_pattern .= '|[1-7][0-7]{8}';
104+
$octal_pattern .= ')/';
105+
// echo $octal_pattern."\n";
106+
107+
108+
109+
110+
// 00000001.00000000.00000000.00000000 => 11111111.11111111.11111111.11111110
111+
$dotted_binary_pattern = '([0-1]{7}1|[0-1]{6}1[0-1]|[0-1]{5}1[0-1]{2}|[0-1]{4}1[0-1]{3}|[0-1]{3}1[0-1]{4}|[0-1]{2}1[0-1]{5}|[0-1]1[0-1]{6}|1[0-1]{7})'; // 00000001=>11111111
112+
$dotted_binary_pattern .= '\.([0-1]{8})'; // 00000000=>11111111
113+
$dotted_binary_pattern .= '\.([0-1]{8})'; // 00000000=>11111111
114+
$dotted_binary_pattern .= '\.([0-1]{7}0|[0-1]{6}0[0-1]|[0-1]{5}0[0-1]{2}|[0-1]{4}0[0-1]{3}|[0-1]{3}0[0-1]{4}|[0-1]{2}0[0-1]{5}|[0-1]0[0-1]{6}|0[0-1]{7})'; // 00000000=>11111110
115+
$dotted_binary_pattern = '/'.$dotted_binary_pattern.'/';
116+
// echo $dotted_binary_pattern."\n";
117+
118+
$binary_pattern = str_replace('\.','',$dotted_binary_pattern);
119+
// echo $binary_pattern."\n";
120+
121+
$patterns = array(
122+
'dotted_hexa_pattern' => $dotted_hexa_pattern,
123+
'dotted_binary_pattern' => $dotted_binary_pattern,
124+
'dotted_octal_pattern' => $dotted_octal_pattern,
125+
'dotted_decimal_pattern' => $dotted_decimal_pattern,
126+
'hexa_pattern' => $hexa_pattern,
127+
'binary_pattern' => $binary_pattern, // invert order?
128+
'octal_pattern' => $octal_pattern, // invert order?
129+
'decimal_pattern' => $decimal_pattern,
130+
);
131+
132+
// print_r($patterns);
133+
134+
$ips = array();
135+
136+
while ( $fp && !feof( $fp ) ) {
137+
$line = trim(fgets($fp));
138+
if( $line ){
139+
foreach($patterns as $name => $pattern){
140+
// echo $name.': '."\n";
141+
if( preg_match_all($pattern, $line, $matches_sets, PREG_SET_ORDER ) ){
142+
for($i=0, $cnt = count($matches_sets) ; $i < $cnt ; $i++){
143+
$matches = $matches_sets[$i];
144+
$ip = false;
145+
switch( $name ){
146+
case 'dotted_hexa_pattern' :
147+
$ip = hexdec($matches[1]).'.'.hexdec($matches[2]).'.'.hexdec($matches[3]).'.'.hexdec($matches[4]);
148+
break;
149+
case 'dotted_binary_pattern' :
150+
$ip = bindec($matches[1]).'.'.bindec($matches[2]).'.'.bindec($matches[3]).'.'.bindec($matches[4]);
151+
break;
152+
case 'dotted_octal_pattern' :
153+
$ip = octdec($matches[1]).'.'.octdec($matches[2]).'.'.octdec($matches[3]).'.'.octdec($matches[4]);
154+
break;
155+
case 'dotted_decimal_pattern' :
156+
$ip = $matches[1].'.'.$matches[2].'.'.$matches[3].'.'.$matches[4];
157+
break;
158+
case 'hexa_pattern' :
159+
$ip = hexdec($matches[1]).'.'.hexdec($matches[2]).'.'.hexdec($matches[3]).'.'.hexdec($matches[4]);
160+
break;
161+
case 'binary_pattern' :
162+
$ip = bindec($matches[1]).'.'.bindec($matches[2]).'.'.bindec($matches[3]).'.'.bindec($matches[4]);
163+
break;
164+
case 'octal_pattern' :
165+
$bin = sprintf('%32b',octdec($matches[1]));
166+
$bin = str_split($bin,8);
167+
$ip = bindec($bin[0]).'.'.bindec($bin[1]).'.'.bindec($bin[2]).'.'.bindec($bin[3]);
168+
break;
169+
case 'decimal_pattern' :
170+
$bin = sprintf('%32b',$matches[1]);
171+
$bin = str_split($bin,8);
172+
$ip = bindec($bin[0]).'.'.bindec($bin[1]).'.'.bindec($bin[2]).'.'.bindec($bin[3]);
173+
break;
174+
}
175+
if( $ip ){
176+
if( !isset($ips[$ip]) ){
177+
$ips[$ip] = 0;
178+
}
179+
$ips[$ip]++;
180+
}
181+
}
182+
}//
183+
}
184+
}//
185+
}//
186+
fclose( $fp );
187+
arsort($ips);
188+
list($k,$v) = each($ips);
189+
$ips = array_keys($ips, $v);
190+
sort($ips);
191+
echo implode(' ',$ips)."\n";
192+
}
193+
else{
194+
echo '!fp'."\n";
195+
}
196+
}
197+
else{
198+
echo '!readable'."\n";
199+
}
200+
}
201+
else{
202+
echo '!file_exists'."\n";
203+
}
204+
}
205+
else{
206+
echo '!argv[1]'."\n";
207+
}
208+
209+
exit(0);
210+
211+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
m*M}Qz`\fz/We}e[`md;Puuat-;UP|Yi64iXh%{Hnul8&onq0p*mY+4x\/{ZTw[gXeJV2&.P*Ywe
2+
VA,8Z%z-AYzp6o{qeX3Q|\`Zw7{78:Y80qP-,b0BDVvZh60x59.0xe5.0x82.0xe1uptW8eF8C]nKJ9c(AtXa9>Dy}nF'Jr
3+
Cq2ox2Mmr7PuaPO023244514100.@({-mER/yhWg)wsf"`Fu_tp.C]6$!?(^+wzLBxi,PJ41Hdu`m>Bz=v*^~N|h
4+
jfZ&y9w'XkrrO6JDoZOyZ864.599.341.917JBJ5u(^i%BjecAd"$4UKtPnbtvx^01540.01127.0525.01625tU$HY/,Uw(/CJP]L+/XohV2hD&]
5+
9Pl.1011001111001011000001011100001,Y,HNAiSzL;?BU_UQlCvyzRU^"R]{kVJ"[+3%PK`]\"V?;Y'8CjJ<&QGmESP6W7&P,@$tFtL
6+
`z6DR}/>gLfLX[1&]Vr8"EG-_+wy?sw4beHIp^oTtZzvWBwY{[89.229.130.225R,?B;"?[ix4^9D$fVaJ_V\)N`B
7+
=ddalCNOM)FnA=/r,?}#idpo1E#eeMq.wyfu/2viz;c_[kHppMh,K|\`Q1_R(`jRNvCZW2Niz7Q#
8+
w:b21f[rsnj^Rgg[t!(<5v`Iup^&][email protected]\SwBEbN222.137.104.206[Jo<)lj36bB.034062405073xx37d;~wKi/D"I'AeVfeBO
9+
|7$mi3k]f}9N*Vjq5aMy[Xd+3a$n$paB?5p9^01000000.11101110.00100000.01100101u@0:7&J;8FDZt840131.0345.0202.0341`}Z*Xz[8IH?'
10+
^~H5]JqL#?>d8V5JPP1101100000.1001010111.101010101.11100101011Funfr=3*E\pEa"3YV^?J+;dLA#t)$3Lvi5J?|?qQyV(0k?>KPB:t{IRUm>cuN0^[YSOsixxF"zzl5LALPIaXFM.
11+
jMfgpfH+w>M8\`r{;`XdEYm0Rc7o>Aqq4k?gP>,O^2I^]OF#zN\cLSUQ(x!(oxA0ld:yYl*K_AK^Hd_cpd`
12+
OwEK!d?y\M(_L~|=lm1++BLA<&PnOnBAfga>t},x{T$*&/}+wX{j/pm'|N~Cq1000000111011100010000001100101x~[*Scc=lb82`K~
13+
HydTS#@@864.599.341.917Q&.DVb\ails}C101100100110010011011000101110100&@KyFK"7}u3\63?u][~zz>-r$_OqUbE*uv,\ccCnUmP\`4/Ht!3puL.A\'].BA>reos{U0x360.0x257.0x155.0x395y^UGXh^'`|I.CV}R>a}RAhO%Vw
14+
uQ#27/z^B8q:x(I|$k9dmF{\P(t!ui5[

0 commit comments

Comments
 (0)