Created
March 10, 2019 10:07
-
-
Save UnsGentoals/7d9cb5553da63a96dceaa5b58bc26bc8 to your computer and use it in GitHub Desktop.
数码荔枝
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.在PHP 7里,正则式 ^abc$ 和 \Aabc\z 的区别是什么 | |
^abc$ 和 \Aabc\z 的区别是: | |
前者在不同模式下使用的含义不同, | |
在单行模式(默认下)可以仅匹配字符串"abc","abc/n", | |
在设置 D (PCRE_DOLLAR_ENDONLY) 后仅能匹配"abc" | |
在多行模式 m (PCRE_MULTILINE) 下可以匹配,字符串开始的"abc\n"和字符串结束的"\nabc",以及字符串中任意位置的"\nabc\n" | |
后者则永远仅匹配目标字符串"abc",而不会受模式修饰符的影响 | |
2.使用PHP代码,将 中国时间 2017/4/1 13:00:00 和 2017/10/30 13:00:00 转换为 英国时间 | |
$date1 = new DateTime('2017/4/1 13:00:00', new DateTimeZone('Asia/Shanghai')); | |
$date1->setTimezone(new DateTimeZone('Europe/London')); | |
echo $date1->format('Y-m-d H:i:sP') . "\n"; | |
//strange | |
$date2 = new DateTime('2017/10/30 13:00:00', new DateTimeZone('GMT+8')); | |
$date2->setTimezone(new DateTimeZone('GMT+0')); | |
echo $date2->format('Y-m-d H:i:sP') . "\n"; | |
3.不使用嵌套循环,以 id 为索引,将 b 数组的 name 放到 a 数组内; | |
$a = [ | |
['id' => 1, 'age' => 18], | |
['id' => 2, 'age' => 19], | |
['id' => 3, 'age' => 20], | |
['id' => 4, 'age' => 22] | |
]; | |
$b = [ | |
['id' => 2, 'name' => 'aa'], | |
['id' => 4, 'name' => 'bb'] | |
]; | |
foreach($a as $v){$a1['id_'.$v['id']]=$v;} | |
foreach($b as $v){$b1['id_'.$v['id']]=$v;} | |
$a = (array_values(array_replace_recursive($a1, $b1))); | |
4.写一个 PHP 函数,删除指定目录里所有没有文件扩展名的文件,并列举出所有被删除的文件(注意有嵌套目录) | |
function deleteFilesWithoutExtensions($path) { | |
$working_dir_handle = opendir($path); | |
while ($file = readdir($working_dir_handle)) { | |
if($file!="." && $file!="..") { | |
$fullpath = $path."/".$file; | |
if(!is_dir($fullpath)) { | |
$file_path_arry = preg_split ('/\//', $fullpath, 0, PREG_SPLIT_NO_EMPTY); | |
$filename = end($file_path_arry); | |
if(preg_match('/\./', $filename) == 0){ | |
echo "\ndeleteing file : ".$fullpath; | |
try { | |
unlink($fullpath); | |
}catch (Exception $e) { | |
echo "\nCaught exception : ", $e->getMessage(), "\n"; | |
} | |
} | |
} else { | |
deleteFilesWithoutExtensions($fullpath); | |
} | |
} | |
} | |
closedir($working_dir_handle); | |
} | |
5.使用 PHP 实现一个锁机制,防止同一个 PHP 程序被同时运行多次(防止作为服务被同时启动多个) | |
<?php | |
$fp = fopen("/tmp/myservice.lock", "c"); | |
if(flock($fp,LOCK_EX | LOCK_NB)) | |
{ | |
/* service_logic_here */ | |
flock($fp,LOCK_UN); | |
} | |
fclose($fp); | |
?> | |
6.写一个 PHP 函数,检测 IP 是否在指定的IP段(CIDR格式)内 (需要支持IPv6) | |
function checkIPAllow($ip_check,$cidrs_allowed){ | |
$allow = false; | |
foreach($cidrs_allowed as $cidr_allowed) { | |
if(strpos($cidr_allowed, '/') === false) { | |
// Check Single IP | |
if(inet_pton($ip_check) == inet_pton($cidr_allowed)) { | |
$allow = true; | |
break; | |
} | |
} | |
else { | |
// Check IP range | |
list($subnet, $bits) = explode('/', $cidr_allowed); | |
// Convert subnet to binary string of $bits length | |
$subnet = unpack('H*', inet_pton($subnet)); // Subnet in Hex | |
foreach($subnet as $i => $h) $subnet[$i] = base_convert($h, 16, 2); // Array of Binary | |
$subnet = substr(implode('', $subnet), 0, $bits); // Subnet in Binary, only network bits | |
// Convert remote IP to binary string of $bits length | |
$ip = unpack('H*', inet_pton($ip_check)); // IP in Hex | |
foreach($ip as $i => $h) $ip[$i] = base_convert($h, 16, 2); // Array of Binary | |
$ip = substr(implode('', $ip), 0, $bits); // IP in Binary, only network bits | |
// Check network bits match | |
if($subnet == $ip) { | |
$allow = true; | |
break; | |
} | |
} | |
} | |
if(!$allow) { | |
echo 'Ip not allow.'; | |
return false; | |
}else{ | |
echo 'Ip allow'; | |
return true; | |
} | |
} | |
$ip_check = "192.168.2.8"; | |
$cidrs_allowed = array( | |
'192.168.2.0/24' | |
); | |
checkIPAllow($ip_check,$cidrs_allowed); | |
7.写一个 PHP 函数,要求在 100GB 大小的多行文本文件内搜索指定字符串,并输出所有包含这个字符串的行的内容 | |
function readFileIterator($path) { | |
$handle = fopen($path, "r"); | |
while(!feof($handle)) { | |
yield trim(fgets($handle)); | |
} | |
fclose($handle); | |
} | |
function searchStringFromFile($file_path,$substring_to_search){ | |
$string_iterator = readFileIterator($file_path); | |
foreach ($string_iterator as $string) { | |
if(strpos($string, $substring_to_search) !== false) { | |
echo "查找到一行匹配:\n $string\n"; | |
} | |
} | |
} | |
8.写一个 PHP 函数,返回指定日期的上个月的第一天和最后一天的日期 (要求 Y-m-d 格式) | |
function getlastMonthDays($date){ | |
$timestamp=strtotime($date); | |
if(!$timestamp){ | |
throw new Exception('Bad date input.'); | |
} | |
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01')); | |
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day")); | |
return array('firstday'=>$firstday,'lastday'=>$lastday); | |
} | |
9.写一个 SQL 语句,统计出 csdn 600万 数据库中被使用次数最多的前10条密码 (只需给出你的sql语句) | |
SELECT csdn_user_password, COUNT(1) AS same_password_times FROM `csdn_leak`.`csdn_user` GROUP BY csdn_user_password ORDER BY same_password_times DESC LIMIT 10; | |
10.https://sudo.pagerduty.com/for_engineers/ 阅读这篇文章,回答下列问题 | |
a) 文章的开头给了三点简单的警告,请列出这三点警告具体说的是什么 | |
1.不要信任框架的功能没有错误,没有软件是没有漏洞的;不要盲目的使用框架,确定了解底层原理之后再使用; | |
2.不要因为是hackday就放松安全规范; | |
3.不要将真实用户数据用于非生产环境。 | |
b) 为什么我们不能通过使用关键字过滤的方法来防止数据库注入 | |
1.维护关键字黑名单的工作(注意大小写); | |
2.黑客可以构造特殊字符能绕过黑名单检查。 | |
c) 密码加盐的意义是什么 | |
防止数据库泄漏之后黑客能够使用彩虹表的方式暴力破解用户密码。 | |
d) XSS 与 CSRF 的区别是什么,分别应该如何防御 | |
XSS:用户在访问受攻击的网站时,会在浏览器端执行黑客定义的脚本; | |
对所有用户输入的数据进行输出操作前进行转义操作; | |
CSRF:用户在访问受攻击的网站时,会对网站发起非用户主动请求的操作; | |
正确设置和使用"CSRF token": | |
每个用户的每个会话设置不同的token; | |
使用安全的密码学算法真随机生成的token; | |
在服务器端检查token有效行,token归属者,token有无过期; | |
对所有状态改变操作检查token; | |
正确使用HTTP GET 请求: | |
不要将GET请求用于状态改变。 | |
e) 如何防止 Session ID cookies 被 XSS 漏洞攻击获取 | |
浏览器端的cookies应指定domain,并且设置secure 以及 httpOnly 标记。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment