Notes Site.

RMI

post @ 2024-03-20

RMI

概述

RMI(Remote Method Invocation)是计算机之间通过网络实现对象调用的一种通讯机制。它允许在Java虚拟机(JVM)之间进行通信,使得在一个JVM中的对象可以调用另一个JVM中的对象的方法,就像这些对象都在同一个JVM中一样。

服务端(Server):

  • 服务端创建一个远程对象,并实现一个或多个远程接口。
  • 服务端启动 RMI 注册表,并将远程对象绑定到注册表中。

客户端(Client):

  • 客户端从 RMI 注册表中查找所需的远程对象。
  • 客户端获取远程对象的引用,并调用其方法。

测试环境:JDK8u41

RMIServer.java

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
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RMIServer {
// 接口
public interface IRemoteObj extends Remote {
public String hello(String para) throws RemoteException;
}
public class RemoteObjImpl extends UnicastRemoteObject implements IRemoteObj {
protected RemoteObjImpl() throws RemoteException {
super();
}
public String hello(String para) throws RemoteException {
System.out.println(para);
return "over";
}
}
private void start() throws Exception {
// 实例化远程对象
RemoteObjImpl h = new RemoteObjImpl();
// 创建注册中心
LocateRegistry.createRegistry(1099);
// 绑定对象示例到注册中心
Naming.rebind("rmi://127.0.0.1:1099/Remote", h);

// Naming.rebind(静态方法) 和 registry.bind(实例方法) 都是将一个远程对象绑定到 RMI 注册表中,Naming.rebind()方法是JNDI的一部分
// RemoteObjImpl h = new RemoteObjImpl();
// Registry registry = LocateRegistry.createRegistry(1099);
// registry.bind("Remote", h);
}

public static void main(String[] args) throws Exception {
new RMIServer().start();
}
}

Read More
post @ 2024-02-20

Python Prototype Pollution

Python原型链污染通常是指通过某种方式非法地修改了对象的原型链,导致对象访问到了不应该访问到的属性或方法,或者对象的属性被非法地修改

Python中,这通常意味着通过修改类的__dict__或对象的__class__属性,来实现对类或对象属性的非法修改

基类

1
2
3
4
5
6
7
8
9
string = ""
print(string.__class__) # <class 'str'>
print(string.__class__.__base__) # <class 'object'>

def function():
pass
print(function.__class__) # <class 'function'>
print(function.__class__.__base__) # <class 'object'>

原型链污染

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
class father:
secret = "xxxx"
class son_a(father):
pass
class son_b(father):
pass

# 将源字典 src 中的键值对合并到目标对象 dst 中
def merge(src, dst):
for k, v in src.items():
if hasattr(dst, '__getitem__'):
if dst.get(k) and type(v) == dict:
merge(v, dst.get(k))
else:
dst[k] = v
elif hasattr(dst, k) and type(v) == dict:
merge(v, getattr(dst, k))
else:
setattr(dst, k, v)

instance = son_b()
payload = {
"__class__": {
"__base__": {
"secret": "no"
}
}
}

print(son_a.secret) # xxxx
print(instance.secret) # xxxx

merge(payload, instance)
print(son_a.secret) # no
print(instance.secret) # no


# 污染内置属性
# payload = {
# "__class__" : {
# "__base__" : {
# "__str__" : "Polluted"
# }
# }
# }
#
# print(father.__str__) #<slot wrapper '__str__' of 'object' objects>
# merge(payload, instance)
# print(father.__str__) #Polluted

无法污染的Object

1
2
merge(payload, object)
# 报错:TypeError: can't set attributes of built-in/extension type 'object'

全局变量获取

Read More
⬆︎TOP