(*Adapted by Rafael Luís: rafaelluis@netmadeira.com
updated to Mathematica v.10 by Ross Moore *)

Clear[pieces, menger] 
pieces = Complement[ Flatten[Table[{i, j, k}, {i, 0, 3}, {j, 0, 3}, {k, 0, 3}], 
2],
 Union[Flatten[Table[{{1, 1, i}, {2, 1, i}, {1, 2, i}, {2, 2, i}, 
{1,i, 1}, {2, i, 1}, {1, i, 2}, {2, i, 2}, {i, 1, 
1}, {i, 2, 1}, {i, 1, 2}, {i, 2, 2}}, {i, 0, 3}], 1]]] ;
menger[cornerPt_, sideLen_, n_] := menger[cornerPt + #1*(sideLen/4), sideLen/4, n - 1] & /@ pieces 
menger[cornerPt_, sideLen_, 0] := Cuboid[cornerPt, cornerPt + sideLen*{1, 1, 1}]

Manipulate[Graphics3D[Flatten[menger[{0, 0, 0}, 1, k]], Boxed -> False
],{{k,1,"depth"},Range[3]}]