Zhuanli&Blog


  • 首页

  • 标签

  • 分类

  • 归档

linux下网络抓包过程

发表于 2018-03-13 | 分类于 linux

linux网络抓包

1.使用tcpdump

tcpdump tcp -i eth0 -t -s 0 and src net 101.95.31.111 -w ./target.cap
tcp: ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型
-i eth0 : 只抓经过接口eth0的包
-t : 不显示时间戳
-s 0 : 抓取数据包时默认抓取长度为68字节。加上-S 0 后可以抓到完整的数据包
src net 101.95.31.111:抓取来源地址
-w ./target.cap:抓取保存

2.使用wireshark

在windows操作系统下,用wireshark打开保存的target.cap文件然后查看,wireshark的具体使用方法自行百度。

c语言文件读写

发表于 2018-03-13 | 分类于 c

读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FILE *pFile=fopen("1.txt","r"); //获取文件的指针

char *pBuf; //定义文件指针

fseek(pFile,0,SEEK_END); //把指针移动到文件的结尾 ,获取文件长度

int len=ftell(pFile); //获取文件长度

pBuf=new char[len+1]; //定义数组长度

rewind(pFile); //把指针移动到文件开头 因为我们一开始把指针移动到结尾,如果不移动回来 会出错

fread(pBuf,1,len,pFile); //读文件

pBuf[len]=0; //把读到的文件最后一位 写为0 要不然系统会一直寻找到0后才结束

MessageBox(pBuf); //显示读到的数据

fclose(pFile); // 关闭文件

写

1
2
3
4
5
6
7
8
9
10
11
12
//获取文件指针
FILE *pFile = fopen("1.txt", //打开文件的名称
"w"); // 文件打开方式 如果原来有内容也会销毁
/
//向文件写数据
fwrite ("hello", //要输入的文字
1,//文字每一项的大小 以为这里是字符型的 就设置为1 如果是汉字就设置为4
strlog("hello"), //单元个数 我们也可以直接写5
pFile //我们刚刚获得到的地址
);
//fclose(pFile); //告诉系统我们文件写完了数据更新,但是我们要要重新打开才能在写
fflush(pFile); //数据刷新 数据立即更新

base64编解码源码解析

发表于 2018-03-13 | 分类于 c

base64 decode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//可以自己定制,比如说把+和/改成-和_以满足url编码的要求。
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char t;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )//每四个字节为一组
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[0]= k;//获取一组中每个字节对应的编号
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+1] )
temp[1]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+2] )
temp[2]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+3] )
temp[3]= k;
}

bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |((unsigned char)((unsigned char)(temp[1]>>4)&0x03));//这些就是补0补位的操作了

if ( base64[i+2] == '=' )
break;

bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) | ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));

if ( base64[i+3] == '=' )
break;

bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |((unsigned char)(temp[3]&0x3F));

}
return j;
}

base64_encode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
char * base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;

for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];

current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];

current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];

current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
}

url编解码实现

发表于 2018-03-13 | 分类于 c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
static unsigned char char_to_hex(unsigned char x)
{
return (unsigned char)(x > 9 ? x + 55 : x + 48);
}

static int is_alpha_number_char(unsigned char c)
{
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
return 1;
return 0;
}

//url编码实现
void urlencode(unsigned char * src, int src_len, unsigned char * dest, int dest_len)
{
unsigned char ch;
int len = 0;
while(len < (dest_len - 4) && *src)
{
ch = (unsigned char) * src;
if(*src == ' ')
{
*dest++ = '+';
}
else if(is_alpha_number_char(ch) || strchr("-_.!~*'()", ch))
{
*dest++ = *src;
}
else
{
*dest++ = '%';
*dest++ = char_to_hex((unsigned char)(ch >> 4));
*dest++ = char_to_hex((unsigned char)(ch % 16));
}
++src;
++len;
}
*dest = 0;
return ;
}


/**
* @param str 需要解码的url字符串
* @param len 需要解码的url的长度
* @return int 返回解码后的url长度
*/
int urldecode(char *str, int len)
{
char *dest = str;
char *data = str;

int value;
int c;

while(len--)
{
if(*data == '+')
{
*dest = ' ';
}
else if(*data == '%' && len >= 2 && isxdigit((int) * (data + 1))
&& isxdigit((int) * (data + 2)))
{

c = ((unsigned char *)(data + 1))[0];
if(isupper(c))
c = tolower(c);
value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
c = ((unsigned char *)(data + 1))[1];
if(isupper(c))
c = tolower(c);
value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

*dest = (char)value ;
data += 2;
len -= 2;
}
else
{
*dest = *data;
}
data++;
dest++;
}
*dest = '\0';
return dest - str;
}

leetcode 65. Valid Number

发表于 2018-03-12 | 分类于 leetcode

Validate if a given string is numeric.

Some examples:

"0" => true

" 0.1 " => true

"abc" => false

"1 a" => false

"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution(object):
def isNumber(self, s):
"""
:type s: str
:rtype: bool
"""
#define DFA state transition tables
states = [{},
# State (1) - initial state (scan ahead thru blanks)
{'blank': 1, 'sign': 2, 'digit':3, '.':4},
# State (2) - found sign (expect digit/dot)
{'digit':3, '.':4},
# State (3) - digit consumer (loop until non-digit)
{'digit':3, '.':5, 'e':6, 'blank':9},
# State (4) - found dot (only a digit is valid)
{'digit':5},
# State (5) - after dot (expect digits, e, or end of valid input)
{'digit':5, 'e':6, 'blank':9},
# State (6) - found 'e' (only a sign or digit valid)
{'sign':7, 'digit':8},
# State (7) - sign after 'e' (only digit)
{'digit':8},
# State (8) - digit after 'e' (expect digits or end of valid input)
{'digit':8, 'blank':9},
# State (9) - Terminal state (fail if non-blank found)
{'blank':9}]
currentState = 1
for c in s:
# If char c is of a known class set it to the class name
if c in '0123456789':
c = 'digit'
elif c in ' \t\n':
c = 'blank'
elif c in '+-':
c = 'sign'
# If char/class is not in our state transition table it is invalid input
if c not in states[currentState]:
return False
# State transition
currentState = states[currentState][c]
# The only valid terminal states are end on digit, after dot, digit after e, or white space after valid input
if currentState not in [3,5,8,9]:
return False
return True

leetcode 72. Edit Distance

发表于 2018-03-12 | 分类于 leetcode

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character

b) Delete a character

c) Replace a character

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
l1, l2 = len(word1)+1, len(word2)+1
dp = [[0 for _ in xrange(l2)] for _ in xrange(l1)]
for i in xrange(l1):
dp[i][0] = i
for j in xrange(l2):
dp[0][j] = j
for i in xrange(1, l1):
for j in xrange(1, l2):
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+(word1[i-1]!=word2[j-1]))
return dp[-1][-1]

leetcode 62. Unique Paths

发表于 2018-03-10 | 分类于 leetcode

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

picture_1
Note: m and n will be at most 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int uniquePaths(int m, int n) {
int i = 0,j = 0;
if(m <= 0 || n <= 0)
{
return 0;
}
int dp[m][n];
for(i = 0;i < m;i++)
{
dp[i][0] = 1;
}
for(j = 0;j < n;j++)
{
dp[0][j] = 1;
}
for(i = 1;i < m;i++)
{
for(j = 1;j < n;j++)
{
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}

leetcode 55. Jump Game

发表于 2018-03-10 | 分类于 leetcode

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

1
2
3
4
5
6
7
8
9
10
11
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
earn = len(nums)-1
for i in range(len(nums))[::-1]:
if i + nums[i] >= earn:
earn = i
return not earn

leetcode 16. 3Sum Closest

发表于 2018-03-10 | 分类于 leetcode

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def threeSumClosest(self,nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
min_target = float("inf")
for i in range(len(nums)-2):
l,r = i + 1,len(nums)-1
while l < r:
if nums[i] + nums[l] + nums[r] - target == 0:
return target
if abs(nums[i] + nums[l] + nums[r] - target) <= abs(min_target - target):
min_target = nums[i] + nums[l] + nums[r]
if nums[i] + nums[l] + nums[r] > target:
r -= 1
elif nums[i] + nums[l] + nums[r] < target:
l += 1

return min_target

leetcode 15. 3Sum

发表于 2018-03-10 | 分类于 leetcode

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
res = []
for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i-1]:
continue
l,r = i + 1,len(nums)-1
while l < r:
s = nums[i] + nums[r] + nums[l]
if s > 0:
r -= 1
elif s < 0:
l += 1
else:
res.append([nums[i],nums[l],nums[r]])
while l < r and nums[l] == nums[l + 1]:
l += 1
while l < r and nums[r] == nums[r - 1]:
r -= 1
l += 1
r -= 1
return res
1…181920

zhuanli

194 日志
9 分类
41 标签
GitHub
© 2018 — 2019 zhuanli
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4